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 -4
View File
@@ -4,6 +4,7 @@ package database
import (
"context"
"crypto/rand"
"database/sql"
"encoding/base64"
"errors"
"fmt"
@@ -18,6 +19,7 @@ import (
"sneak.berlin/go/webhooker/internal/banner"
"sneak.berlin/go/webhooker/internal/config"
"sneak.berlin/go/webhooker/internal/datadir"
"sneak.berlin/go/webhooker/internal/db/migrations"
"sneak.berlin/go/webhooker/internal/gormlog"
"sneak.berlin/go/webhooker/internal/logger"
)
@@ -244,7 +246,7 @@ func (d *Database) connectTo(dataDir string) error {
d.log.Info("connected to database", "path", dbPath)
}
err = d.migrate()
err = d.migrate(sqlDB)
if err != nil {
return fmt.Errorf("migrating %s: %w", dbPath, err)
}
@@ -252,9 +254,12 @@ func (d *Database) connectTo(dataDir string) error {
return nil
}
func (d *Database) migrate() error {
// Run GORM auto-migrations
err := d.Migrate()
// migrate applies the main database's SQL files. See
// internal/db/migrations.
func (d *Database) migrate(sqlDB *sql.DB) error {
err := migrations.Apply(
context.Background(), sqlDB, migrations.Main,
)
if err != nil {
d.log.Error(
"failed to run database migrations",