Skip to content

@iamcalegari/mongoat / runMigrations

Function: runMigrations()

runMigrations(database, config): Promise<void>

Defined in: src/migrate/runner.ts:410

Applies every pending migration found in config.dir, in ascending lexicographic order, tracking applied state in config.collection (default _migrations). Idempotent — a migration whose version is already recorded as applied is never re-run. Before applying anything, re-verifies the checksum of every already-applied migration still on disk and refuses to proceed on any drift (MIGRATION_CHECKSUM_MISMATCH).

Acquires an exclusive run lock before reading any migration state and releases it once the run finishes (successfully, on failure, or on config.signal abort) — this guarantees two concurrent runs against the same control collection can never act on the same snapshot of pending migrations.

Each migration's up(ctx) runs with a ClientSession bound to an active MongoDB transaction (requires a replica set/mongos — see assertReplicaSetOrThrow); ctx.schema.* calls never enlist that session, so DDL (collMod/createIndex) is never part of the transaction. A failing migration is recorded { status: 'failed' } and the run stops — no automatic rollback of any DDL already applied.

The topology precondition (assertReplicaSetOrThrow) runs ONCE here, before the apply loop and OUTSIDE any failure-recording try — a REPLICA_SET_REQUIRED failure propagates to the caller UNWRAPPED (own .code, actionable message) and never persists a bogus failed record for a migration that never ran.

Parameters

database

Database

A connected Database instance.

config

MigrateConfig

Migration directory, control collection name, the allowNoTransaction opt-in, an optional lockTtlMs, and an optional graceful-stop signal.

Returns

Promise<void>