Skip to content
Back to skills

Add Migration

ASecurity

Use when adding or changing a SQLite table, column, index, or CHECK constraint in the Watchtower Go backend — schema migrations, goose files, expanding entity_type/source_type/trigger_type enums, or keeping schema.sql in sync for the AI prompt.

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 11, 2026
ai-agentsgoswiftbashsqldatabasebackend

Works with

  • mcp

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add aiwatchtowers/watchtower --skill add-migration --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Add Migration?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Add Migration
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/aiwatchtowers-add-migration/badge)](https://www.skillsdirectory.com/skills/aiwatchtowers-add-migration)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: add-migration
description: Use when adding or changing a SQLite table, column, index, or CHECK constraint in the Watchtower Go backend — schema migrations, goose files, expanding entity_type/source_type/trigger_type enums, or keeping schema.sql in sync for the AI prompt.
---

# Add a DB Migration (Watchtower)

Watchtower uses **goose** migrations (numbered `.sql` files), NOT PRAGMA `user_version` or a hand-edited version int. CLAUDE.md's "schema version N" notes are historical — ignore them; follow the goose flow below.

## Steps

1. **Create the migration file** `internal/db/migrations/0000N_<name>.sql` (next number after the highest existing — currently `00003`). It is auto-discovered via `//go:embed`; no registration code. Every file needs both blocks:
   ```sql
   -- +goose Up
   ALTER TABLE targets ADD COLUMN notified_at TEXT NOT NULL DEFAULT '';

   -- +goose Down
   ALTER TABLE targets DROP COLUMN notified_at;
   ```

2. **Changing a CHECK constraint?** SQLite has no `ALTER TABLE ... ADD CONSTRAINT`. You must recreate the table. Copy the CREATE-new/INSERT-SELECT/DROP/RENAME **structure** from `00003_catchup_review.sql` — but **not** its FK-pragma handling: `00003` uses `defer_foreign_keys` and gets away with it only because `feedback` has no `ON DELETE CASCADE` children. `00002_target_due_inbox.sql` used the same `defer_foreign_keys` and it *is* the documented antipattern below (it wiped `inbox_feedback`). Neither migration shows the correct FK handling.
   - If the table has inbound `ON DELETE CASCADE` FKs (a parent table), wrap the whole dance in `PRAGMA foreign_keys = OFF;` / `PRAGMA foreign_keys = ON;` (issued outside any transaction) — never `defer_foreign_keys`. See the **Gotchas** entry below and `TestMigration_TableRecreationPreservesCascadeChildren` for the correct pattern.
   - **Run the rebuild as one transaction (required).** Start the file with `-- +goose NO TRANSACTION`; put `PRAGMA foreign_keys = OFF;` first and `PRAGMA foreign_keys = ON;` last, each as its own statement outside the transaction; put everything between them as `BEGIN IMMEDIATE;` … `COMMIT;` inside ONE `-- +goose StatementBegin` / `-- +goose StatementEnd` block, so goose sends it as one multi-statement exec. A failure part-way then leaves every table as it was. Separate `BEGIN` and `COMMIT` statements fail: the SQLCipher driver discards a connection left inside a transaction between execs (`internal/sqlcipher/wt_session.go`), which rolls it back. Reference: `00150_builtin_automations.sql`. The Down follows the same shape.
   - `CREATE TABLE x_new (...)` with the expanded CHECK → `INSERT INTO x_new SELECT ... FROM x` (filter legacy rows in the Down) → `DROP TABLE x` → `ALTER TABLE x_new RENAME TO x` → recreate ALL indexes (`CREATE INDEX IF NOT EXISTS ...`).
   - The three enums that recur: `feedback.entity_type`, `targets.source_type`, `inbox_items.trigger_type`.

3. **Mirror the change in `internal/db/schema.sql`** — this file is embedded (`schema_embed.go`) and injected verbatim into the AI system prompt so the model can query via MCP. A new table/column the AI should see MUST land here too.

4. **New table → add it to `TestAllTablesExist`** in `internal/db/db_test.go` (hardcoded list; the test fails otherwise — this is the intended guard).

5. **Regenerate the schema snapshot and the Swift test schema** and commit them:
   ```bash
   go test ./internal/db/ -run 'TestSchemaGolden|TestDesktopTestSchema' -update
   ```
   Commit the migration AND the updated `internal/db/testdata/*.golden` and `WatchtowerDesktop/Tests/Support/TestDatabase+Schema.swift` (generated from the migrated DB; Desktop tests run against it).

6. **Frees a lot of space?** A migration that rewrites or deletes most of a big table (00134 rewrote every `messages.raw_json`) adds its version to `compactAfterMigrations` in `internal/db/compact.go`, so the next launch compacts the file once with `VACUUM` (`docs/features/db-compact.md`). Never put `VACUUM` in the migration itself: the Desktop kills `db migrate` after 30 s.

7. **Verify:** `go test ./internal/db/ -run 'TestMigrationIdempotent|TestAllTablesExist|TestSchemaGolden|TestDesktopTestSchema'`.

## Gotchas

- **Recreating a table that is the parent of an `ON DELETE CASCADE` FK? Don't use `defer_foreign_keys`, use `PRAGMA foreign_keys = OFF`.** Tables like `inbox_items` (← `inbox_feedback`), `digests` (← `digest_topics`/`digest_participants`/`decision_reads`), `targets` (← `target_links`), and `tracks` (← track children) are all referenced by a child with `ON DELETE CASCADE`. The table-recreation dance's `DROP TABLE x` deletes every row of `x`, and with `PRAGMA foreign_keys=ON` (set unconditionally in `db.go`) that DROP fires the cascade immediately, silently wiping every child row. `PRAGMA defer_foreign_keys = ON` does **not** prevent this — it only postpones the *validation* of dangling FK references until commit, it does nothing to the `ON DELETE CASCADE` *action*, which still runs the instant the parent rows disappear. **Incident:** migration `00002_target_due_inbox.sql` used `defer_foreign_keys` when recreating `inbox_items` and it erased every row of `inbox_feedback`; the migration is already applied and cannot be edited, so this is now a permanent scar — don't repeat it. The correct guard is `PRAGMA foreign_keys = OFF;` wrapped around the whole create-new/insert-select/drop/rename sequence (issued outside any transaction — SQLite refuses to toggle `foreign_keys` while a `BEGIN` is open), then `PRAGMA foreign_keys = ON;` once the table is back. See `TestMigration_TableRecreationPreservesCascadeChildren` in `internal/db/migration_fk_recreation_test.go` for both patterns side by side.
- **`schema.sql` is checked at runtime, not only by the prompt.** `db.Open` compares every table `schema.sql` declares with `sqlite_master` after migrating (`internal/db/schema_drift.go`). A table that exists only in `schema.sql`, with no migration that creates it, fails `Open` with a `SchemaDriftError`. Every table in `schema.sql` must be created by a migration, under the same name.
- **Version 125 is a Go migration** (`internal/db/migration_00125.go`, registered with goose; there is no `00125_*.sql`, and adding one panics with goose's "duplicate version"). It is the exception, kept only as a tolerant repair (#667: it adds its columns only when absent). Write new migrations as SQL; a Go migration is only for such a repair, and it must list the tables it touches in `goMigrationTouches` so the schema-drift check (`loadMigrations` in `schema_drift.go`) sees its version and its changes.
- **Don't bump `CurrentSchemaFormat`** (`internal/db/migrations.go`). It's the migration-*engine* version (=2, goose), not your schema version. Goose tracks applied versions in `goose_db_version` automatically.
- **`SetMaxOpenConns(1)`** is already set in `db.go:44` — never remove it. `:memory:` gives each connection a *separate* DB, and per-connection pragmas wouldn't apply to a pool. Any new `db.Open`/`sql.Open` you add keeps this.
- **Down must be real.** Tests replay migrations; a stub Down that loses data fails idempotence.
- **Recreate every index** after a table-recreation, or queries silently lose their index. Partial unique indexes carry a contract, not just speed: a rebuild of `targets` must recreate `idx_targets_action_source` and a rebuild of `agent_actions` must recreate `idx_agent_actions_open_context`, each with its exact `WHERE` clause (copy them from `schema.sql`; AGENT-05, guarded by `TestAgent05_ButtonIndexesPresent`).
- **Generated columns** (e.g. `messages.ts_unix GENERATED ALWAYS AS ...`) must be reproduced in the new table def; never INSERT into them.
- **Rebuilding `messages` copies `rowid`.** `messages` has no `INTEGER PRIMARY KEY`, and `messages_fts` rows are keyed by `messages.rowid` (`transcripts_fts` by `meeting_transcripts.id`), so a plain `INSERT INTO messages_new SELECT ...` renumbers the rows and points every index row at the wrong message. Write `INSERT INTO messages_new(rowid, ...) SELECT rowid, ... FROM messages`, and recreate the rowid-keyed triggers (copy them from `schema.sql`). Never write `messages` with `INSERT OR REPLACE`/`REPLACE INTO`: it gives the row a new rowid. SEARCHIDX-01 (`docs/inventory/search-indexes.md`); `TestSearchIdx01_MigrationsKeepMessagesRowid` fails on a rebuild that loses the rowid.
- **Contract change?** If the table is covered by `docs/inventory/`, update/add a guard test in `internal/db/schema_contracts_test.go` — and if a guard would weaken, **stop and ask the owner** (per CLAUDE.md).

## Reference files
- Engine: `internal/db/migrations.go` · existing migrations: `internal/db/migrations/00001_init.sql`, `00002_target_due_inbox.sql`, `00003_catchup_review.sql`
- AI schema: `internal/db/schema.sql` (+ `schema_embed.go`) · pragmas: `internal/db/db.go`
- Tests/guards: `internal/db/db_test.go`, `schema_contracts_test.go`, `schema_snapshot_test.go`

When done, run `local-review` before opening a PR.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.