Skip to content
VaelJS Blog

vael:db/migrate

Deterministic SQL migration loader, dry-run planner, multi-driver runner, and reconciliation API for vael:db/migrate.

Available in the vjs (vaeljs) runtime only; absent in vjs-lite. Supports paired NNNN_name.up.sql / NNNN_name.down.sql files and single-file NNNN_name.sql migrations (-- migrate:up / -- migrate:down), dialect identifier bindings, read-only status() / plan(), safe --step / --to prefix planning, and MySQL/MariaDB dirty reconciliation.

import ... from "vael:db/migrate"
Since v0.0.0-alpha.2 vjs (V8)
Cargo feature: vael-db (SQLite enabled by default in vaeljs-core; opt-in network drivers vael-db-postgres, vael-db-mysql, vael-db-mariadb, vael-db-mssql, vael-db-all; absent in vaeljs-lite-core)
import { openSqlite } from "vael:db";
import { plan, status, up } from "vael:db/migrate";
await using db = await openSqlite(":memory:");
const before = await status(db, "./migrations");
if (before.pending.length > 0) {
const dryRun = await plan(db, "./migrations");
console.log("Steps:", dryRun.steps.length);
await up(db, "./migrations");
}
API Symbols
Interfaces (19)
MigrationDatabasevjs onlyv0.0.0-alpha.2
Narrow structural database handle satisfied by any Database<DB> regardless of schema generic parameter DB.
LoadedMigrationvjs onlyv0.0.0-alpha.2
Validated migration definition returned by loadMigrations().
LoadMigrationsOptionsvjs onlyv0.0.0-alpha.2
Options for loadMigrations().
MigrateOptionsvjs onlyv0.0.0-alpha.2
Common ledger and binding options shared by migration operations.
MigratePlanOptionsvjs onlyv0.0.0-alpha.2
Options for dry-run plan().
MigrateUpOptionsvjs onlyv0.0.0-alpha.2
Options for up().
MigrateDownOptionsvjs onlyv0.0.0-alpha.2
Options for down().
ReconcileOptionsvjs onlyv0.0.0-alpha.2
Options for reconcile() on MySQL/MariaDB dirty ledgers.
FlattenOptionsvjs onlyv0.0.0-alpha.2
Options for flatten() on SQLite and PostgreSQL.
AppliedMigrationInfovjs onlyv0.0.0-alpha.2
Applied migration entry reported in MigrationStatus.
PendingMigrationInfovjs onlyv0.0.0-alpha.2
Pending migration entry reported in MigrationStatus.
MigrationDriftInfovjs onlyv0.0.0-alpha.2
Redacted drift summary reported by status().
MigrationDirtyInfovjs onlyv0.0.0-alpha.2
MySQL/MariaDB dirty-ledger marker surfaced by status().
MigrationStatusvjs onlyv0.0.0-alpha.2
Result of read-only status().
PlannedMigrationStepvjs onlyv0.0.0-alpha.2
One planned step in MigrationPlan.
MigrationPlanvjs onlyv0.0.0-alpha.2
Result of read-only dry-run plan().
MigrationSummaryvjs onlyv0.0.0-alpha.2
Summary of one applied or reverted migration.
MigrationApplyResultvjs onlyv0.0.0-alpha.2
Result of up() or down().
MigrationVerifyResultvjs onlyv0.0.0-alpha.2
Result of verify().
functionSince v0.0.0-alpha.2vjs only
export function loadMigrations(
  dir: string,
  opts?: LoadMigrationsOptions,
): Promise<readonly LoadedMigration[]>;

Load and validate SQL migrations from a local directory.

Parameters
NameTypeDescription
dirstringPath to the migrations directory.
opts optionalLoadMigrationsOptionsOptional binding, baseline, and dialect options.
Returns
Promise<readonly LoadedMigration[]> — Frozen array of validated LoadedMigration objects sorted by version.
Exceptions
  • DbError — ("MIGRATION_INVALID", "IO", "INVALID_PARAM") on invalid files or bounds.
functionSince v0.0.0-alpha.2vjs only
export function status(
  target: MigrationTarget,
  migrations: string | readonly LoadedMigration[],
  opts?: MigrateOptions,
): Promise<MigrationStatus>;

Inspect applied, pending, drift, and dirty state without creating or modifying ledger tables.

Parameters
NameTypeDescription
targetMigrationTargetOpen Database handle, DbConfig object, or DSN string.
migrationsstring | readonly LoadedMigration[]Directory path or pre-loaded LoadedMigration[].
opts optionalMigrateOptionsOptional ledger, schema, module, and binding configuration.
Returns
Promise<MigrationStatus> — Read-only migration status snapshot.
functionSince v0.0.0-alpha.2vjs only
export function plan(
  target: MigrationTarget,
  migrations: string | readonly LoadedMigration[],
  opts?: MigratePlanOptions,
): Promise<MigrationPlan>;

Validate history and render planned up or down steps without executing writes.

Parameters
NameTypeDescription
targetMigrationTargetOpen Database handle, DbConfig object, or DSN string.
migrationsstring | readonly LoadedMigration[]Directory path or pre-loaded LoadedMigration[].
opts optionalMigratePlanOptionsOptional direction ("up" | "down"), step, to, and ledger options.
Returns
Promise<MigrationPlan> — Ordered dry-run plan.
functionSince v0.0.0-alpha.2vjs only
export function up(
  target: MigrationTarget,
  migrations: string | readonly LoadedMigration[],
  opts?: MigrateUpOptions,
): Promise<MigrationApplyResult>;

Apply pending up migrations (all, --step, or --to) atomically where supported.

Parameters
NameTypeDescription
targetMigrationTargetOpen Database handle, DbConfig object, or DSN string.
migrationsstring | readonly LoadedMigration[]Directory path or pre-loaded LoadedMigration[].
opts optionalMigrateUpOptionsOptional step, to, and ledger configuration.
Returns
Promise<MigrationApplyResult> — Applied migration summary.
functionSince v0.0.0-alpha.2vjs only
export function down(
  target: MigrationTarget,
  migrations: string | readonly LoadedMigration[],
  opts?: MigrateDownOptions,
): Promise<MigrationApplyResult>;

Revert applied migrations (defaults to step: 1; never rolls back all implicitly).

Parameters
NameTypeDescription
targetMigrationTargetOpen Database handle, DbConfig object, or DSN string.
migrationsstring | readonly LoadedMigration[]Directory path or pre-loaded LoadedMigration[].
opts optionalMigrateDownOptionsOptional step, to, and ledger configuration.
Returns
Promise<MigrationApplyResult> — Reverted migration summary.
functionSince v0.0.0-alpha.2vjs only
export function verify(
  migrations: string | readonly LoadedMigration[],
  target?: MigrationTarget | null,
  opts?: MigrateOptions,
): Promise<MigrationVerifyResult>;

Verify local migration files and optionally verify consistency with a target database ledger.

Parameters
NameTypeDescription
migrationsstring | readonly LoadedMigration[]Directory path or pre-loaded LoadedMigration[].
target optionalMigrationTarget | nullOptional Database handle, DbConfig object, or DSN string.
opts optionalMigrateOptionsOptional ledger, schema, module, and binding configuration.
Returns
Promise<MigrationVerifyResult> — Verification summary; throws DbError on drift, dirty state, or invalid files.
functionSince v0.0.0-alpha.2vjs only
export function reconcile(target: MigrationTarget, opts: ReconcileOptions): Promise<void>;

Clear a dirty migration marker on MySQL or MariaDB after manual operator inspection.

Parameters
NameTypeDescription
targetMigrationTargetOpen Database handle, DbConfig object, or DSN string.
optsReconcileOptionsMigration set, operator-verified outcome, and ledger options.
Returns
Promise<void>
Exceptions
  • DbError — ("UNSUPPORTED") on SQLite, PostgreSQL, and SQL Server.
functionSince v0.0.0-alpha.2vjs only
export function flatten(target: MigrationTarget, manifest: FlattenOptions): Promise<void>;

Replace an exact applied legacy history with an irreversible baseline on SQLite or PostgreSQL.

Parameters
NameTypeDescription
targetMigrationTargetOpen Database handle, DbConfig object, or DSN string.
manifestFlattenOptionsLegacy migrations, baseline file or definition, and ledger options.
Returns
Promise<void>
Exceptions
  • DbError — ("UNSUPPORTED") on MySQL, MariaDB, and SQL Server.
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationDatabase {
  readonly dialect: import("vael:db").DbDialect;
  close(): Promise<void>;
}

Narrow structural database handle satisfied by any Database<DB> regardless of schema generic parameter DB.

Properties
PropertyTypeDescriptionSince
dialect readonlyimport("vael:db").DbDialectNative SQL dialect of this handle.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
close(): Promise<void>;

Close the database pool handle.

Returns
Promise<void>
interfaceSince v0.0.0-alpha.2vjs only
export interface LoadedMigration {
  readonly version: number;
  readonly name: string;
  readonly checksum: string;
  readonly hasDown: boolean;
  readonly baseline: boolean;
  readonly format: MigrationFormat;
  readonly upSql: string;
  readonly downSql: string | null;
  readonly boundDriver: import("vael:db").DbDialect | null;
}

Validated migration definition returned by loadMigrations().

Properties
PropertyTypeDescriptionSince
version readonlynumberPositive integer version (>= 1).v0.0.0-alpha.2
name readonlystringMigration name suffix from the filename.v0.0.0-alpha.2
checksum readonlystringSHA-256 checksum domain-tagged by neotales-migration-v1.v0.0.0-alpha.2
hasDown readonlybooleanWhether a down migration script is present.v0.0.0-alpha.2
baseline readonlybooleanWhether this migration is marked as an irreversible flatten baseline.v0.0.0-alpha.2
format readonlyMigrationFormatAuthoring format ("paired" or "single").v0.0.0-alpha.2
upSql readonlystringUp SQL text.v0.0.0-alpha.2
downSql readonlystring | nullDown SQL text, or null when irreversible.v0.0.0-alpha.2
boundDriver readonlyimport("vael:db").DbDialect | nullDialect for which identifier bindings have already been applied, or null.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface LoadMigrationsOptions {
  readonly bindings?: Readonly<Record<string, string>>;
  readonly baselineVersion?: number;
  readonly dialect?: import("vael:db").DbDialect;
}

Options for loadMigrations().

Properties
PropertyTypeDescriptionSince
bindings readonly optionalReadonly<Record<string, string>>Identifier bindings ({{key}} -> lowercase SQL identifier).v0.0.0-alpha.2
baselineVersion readonly optionalnumberOptional version number to mark as an irreversible baseline.v0.0.0-alpha.2
dialect readonly optionalimport("vael:db").DbDialectOptional target dialect to eagerly apply bindings and compute dialect checksums.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrateOptions {
  readonly table?: string;
  readonly schema?: string;
  readonly module?: string;
  readonly lockTimeoutMs?: number;
  readonly managedObjects?: readonly string[];
  readonly bindings?: Readonly<Record<string, string>>;
  readonly baselineVersion?: number;
}

Common ledger and binding options shared by migration operations.

Properties
PropertyTypeDescriptionSince
table readonly optionalstringLedger table name ([a-z_][a-z0-9_]*, defaults to "_vael_migrations").v0.0.0-alpha.2
schema readonly optionalstringTarget schema for PostgreSQL ("public") or SQL Server ("dbo").v0.0.0-alpha.2
module readonly optionalstringLogical module identity in multi-module ledgers (defaults to "app").v0.0.0-alpha.2
lockTimeoutMs readonly optionalnumberAdvisory/application lock wait timeout in milliseconds (defaults to 30000).v0.0.0-alpha.2
managedObjects readonly optionalreadonly string[]Application objects rejected if already present on unregistered first run.v0.0.0-alpha.2
bindings readonly optionalReadonly<Record<string, string>>Identifier placeholder bindings ({{key}} -> lowercase SQL identifier).v0.0.0-alpha.2
baselineVersion readonly optionalnumberOptional version number to mark as an irreversible baseline.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigratePlanOptions extends MigrateOptions {
  readonly direction?: MigrationDirection;
  readonly step?: number;
  readonly to?: number;
}

Options for dry-run plan().

Properties
PropertyTypeDescriptionSince
direction readonly optionalMigrationDirectionDirection to plan ("up" or "down", defaults to "up").v0.0.0-alpha.2
step readonly optionalnumberMaximum number of migrations to include (>= 1).v0.0.0-alpha.2
to readonly optionalnumberTarget migration version to plan up or down to.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrateUpOptions extends MigrateOptions {
  readonly step?: number;
  readonly to?: number;
}

Options for up().

Properties
PropertyTypeDescriptionSince
step readonly optionalnumberMaximum number of pending migrations to apply (>= 1).v0.0.0-alpha.2
to readonly optionalnumberInclusive target migration version to apply up to.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrateDownOptions extends MigrateOptions {
  readonly step?: number;
  readonly to?: number;
}

Options for down().

Properties
PropertyTypeDescriptionSince
step readonly optionalnumberNumber of applied migrations to revert (>= 1, defaults to 1).v0.0.0-alpha.2
to readonly optionalnumberTarget version to revert down to (0 reverts all non-baseline migrations).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface ReconcileOptions extends MigrateOptions {
  readonly migrations: string | readonly LoadedMigration[];
  readonly outcome: "applied" | "reverted" | "notApplied";
}

Options for reconcile() on MySQL/MariaDB dirty ledgers.

Properties
PropertyTypeDescriptionSince
migrations readonlystring | readonly LoadedMigration[]Local migration directory path or pre-loaded migrations.v0.0.0-alpha.2
outcome readonly"applied" | "reverted" | "notApplied"Operator-verified outcome of the interrupted script.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface FlattenOptions extends MigrateOptions {
  readonly migrations?: string | readonly LoadedMigration[];
  readonly legacy?: string | readonly LoadedMigration[];
  readonly legacyMaxVersion?: number;
  readonly baseline: string | LoadedMigration;
}

Options for flatten() on SQLite and PostgreSQL.

Properties
PropertyTypeDescriptionSince
migrations readonly optionalstring | readonly LoadedMigration[]Legacy migration directory path or pre-loaded migrations.v0.0.0-alpha.2
legacy readonly optionalstring | readonly LoadedMigration[]Alias for migrations.v0.0.0-alpha.2
legacyMaxVersion readonly optionalnumberOptional inclusive max version when migrations also contains newer files.v0.0.0-alpha.2
baseline readonlystring | LoadedMigrationBaseline SQL file path or pre-loaded baseline migration.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface AppliedMigrationInfo {
  readonly version: number;
  readonly name: string | null;
  readonly checksum: string;
}

Applied migration entry reported in MigrationStatus.

Properties
PropertyTypeDescriptionSince
version readonlynumberApplied migration version.v0.0.0-alpha.2
name readonlystring | nullLocal migration name if present locally, else null.v0.0.0-alpha.2
checksum readonlystringRecorded SHA-256 checksum.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface PendingMigrationInfo {
  readonly version: number;
  readonly name: string;
  readonly checksum: string;
  readonly hasDown: boolean;
  readonly baseline: boolean;
}

Pending migration entry reported in MigrationStatus.

Properties
PropertyTypeDescriptionSince
version readonlynumberPending migration version.v0.0.0-alpha.2
name readonlystringMigration name.v0.0.0-alpha.2
checksum readonlystringExpected SHA-256 checksum.v0.0.0-alpha.2
hasDown readonlybooleanWhether down SQL is present.v0.0.0-alpha.2
baseline readonlybooleanWhether marked as a flatten baseline.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationDriftInfo {
  readonly kind: string;
  readonly message: string;
}

Redacted drift summary reported by status().

Properties
PropertyTypeDescriptionSince
kind readonlystringMachine-readable drift kind ("modified", "history_drift", etc.).v0.0.0-alpha.2
message readonlystringRedacted drift description.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationDirtyInfo {
  readonly direction: MigrationDirection;
  readonly stage: string;
  readonly version: number;
  readonly checksum: string;
}

MySQL/MariaDB dirty-ledger marker surfaced by status().

Properties
PropertyTypeDescriptionSince
direction readonlyMigrationDirectionDirection of the interrupted operation.v0.0.0-alpha.2
stage readonlystringLast persisted stage ("started", "script_done", or "script_failed").v0.0.0-alpha.2
version readonlynumberMigration version being processed when interrupted.v0.0.0-alpha.2
checksum readonlystringExpected SHA-256 checksum.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationStatus {
  readonly driver: import("vael:db").DbDialect;
  readonly module: string;
  readonly table: string;
  readonly initialized: boolean;
  readonly healthy: boolean;
  readonly currentVersion: number | null;
  readonly bindingFingerprint: string;
  readonly applied: readonly AppliedMigrationInfo[];
  readonly pending: readonly PendingMigrationInfo[];
  readonly drift: MigrationDriftInfo | null;
  readonly dirty: MigrationDirtyInfo | null;
}

Result of read-only status().

Properties
PropertyTypeDescriptionSince
driver readonlyimport("vael:db").DbDialectTarget database dialect.v0.0.0-alpha.2
module readonlystringModule identity.v0.0.0-alpha.2
table readonlystringLedger table name.v0.0.0-alpha.2
initialized readonlybooleanWhether ledger tables exist and the module is registered.v0.0.0-alpha.2
healthy readonlybooleantrue when there is neither drift nor a dirty marker.v0.0.0-alpha.2
currentVersion readonlynumber | nullHighest applied migration version, or null.v0.0.0-alpha.2
bindingFingerprint readonlystringBinding fingerprint for the target dialect and bindings.v0.0.0-alpha.2
applied readonlyreadonly AppliedMigrationInfo[]Applied migrations recorded in the ledger.v0.0.0-alpha.2
pending readonlyreadonly PendingMigrationInfo[]Local migrations not yet applied.v0.0.0-alpha.2
drift readonlyMigrationDriftInfo | nullDrift information if local definitions disagree with the ledger, else null.v0.0.0-alpha.2
dirty readonlyMigrationDirtyInfo | nullDirty marker on MySQL/MariaDB if reconciliation is required, else null.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface PlannedMigrationStep {
  readonly version: number;
  readonly name: string;
  readonly checksum: string;
  readonly sql: string;
  readonly batches?: readonly string[];
}

One planned step in MigrationPlan.

Properties
PropertyTypeDescriptionSince
version readonlynumberMigration version.v0.0.0-alpha.2
name readonlystringMigration name.v0.0.0-alpha.2
checksum readonlystringMigration SHA-256 checksum.v0.0.0-alpha.2
sql readonlystringDialect-bound SQL script for this step.v0.0.0-alpha.2
batches readonly optionalreadonly string[]For SQL Server, pre-split GO batches.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationPlan {
  readonly driver: import("vael:db").DbDialect;
  readonly direction: MigrationDirection;
  readonly currentVersion: number | null;
  readonly targetVersion: number | null;
  readonly steps: readonly PlannedMigrationStep[];
}

Result of read-only dry-run plan().

Properties
PropertyTypeDescriptionSince
driver readonlyimport("vael:db").DbDialectTarget database dialect.v0.0.0-alpha.2
direction readonlyMigrationDirectionPlanned direction.v0.0.0-alpha.2
currentVersion readonlynumber | nullCurrent applied head version before the plan, or null.v0.0.0-alpha.2
targetVersion readonlynumber | nullProjected head version after executing the plan, or null.v0.0.0-alpha.2
steps readonlyreadonly PlannedMigrationStep[]Ordered steps that would execute.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationSummary {
  readonly version: number;
  readonly name: string;
  readonly checksum: string;
}

Summary of one applied or reverted migration.

Properties
PropertyTypeDescriptionSince
version readonlynumberMigration version.v0.0.0-alpha.2
name readonlystringMigration name.v0.0.0-alpha.2
checksum readonlystringMigration SHA-256 checksum.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationApplyResult {
  readonly driver: import("vael:db").DbDialect;
  readonly direction: MigrationDirection;
  readonly applied: readonly MigrationSummary[];
  readonly currentVersion: number | null;
  readonly appliedCount: number;
}

Result of up() or down().

Properties
PropertyTypeDescriptionSince
driver readonlyimport("vael:db").DbDialectTarget database dialect.v0.0.0-alpha.2
direction readonlyMigrationDirectionApplied direction ("up" or "down").v0.0.0-alpha.2
applied readonlyreadonly MigrationSummary[]Migrations applied (for up) or reverted (for down).v0.0.0-alpha.2
currentVersion readonlynumber | nullCurrent applied head version after the operation, or null.v0.0.0-alpha.2
appliedCount readonlynumberTotal number of applied migrations remaining in the ledger.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface MigrationVerifyResult {
  readonly valid: boolean;
  readonly driver: import("vael:db").DbDialect | null;
  readonly localCount: number;
  readonly appliedCount: number;
  readonly pendingCount: number;
  readonly bindingFingerprint: string;
}

Result of verify().

Properties
PropertyTypeDescriptionSince
valid readonlybooleanAlways true when verification succeeds (throws DbError on failure).v0.0.0-alpha.2
driver readonlyimport("vael:db").DbDialect | nullTarget dialect when verified against a database, or null when offline.v0.0.0-alpha.2
localCount readonlynumberTotal number of local migrations validated.v0.0.0-alpha.2
appliedCount readonlynumberNumber of applied migrations in the target ledger (0 when offline).v0.0.0-alpha.2
pendingCount readonlynumberNumber of pending migrations.v0.0.0-alpha.2
bindingFingerprint readonlystringBinding fingerprint for the validated migration set.v0.0.0-alpha.2
typeSince v0.0.0-alpha.2vjs only
export type MigrationErrorCode =
  | import("vael:db").DbErrorCode
  | "MIGRATION_INVALID"
  | "MIGRATION_DRIFT"
  | "MIGRATION_DIRTY"
  | "MIGRATION_MISSING_DOWN"
  | "MIGRATION_BASELINE"
  | "MIGRATION_UNTRACKED_OBJECTS"
  | "MIGRATION_UNREGISTERED"
  | "MIGRATION_NOT_DIRTY"
  | "MIGRATION_LOCK_TIMEOUT"
  | "UNSUPPORTED"
  | "IO";

Error codes thrown on DbError.code by vael:db/migrate operations.

typeSince v0.0.0-alpha.2vjs only
export type MigrationTarget =
  | MigrationDatabase
  | import("vael:db").DbConfig
  | string;

Database target accepted by vael:db/migrate operations: an open Database handle, a DbConfig object, or a DSN string (sqlite:, postgres://, mysql://, mariadb://, mssql://).

typeSince v0.0.0-alpha.2vjs only
export type MigrationFormat = "paired" | "single";

File authoring format of a loaded migration.

typeSince v0.0.0-alpha.2vjs only
export type MigrationDirection = "up" | "down";

Direction of a migration plan or apply result.

constSince v0.0.0-alpha.2vjs only
export const DbError: typeof import("vael:db").DbError;

Re-exported DbError constructor shared with vael:db.