Overview
TheMigrator class provides methods for managing database migrations. It handles migration execution, rollback, and tracking of migration state.
Constructor
Example
Configuration
MigratorProps
The Kysely database instance to run migrations against.
A migration provider that supplies migrations to execute. Use
FileMigrationProvider to load migrations from a folder.The name of the internal migration table.Warning: If you specify this, you must ALWAYS use the same value. Changing it after migrations have run will cause Kysely to create a new empty migration table and attempt to re-run migrations.
The name of the internal migration lock table.Warning: If you specify this, you must ALWAYS use the same value from the beginning of the project.
The schema of the internal migration tables. Defaults to the default schema on dialects that support schemas.Only works on PostgreSQL and MSSQL.Warning: If you specify this, you must ALWAYS use the same value. Changing it will create new migration tables in the new schema.
Enforces whether or not migrations must be run in alpha-numeric order.
- When
false(default): migrations must be run in exact alpha-numeric order, checked against already-run migrations in the database. This is the safest option. - When
true: migrations are still run in alpha-numeric order, but the order is not validated against the database. Kysely will simply run all pending migrations in alpha-numeric order.
A function that compares migration names when sorting migrations in ascending order.Default is
name0.localeCompare(name1).When
true, don’t run migrations in transactions even if the dialect supports transactional DDL.Useful when some migrations include queries that would fail in a transaction.Methods
getMigrations()
MigrationInfo object for each migration. The returned array is sorted by migration name.
Name of the migration.
The actual migration object.
When the migration was executed.
undefined if the migration hasn’t been executed yet.migrateToLatest()
MigrationResultSet instance and never throws. MigrationResultSet.error holds the error if something went wrong. MigrationResultSet.results contains information about which migrations were executed and which failed.
This method goes through all possible migrations provided by the provider and runs the ones whose names come alphabetically after the last migration that has been run.
Example
migrateTo()
MigrationResultSet instance and never throws.
Parameters
The name of the target migration, or
NO_MIGRATIONS to migrate all the way down.Examples
Migrate to a specific migration:down method of the first migration. To migrate all the way down, use the NO_MIGRATIONS constant:
migrateUp()
MigrationResultSet instance and never throws.
Example
migrateDown()
MigrationResultSet instance and never throws.
Example
Types
MigrationResultSet
All migration methods return this object instead of throwing errors.Defined if something went wrong. An error may have occurred in one of the migrations (check
results for which one) or before Kysely could determine which migrations to execute (in which case results is undefined).MigrationResult for each individual migration that was supposed to be executed.- If all went well, each result’s
statusisSuccess - If a migration failed, the failed migration’s result has
statusofErrorand all subsequent results havestatusofNotExecuted - Can be
undefinedif an error occurred before determining which migrations to execute - Empty array if there were no migrations to execute
MigrationResult
The name of the migration.
The direction in which this migration was executed.
The execution status:
Success: migration was successfully executed (note: may be rolled back if a later migration failed and dialect supports transactional DDL)Error: migration failed (seeMigrationResultSet.errorfor details)NotExecuted: migration was supposed to be executed but wasn’t because an earlier migration failed
Migration
Interface that all migrations must implement.Method to run when migrating up.
Optional method to run when migrating down. If not provided, the migration is skipped when migrating down.
MigrationInfo
Information about a migration returned bygetMigrations().
Name of the migration.
The actual migration object.
When the migration was executed.
undefined if not yet executed.Constants
NO_MIGRATIONS
migrateTo() to migrate all the way down (rolling back all migrations).
DEFAULT_MIGRATION_TABLE
DEFAULT_MIGRATION_LOCK_TABLE
DEFAULT_ALLOW_UNORDERED_MIGRATIONS
allowUnorderedMigrations option.