fix(db): make earliest migrations idempotent (0002/0011/0012) - #503
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Turns fully-specced but never-shipped autonomous-cycle issue #486 into a PR.
Problem
DatabaseManager.runMigrationsfails fast — any error in a migration'supSQL is rethrown and aborts DB init (DatabaseManager.ts:138). Migrations 0002, 0011, 0012 are the earliest and predate theIF NOT EXISTSconvention 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 theirupand throw42P07(duplicate table) / duplicate column — bricking boot.Fix
CREATE TABLE agent_awareness→CREATE TABLE IF NOT EXISTSCREATE TABLE sulla_settings→CREATE TABLE IF NOT EXISTS; also fixes a latent down-migration bug — it droppedsettingsinstead ofsulla_settingsADD COLUMN "cast"→ADD COLUMN IF NOT EXISTS; down →DROP COLUMN IF EXISTSAudit
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 useCREATE INDEX IF NOT EXISTS; noCREATE TYPE/enum. Schema-only, no user data (complies with the no-user-data-in-migrations rule).Verification (this PR)
tsc --noEmit --isolatedModulesexit 0 — template literals balanced.agent_awareness,sulla_settings, and thesulla_settings."cast"column all already exist (the exact bricking precondition).upstatements 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.