A path is still SQLite and a postgres:// URL is PostgreSQL, so every existing configuration keeps working untouched and moving is one line changed rather than a new setting to find. An address of the form @/path/to/file reads the real one out of that file, which is how a DSN carrying a password stays out of a configuration that gets copied, diffed and pasted into issues. The driver is pgx, which is pure Go. That is not a preference: the whole binary is built with CGO_ENABLED=0 so it runs on a node whose libc is older than the builder's, and a cgo driver would take that away from every deployment, including the ones still on SQLite. Tested against a real PostgreSQL, which found two things nothing else could. The schema_migrations table is created in Go rather than in a migration file, so it escaped the port and still said INTEGER — 64 bits in SQLite, 32 in PostgreSQL, and a millisecond timestamp does not fit, so the very first migration failed to record that it had run. And the memory migration uses an FTS5 virtual table with three triggers keeping it in step, which has no counterpart: PostgreSQL derives a tsvector from the row, so there is nothing to keep in step and no trigger to forget. That leaves exactly one query that is not the same sentence in both dialects — the note search, MATCH against @@ — and it is written out twice rather than hidden behind a shared spelling that would mean something subtly different in each. Everything else is one set of queries. The text search uses the 'simple' configuration rather than English. These are an agent's own notes, in whatever language it chose, and stemming English rules over Turkish would quietly stop finding things. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAPathIsStillSQLite(t *testing.T) {
|
|
// Every existing configuration names a file, and every one of them has to
|
|
// keep working without being touched.
|
|
for _, addr := range []string{
|
|
"/var/lib/llmgw/llmgw.db", "llmgw.db", "./data/llmgw.db",
|
|
} {
|
|
if isPostgres(addr) {
|
|
t.Errorf("%q was read as a PostgreSQL address", addr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAPostgresURLIsRecognised(t *testing.T) {
|
|
for _, addr := range []string{
|
|
"postgres://u:p@h:5432/db", "postgresql://u@h/db?sslmode=disable",
|
|
} {
|
|
if !isPostgres(addr) {
|
|
t.Errorf("%q was not read as a PostgreSQL address", addr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnAddressFileKeepsThePasswordOutOfTheConfig(t *testing.T) {
|
|
// A DSN carries a password, and a configuration file gets copied, diffed
|
|
// and pasted into issues. The indirection is what lets the secret live
|
|
// somewhere with its own permissions.
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "dsn")
|
|
// With the trailing newline every editor and every echo adds, which must
|
|
// not become part of the password.
|
|
if err := os.WriteFile(p, []byte("postgres://u:secret@h/db\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := resolveAddress("@" + p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != "postgres://u:secret@h/db" {
|
|
t.Errorf("resolved to %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAMissingOrEmptyAddressFileSaysSo(t *testing.T) {
|
|
// Rather than falling back to creating a SQLite file named "@/etc/...",
|
|
// which is what a silent failure here would produce.
|
|
if _, err := resolveAddress("@/nonexistent/dsn"); err == nil {
|
|
t.Error("a missing address file was accepted")
|
|
}
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "empty")
|
|
if err := os.WriteFile(p, []byte(" \n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := resolveAddress("@" + p)
|
|
if err == nil || !strings.Contains(err.Error(), "empty") {
|
|
t.Errorf("an empty address file gave %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOpenStillCreatesASQLiteFile(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "llmgw.db")
|
|
s, err := Open(context.Background(), p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
if s.dialect != SQLiteDialect {
|
|
t.Errorf("a path opened as %q", s.dialect)
|
|
}
|
|
if _, err := os.Stat(p); err != nil {
|
|
t.Errorf("no database file was created: %v", err)
|
|
}
|
|
}
|