Build every database's schema from embedded SQL files (closes #56)
check / check (push) Failing after 2m26s

The main database, each webhook's event database and each archive file
get their schema from SQL files under internal/db/migrations/, one
directory per kind. Opening a file runs, in one transaction, each file
that schema_migrations does not list. GORM stays for queries.
AutoMigrate is gone, tests included.

An event database holds only its own tables now, so the sweep that
removed target rows copied into it is deleted. The association guard
stays: without it a delivery write fails on the missing targets table.
The event totals row is inserted by events/001_schema.sql rather than
on every open.

Model: opus-5-5
This commit is contained in:
2026-10-13 02:00:36 +00:00
parent 957de7f99f
commit f69de8460c
23 changed files with 714 additions and 711 deletions
+9 -8
View File
@@ -22,6 +22,7 @@ import (
_ "modernc.org/sqlite" // Pure Go SQLite driver.
"sneak.berlin/go/webhooker/internal/database"
"sneak.berlin/go/webhooker/internal/database/databasetest"
"sneak.berlin/go/webhooker/internal/db/migrations"
"sneak.berlin/go/webhooker/internal/delivery"
"sneak.berlin/go/webhooker/internal/gormlog"
)
@@ -166,16 +167,16 @@ func seedArchiveFile(
)
require.NoError(t, err)
require.NoError(t, migrations.Apply(
t.Context(), sqlDB, migrations.Archive,
))
gdb, err := gorm.Open(
sqlite.Dialector{Conn: sqlDB},
&gorm.Config{Logger: gormlog.New(slog.New(slog.DiscardHandler))},
)
require.NoError(t, err)
require.NoError(
t, gdb.AutoMigrate(&delivery.ExportArchivedEvent{}),
)
for i, at := range archivedAt {
row := delivery.ExportArchivedEvent{
EventID: fmt.Sprintf("ev-%d", i),
@@ -781,9 +782,9 @@ func TestArchiveSweep_NeverExpiryUntouched(t *testing.T) {
//
// The spec is stronger than that — a "never" archive is skipped
// before any file is touched — so the archive here exists but has
// never been migrated. Opening it at all would run AutoMigrate
// and create the archive table, which is exactly what must not
// happen.
// never been migrated. Opening it at all would apply the archive
// schema and create the archive table, which is exactly what must
// not happen.
func TestArchiveSweep_NeverExpirySkipsBeforeOpening(
t *testing.T,
) {
@@ -807,7 +808,7 @@ func TestArchiveSweep_NeverExpirySkipsBeforeOpening(
// seedUnmigratedArchive creates an archive file that exists but
// carries no archive schema, so any open of it is observable: the
// archive table appears only if something ran AutoMigrate.
// archive table appears only if something applied the schema.
func seedUnmigratedArchive(t *testing.T, path string) {
t.Helper()