Class: MigrationRunner
Defined in: Data/Migration/MigrationRunner.ts:38
Applies the ordered migration sequence in packages/core/src/sql/migrations/
to a database, exactly once each, and records what it did.
Design points that matter:
-
Idempotent. Every migration is recorded in
schema_migrationand never re-run. Individual statements additionally carry@skip-ifguards, so even a database whose ledger was lost -- or one dragged partway forward by the older hard-coded migrator inelectron/utils/initMainDatabase.ts-- lands in the same place. -
Atomic per migration. Each migration runs inside one transaction. A failure rolls that migration back entirely; migrations that already succeeded stay applied and are not repeated.
-
Foreign keys off, then verified. Dropping a CHECK in SQLite means rebuilding the table, which is only safe with
foreign_keysdisabled.PRAGMA foreign_keysis a no-op inside a transaction, so it is toggled around it, andPRAGMA foreign_key_checkruns inside the transaction before commit -- a violation therefore rolls back rather than persisting.
Constructors
Constructor
new MigrationRunner(
sql,target):MigrationRunner
Defined in: Data/Migration/MigrationRunner.ts:46
Parameters
sql
Database to migrate.
target
"main" | "user"
Which database this connection is -- 'main' or 'user'.
Migrations aimed at the other one are skipped.
Returns
MigrationRunner
Methods
baseline()
baseline(
scripts):MigrationRunResult
Defined in: Data/Migration/MigrationRunner.ts:101
Record every migration as applied without running any of it.
For a database just created from sql/schemas/initial/, which already has
the v2 structure. Replaying history against it would be wasted work at
best. Migrations added after the baseline still run normally on the next
run().
Parameters
scripts
Returns
getApplied()
getApplied():
AppliedMigration[]
Defined in: Data/Migration/MigrationRunner.ts:136
Rows of the ledger, oldest first.
Returns
getPending()
getPending(
scripts):MigrationScript[]
Defined in: Data/Migration/MigrationRunner.ts:151
Migrations targeting this database that run() would apply.
Parameters
scripts
Returns
run()
run(
scripts):MigrationRunResult
Defined in: Data/Migration/MigrationRunner.ts:54
Apply every migration in scripts that targets this database and has not
been applied yet, in version order.