Build every database's schema from embedded SQL files (closes #56) #519

Merged
clawbot merged 1 commits from issue-56-sql-schema into next 2026-10-13 06:34:06 +02:00
Collaborator

Closes #56.

Each kind of database file gets its schema from SQL files embedded under internal/db/migrations/: main/ for webhooker.db, events/ for each events-{uuid}.db, archive/ for each archive file. 000_migration.sql creates schema_migrations (version INTEGER PRIMARY KEY, as in sneak/pixa) and records itself. 001_schema.sql is the schema. migrations.Apply runs, in one transaction, every file the database has not had, where AutoMigrate used to run. GORM stays for queries. No AutoMigrate is left, in tests either.

The SQL is the schema AutoMigrate built in fresh files: same columns, defaults, checks and indexes. The event schema leaves out the copies of users, webhooks, entrypoints and targets, and the foreign keys to them, so the sweep that cleaned those copies is deleted. The association guard stays: without it a delivery write now fails on the missing targets table.

Not visible in the diff:

  • AutoMigrate also made GORM parse every model before a handle was shared. GORM 1.25.5 races when two goroutines first use models that refer to each other, as the engine does at start, so the main and event databases now parse their models on open.
  • events/001_schema.sql inserts the event totals row; it is no longer created on every open.
  • The delivery tests' testWebhookDB opens through WebhookDBManager, so it has the guard.
  • Judgement call: the index test lost its half that expected a reopen to restore dropped indexes; pre-1.0 there is no upgrade path.
  • Deviation: one handler test renames its subtests. A repeated name gets #01, which SQLiteDSN does not escape, so that test's databases shared one file: #518.

Model: opus-5-5

Closes https://git.eeqj.de/sneak/webhooker/issues/56. Each kind of database file gets its schema from SQL files embedded under `internal/db/migrations/`: `main/` for `webhooker.db`, `events/` for each `events-{uuid}.db`, `archive/` for each archive file. `000_migration.sql` creates `schema_migrations` (`version INTEGER PRIMARY KEY`, as in sneak/pixa) and records itself. `001_schema.sql` is the schema. `migrations.Apply` runs, in one transaction, every file the database has not had, where `AutoMigrate` used to run. GORM stays for queries. No `AutoMigrate` is left, in tests either. The SQL is the schema `AutoMigrate` built in fresh files: same columns, defaults, checks and indexes. The event schema leaves out the copies of `users`, `webhooks`, `entrypoints` and `targets`, and the foreign keys to them, so the sweep that cleaned those copies is deleted. The association guard stays: without it a delivery write now fails on the missing `targets` table. Not visible in the diff: - `AutoMigrate` also made GORM parse every model before a handle was shared. GORM 1.25.5 races when two goroutines first use models that refer to each other, as the engine does at start, so the main and event databases now parse their models on open. - `events/001_schema.sql` inserts the event totals row; it is no longer created on every open. - The delivery tests' `testWebhookDB` opens through `WebhookDBManager`, so it has the guard. - Judgement call: the index test lost its half that expected a reopen to restore dropped indexes; pre-1.0 there is no upgrade path. - Deviation: one handler test renames its subtests. A repeated name gets `#01`, which `SQLiteDSN` does not escape, so that test's databases shared one file: https://git.eeqj.de/sneak/webhooker/issues/518. Model: opus-5-5
clawbot added the needs-review label 2026-10-13 05:30:50 +02:00
clawbot self-assigned this 2026-10-13 05:30:50 +02:00
clawbot added 1 commit 2026-10-13 05:30:50 +02:00
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.

AutoMigrate also had GORM parse every model before a handle was shared.
That is now done on its own: GORM races when two goroutines first use
models that refer to each other.

Model: opus-5-5
Author
Collaborator

Review passed.

Model: opus-5-5

Review passed. Model: opus-5-5
clawbot merged commit 15377f792b into next 2026-10-13 06:34:06 +02:00
clawbot deleted branch issue-56-sql-schema 2026-10-13 06:34:08 +02:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/webhooker#519