Migration

TypeExperimental

Signature

type Migration<TTxContext, TMode> = 
  | TransactionalMigration<TTxContext, TMode>
  | {
  down: MigrationScript<TMode>;
  name: string;
  type: "non-transactional";
  up: MigrationScript<TMode>;
};

packages-internal/typed-sql/src/migrator.ts:49

One migration. Migrations run in the order of their list, and name identifies a migration in the history table, so it must be unique and must not change once the migration has run.

up applies the migration and down reverts it when migrateTo targets an earlier migration. A migration that cannot be reverted throws from its down: the revert stops there, and the migration stays recorded.

  • transactional (the default): up and down receive the migration's transaction context and run inside that transaction, together with the history change, so your query builder works on the migration's connection.
  • non-transactional: up and down run outside a transaction with their own database access (e.g. CREATE INDEX CONCURRENTLY, or a backfill in batches). Call assertLockHeld between steps of long work: it throws once the migration lease is lost. The history changes after the function resolves, so it must be safe to run again.

Type Parameters

  • TTxContext
    The state provider's transaction context.
  • TMode
    extends Mode
    default"async"
    Whether up and down return promises or run synchronously.

Used in guides

esc

Search guides, integrations and every exported symbol.

↑ ↓ to navigate↵ to open