Skip to content

fix(db): make earliest migrations idempotent (0002/0011/0012) - #503

Merged
jonathonbyrdziak merged 1 commit into
mainfrom
hb/migration-idempotency
Jul 22, 2026
Merged

fix(db): make earliest migrations idempotent (0002/0011/0012)#503
jonathonbyrdziak merged 1 commit into
mainfrom
hb/migration-idempotency

Conversation

@jonathonbyrdziak

Copy link
Copy Markdown
Contributor

Turns fully-specced but never-shipped autonomous-cycle issue #486 into a PR.

Problem

DatabaseManager.runMigrations fails fast — any error in a migration's up SQL is rethrown and aborts DB init (DatabaseManager.ts:138). Migrations 0002, 0011, 0012 are the earliest and predate the IF NOT EXISTS convention that every migration from 0009 onward already follows.

On any install where the tables/columns already exist but aren't recorded in sulla_migrations (pre-tracking installs, restored DBs, a manually seeded migration table), these three re-run their up and throw 42P07 (duplicate table) / duplicate column — bricking boot.

Fix

  • 0002CREATE TABLE agent_awarenessCREATE TABLE IF NOT EXISTS
  • 0011CREATE TABLE sulla_settingsCREATE TABLE IF NOT EXISTS; also fixes a latent down-migration bug — it dropped settings instead of sulla_settings
  • 0012ADD COLUMN "cast"ADD COLUMN IF NOT EXISTS; down → DROP COLUMN IF EXISTS

Audit

Swept all 22 registered migrations: these three are the only non-idempotent DDL. 0009/0014/0024 already use ADD COLUMN IF NOT EXISTS; 0010 indexes use CREATE INDEX IF NOT EXISTS; no CREATE TYPE/enum. Schema-only, no user data (complies with the no-user-data-in-migrations rule).

Verification (this PR)

  • tsc --noEmit --isolatedModules exit 0 — template literals balanced.
  • Confirmed against the live DB that agent_awareness, sulla_settings, and the sulla_settings."cast" column all already exist (the exact bricking precondition).
  • Ran all three fixed up statements against that live DB → all succeed as no-ops (0 rows affected). Pre-fix these would have thrown. Idempotency proven.

Diff: 3 files, +6 −6.

Closes #486

🤖 Generated with Claude Code during an autonomous heartbeat cycle.

DatabaseManager.runMigrations fails fast — any error in a migration's up SQL
aborts DB init. Migrations 0002/0011/0012 are the earliest and predate the
IF NOT EXISTS convention that every migration from 0009 onward follows. On any
install where the objects already exist but aren't recorded in sulla_migrations
(pre-tracking installs, restored DBs, manually seeded migration table), these
three re-run their up and throw 42P07 (duplicate table) / duplicate column,
bricking boot.

- 0002: CREATE TABLE agent_awareness -> CREATE TABLE IF NOT EXISTS
- 0011: CREATE TABLE sulla_settings  -> CREATE TABLE IF NOT EXISTS; also fixes a
        latent down-migration bug (it dropped 'settings' instead of 'sulla_settings')
- 0012: ADD COLUMN "cast" -> ADD COLUMN IF NOT EXISTS; down -> DROP COLUMN IF EXISTS

Swept all 22 registered migrations: these three are the only non-idempotent DDL.
Schema-only, no user data. Verified: tsc --isolatedModules exit 0; ran all three
fixed up statements against the live DB (objects already present) -> all succeed
as no-ops (pre-fix they threw).

Closes #486

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant