From ade689fc8ef6d34cb9dd940906428f70741eff49 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Fri, 25 Sep 2026 18:40:12 +0200 Subject: [PATCH] Refuse to run the destructive store tests against a real database MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These TRUNCATE every table before each test. Pointed at a deployment that is a silent and complete data loss — every key, every user, every conversation. Not hypothetical. I did it: an SSH tunnel to the production database had been opened minutes earlier for a perfectly good reason, and the same DSN was then handed to the test suite. Twenty-one users, nine API keys, eighteen sessions, four hundred and sixty-one usage records and three runtime-added models, gone between one command and the next. Recovered only because the pre-migration SQLite file was still there. The guard is the database name. A test database has to say so, because a check that could be satisfied by accident would not have stopped this one — the DSN was entirely correct for what it had been opened for, and catastrophic for what it was then used for. Co-Authored-By: Claude Opus 5 (1M context) --- internal/store/postgres_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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)