Stop calling the PostgreSQL store SQLite, and stop guessing where it is

Two things that were only ever confusing, and one of them stopped a command
working on the cluster.

The store type was named SQLite and kept the name when PostgreSQL was added, so
OpenPostgres returned a *SQLite and all seventy-odd methods on the production
store were spelled after the database it was not running on. Nothing broke;
people did. Earlier today I added three methods to *SQLite for a table that on
this cluster only ever lives in PostgreSQL, and the name did not once look
wrong. It is a field, which is all it ever was.

`llmgw users` and `llmgw keys` defaulted --store to a SQLite path. On a
deployment whose store is PostgreSQL every one of them failed with "unable to
open database file" while the right answer sat in auth.store in the config file
beside them; the conclusion an operator draws is that the tool is broken, and
they are not far off. They now ask the server's own configuration before
falling back, and the error names which of the three sources chose the address
— the failure was never the path, it was which one picked it.

The single-machine default still applies last, so a deployment with no config
file keeps working with no flags.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-27 12:20:40 +02:00
1 parent 3febb6ef63
commit ebce41e22d
32 files changed
+320 -140

No files matched your search

+6 -4
View File
@@ -16,7 +16,7 @@ import (
)
func newTenancyCmd() *cobra.Command {
var dbPath string
var sf storeFlags
cmd := &cobra.Command{
Use: "keys",
@@ -25,9 +25,11 @@ func newTenancyCmd() *cobra.Command {
"can be issued before the server is running and without exposing an endpoint\n" +
"that mints credentials.",
}
cmd.PersistentFlags().StringVar(&dbPath, "store", "/var/lib/llmgw/llmgw.db", "path to the store")
sf.bind(cmd)
open := func(ctx context.Context) (*store.SQLite, error) { return store.Open(ctx, dbPath) }
// Takes a context rather than the command, because some of these run
// before the command tree is entered.
open := func(ctx context.Context) (*store.DB, error) { return sf.openCtx(ctx) }
cmd.AddCommand(
newKeyInitCmd(open),
@@ -42,7 +44,7 @@ func newTenancyCmd() *cobra.Command {
return cmd
}
type opener func(context.Context) (*store.SQLite, error)
type opener func(context.Context) (*store.DB, error)
func newKeyInitCmd(open opener) *cobra.Command {
var org, team, project string
+1 -1
View File
@@ -362,7 +362,7 @@ func startControlPlane(ctx context.Context, cfg config.Server, cat *catalog.Live
// the sink is the router when the router is serving, and nothing when a
// control plane runs on its own.
var lead *control.Leadership
if h, ok := sink.(interface{ Store() *storepkg.SQLite }); ok {
if h, ok := sink.(interface{ Store() *storepkg.DB }); ok {
if st := h.Store(); st != nil {
lead = control.NewLeadership(st, cfg.Control.LeaseTTL, log)
ctl.SetLeadership(lead)
+76
View File
@@ -0,0 +1,76 @@
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// defaultStorePath is where a single-machine deployment keeps its store.
const defaultStorePath = "/var/lib/llmgw/llmgw.db"
// defaultConfigPath is where the control plane's configuration usually lives.
//
// Consulted rather than required. The commands that work on the store run on
// the same machine as the server far more often than not, and the server
// already knows where the store is — asking it is better than making the
// operator repeat an answer that is written down three lines away.
const defaultConfigPath = "/etc/llmgw/server.yaml"
// storeFlags is how a command that works directly on the store finds it.
type storeFlags struct {
address string
config string
}
// bind adds --store and --config to a command.
func (f *storeFlags) bind(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&f.address, "store", "",
"store address: a SQLite path, a postgres:// DSN, or @file holding one "+
"(default: auth.store from --config, else "+defaultStorePath+")")
cmd.PersistentFlags().StringVar(&f.config, "config", defaultConfigPath,
"server configuration to read the store address from")
}
// open resolves the store and opens it.
//
// Explicit --store wins; otherwise the server's own configuration is asked,
// and only then does the single-machine default apply. That order exists
// because the default was a SQLite path and nothing else: on a cluster whose
// store is PostgreSQL, every one of these commands failed with "unable to open
// database file" while the right answer sat in the config file beside it. The
// operator's conclusion was that the tool was broken, and they were not far off.
func (f *storeFlags) open(cmd *cobra.Command) (*store.DB, error) {
return f.openCtx(cmd.Context())
}
// openCtx is open for a caller that has a context rather than a command.
func (f *storeFlags) openCtx(ctx context.Context) (*store.DB, error) {
address, from := f.resolve()
s, err := store.Open(ctx, address)
if err != nil {
return nil, fmt.Errorf("opening the store at %s (%s): %w", address, from, err)
}
return s, nil
}
// resolve returns the address and where it came from, for the error message.
//
// Where it came from is half the message. "unable to open database file" says
// nothing about which of three sources chose that path, and the whole problem
// is that the wrong one did.
func (f *storeFlags) resolve() (address, from string) {
if f.address != "" {
return f.address, "--store"
}
if f.config != "" {
if cfg, err := config.Load(f.config); err == nil && cfg.Auth.Store != "" {
return cfg.Auth.Store, "auth.store in " + f.config
}
}
return defaultStorePath, "the default; no --store and nothing usable in " + f.config
}
+82
View File
@@ -0,0 +1,82 @@
package cli
import (
"os"
"path/filepath"
"strings"
"testing"
)
func writeConfig(t *testing.T, body string) string {
t.Helper()
p := filepath.Join(t.TempDir(), "server.yaml")
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
return p
}
func TestAnExplicitStoreWins(t *testing.T) {
cfg := writeConfig(t, "listen: \":8080\"\nauth:\n enabled: true\n store: postgres://from-config/db\n")
f := storeFlags{address: "postgres://explicit/db", config: cfg}
got, from := f.resolve()
if got != "postgres://explicit/db" || !strings.Contains(from, "--store") {
t.Errorf("an explicit --store should win; got %q from %q", got, from)
}
}
func TestTheServerConfigIsAskedBeforeTheDefault(t *testing.T) {
cfg := writeConfig(t, "listen: \":8080\"\nauth:\n enabled: true\n store: postgres://from-config/db\n")
f := storeFlags{config: cfg}
got, from := f.resolve()
// The whole defect: on a cluster whose store is PostgreSQL, these commands
// reached for a SQLite path and failed with "unable to open database file"
// while the right answer sat in the config beside them.
if got != "postgres://from-config/db" {
t.Errorf("expected the address from the config, got %q", got)
}
if !strings.Contains(from, "auth.store") {
t.Errorf("the source should be named for the error message; got %q", from)
}
}
func TestTheSingleMachineDefaultStillApplies(t *testing.T) {
f := storeFlags{config: filepath.Join(t.TempDir(), "absent.yaml")}
got, from := f.resolve()
// A deployment with no config file is the single-machine one, and it worked
// before this existed. It has to keep working with no flags at all.
if got != defaultStorePath {
t.Errorf("expected the default path, got %q", got)
}
if !strings.Contains(from, "default") {
t.Errorf("the source should say it fell back; got %q", from)
}
}
func TestAConfigWithNoStoreFallsThrough(t *testing.T) {
// Authentication off means no store at all. Reading an empty value as the
// answer would open a database named "".
cfg := writeConfig(t, "listen: \":8080\"\n")
f := storeFlags{config: cfg}
if got, _ := f.resolve(); got != defaultStorePath {
t.Errorf("expected the default when the config names no store, got %q", got)
}
}
func TestTheErrorSaysWhichSourceChoseThePath(t *testing.T) {
f := storeFlags{address: filepath.Join(t.TempDir(), "nope", "x.db")}
_, err := f.openCtx(t.Context())
if err == nil {
t.Fatal("expected opening a store in a missing directory to fail")
}
// "unable to open database file" says nothing about which of three sources
// picked that path, and the whole problem is that the wrong one did.
if !strings.Contains(err.Error(), "--store") {
t.Errorf("the error should name where the address came from; got %q", err)
}
}
+5 -5
View File
@@ -12,7 +12,7 @@ import (
)
func newUsersCmd() *cobra.Command {
var dbPath string
var sf storeFlags
cmd := &cobra.Command{
Use: "users",
@@ -22,9 +22,9 @@ func newUsersCmd() *cobra.Command {
"the step that decides who gets to spend the cluster, and it needs a shell\n" +
"on this machine rather than a session cookie.",
}
cmd.PersistentFlags().StringVar(&dbPath, "store", "/var/lib/llmgw/llmgw.db", "path to the store")
open := func(cmd *cobra.Command) (*store.SQLite, error) {
return store.Open(cmd.Context(), dbPath)
sf.bind(cmd)
open := func(cmd *cobra.Command) (*store.DB, error) {
return sf.open(cmd)
}
cmd.AddCommand(newUserListCmd(open), newUserApproveCmd(open),
@@ -38,7 +38,7 @@ func newUsersCmd() *cobra.Command {
return cmd
}
type userOpener func(*cobra.Command) (*store.SQLite, error)
type userOpener func(*cobra.Command) (*store.DB, error)
func newUserListCmd(open userOpener) *cobra.Command {
var pending bool