Defining Migrations
Pass amigrations array to startApp(). Each migration has:
version- Unique numeric versiondescription- Human-readable summaryhandler- Async function that performs the task
migrations/ directory,
then wire up versions and descriptions in the index file:
src/server/migrations/backfill-todo-status.ts
src/server/migrations/index.ts
src/server/app.ts
How Migrations Run
On application startup, Modelence will:- Acquire a distributed
migrationslock. If another instance already owns it, this instance skips running migrations. - Read existing migration versions from the
_modelenceMigrationscollection. - Run only pending migration versions from your
migrationsarray. - Write a record to
_modelenceMigrationswith:versionstatus(completedorfailed)descriptionoutput(handler result or error message)appliedAt
- Release the lock.
Migrations run in the order they appear in your
migrations array, so keep
that array intentionally ordered and use unique versions.Store indexes run before migrations with this startup behavior:
- Stores using
indexCreationMode: 'blocking'are awaited before migrations begin. Use this for small collections with critical index dependencies (e.g. unique indexes that a migration relies on). - Stores using
indexCreationMode: 'background'may still be creating indexes while migrations run. Prefer this for large collections to avoid blocking app startup.
Migrations and Cron Jobs
Recommended approach:- Make migration handlers idempotent (safe to run once or be retried manually).
- Use conditional updates (for example, update only documents missing the new field).
- Keep cron handlers compatible with both pre-migration and post-migration data during rollout windows.
- If a cron job strictly depends on a migration, add an explicit readiness guard in the cron handler.
Failure Behavior
Failed migrations are recorded withstatus: 'failed'. Since version tracking
is version-based, a failed version is still considered already seen on future
starts.
If you need to rerun logic, create a new migration version (recommended) or
manually clean up the migration record before restarting.