Files
bookhoard/internal/database/schema_test.go
T
John O'Keefe b9645752f5 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.
2026-09-12 14:21:28 -04:00

29 lines
915 B
Go

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)
}
}
}