diff --git a/internal/store/postgres_test.go b/internal/store/postgres_test.go index 6748211..57465f7 100644 --- a/internal/store/postgres_test.go +++ b/internal/store/postgres_test.go @@ -3,7 +3,9 @@ package store import ( "context" "errors" + "net/url" "os" + "strings" "testing" "time" ) @@ -24,6 +26,8 @@ func openPG(t *testing.T) *SQLite { if dsn == "" { t.Skip("set LLMGW_TEST_POSTGRES to a database DSN to run these") } + requireTestDatabase(t, dsn) + s, err := OpenPostgres(context.Background(), dsn) if err != nil { t.Fatalf("opening the test database: %v", err) @@ -46,6 +50,32 @@ func openPG(t *testing.T) *SQLite { return s } +// requireTestDatabase refuses to run against a database that is not obviously +// a test one. +// +// These tests truncate every table before each one. Pointed at a real +// deployment that is a silent, complete data loss — every key, every user, +// every conversation — and it is not a hypothetical: it happened here, through +// an SSH tunnel to the production database that had been opened minutes +// earlier for a perfectly good reason. The DSN was correct for what it was +// opened for and catastrophic for what it was then used for. +// +// The name has to say so. A guard that could be satisfied by accident would +// not have stopped it. +func requireTestDatabase(t *testing.T, dsn string) { + t.Helper() + u, err := url.Parse(dsn) + if err != nil { + t.Fatalf("LLMGW_TEST_POSTGRES is not a URL: %v", err) + } + name := strings.TrimPrefix(u.Path, "/") + if !strings.Contains(strings.ToLower(name), "test") { + t.Fatalf("refusing to run destructive tests against database %q:\n"+ + " these TRUNCATE every table before each test.\n"+ + " Point LLMGW_TEST_POSTGRES at a database whose name contains \"test\".", name) + } +} + func TestPostgresCarriesTheWholeTenancyTree(t *testing.T) { ctx := context.Background() s := openPG(t)