From b9645752f5800e487b55e7944082766453c5b7c2 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 12 Sep 2026 14:21:28 -0400 Subject: [PATCH] fix(db): skip SQL comment lines when parsing schema table names parseTableNames() regex-scanned every line of schema.sql, comments included, with 'CREATE TABLE (?:IF NOT EXISTS )?(?:\w+\.)?(\w+)'. A doc comment containing that phrase in prose - e.g. 'declared in CREATE TABLE above' - registered a phantom table ('above'), and startup verification then failed with 'missing tables: above', crash-looping the app container on every restart. Skip lines whose trimmed form starts with '--' so comments can never contribute table names, and add a regression test asserting every parsed table maps back to a real CREATE TABLE statement. --- internal/database/schema.go | 6 ++++++ internal/database/schema_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 internal/database/schema_test.go diff --git a/internal/database/schema.go b/internal/database/schema.go index 8a19e54..ae7abe8 100644 --- a/internal/database/schema.go +++ b/internal/database/schema.go @@ -77,6 +77,12 @@ func parseTableNames() ([]string, error) { tables := make(map[string]bool) for scanner.Scan() { line := scanner.Text() + // Skip SQL comments: a doc line like "-- ... declared in CREATE + // TABLE above" would otherwise register a phantom table and fail + // startup verification. + if strings.HasPrefix(strings.TrimSpace(line), "--") { + continue + } matches := pattern.FindStringSubmatch(line) if len(matches) > 1 { tableName := matches[1] diff --git a/internal/database/schema_test.go b/internal/database/schema_test.go new file mode 100644 index 0000000..92fdaec --- /dev/null +++ b/internal/database/schema_test.go @@ -0,0 +1,28 @@ +package database + +import ( + "regexp" + "testing" +) + +// TestParseTableNamesIgnoresComments guards against phantom tables parsed out +// of SQL comments (e.g. "-- ... declared in CREATE TABLE above" once +// registered a table named "above" and crashed startup verification). +func TestParseTableNamesIgnoresComments(t *testing.T) { + tables, err := parseTableNames() + if err != nil { + t.Fatalf("parseTableNames() error: %v", err) + } + if len(tables) == 0 { + t.Fatal("parseTableNames() returned no tables") + } + + for _, name := range tables { + // Every parsed table must correspond to a real CREATE TABLE statement + // at the start of a (non-comment) line. + stmt := regexp.MustCompile(`(?m)^CREATE TABLE (?:IF NOT EXISTS )?(?:\w+\.)?` + regexp.QuoteMeta(name) + `\s`) + if !stmt.MatchString(SchemaFile) { + t.Errorf("parseTableNames() returned phantom table %q with no matching CREATE TABLE statement", name) + } + } +}