Returns a MigrationInfo object for each migration.
The returned array is sorted by migration name.
Migrate one step down.
This method returns a 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.
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { FileMigrationProvider, Migrator } from 'kysely'
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
// Path to the folder that contains all your migrations.
migrationFolder: 'some/path/to/migrations',
path,
})
})
await migrator.migrateDown()
Migrate up/down to a specific migration.
This method returns a 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.
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { FileMigrationProvider, Migrator } from 'kysely'
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
// Path to the folder that contains all your migrations.
migrationFolder: 'some/path/to/migrations',
path,
})
})
await migrator.migrateTo('some_migration')
If you specify the name of the first migration, this method migrates
down to the first migration, but doesn't run the down
method of
the first migration. In case you want to migrate all the way down,
you can use a special constant NO_MIGRATIONS
:
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { FileMigrationProvider, Migrator, NO_MIGRATIONS } from 'kysely'
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
// Path to the folder that contains all your migrations.
migrationFolder: 'some/path/to/migrations',
path,
})
})
await migrator.migrateTo(NO_MIGRATIONS)
Runs all migrations that have not yet been run.
This method returns a 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. See the examples below.
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. If the list of executed migrations doesn't match the beginning of the list of possible migrations an error is returned.
import { promises as fs } from 'node:fs'
import path from 'node:path'
import * as Sqlite from 'better-sqlite3'
import { FileMigrationProvider, Migrator } from 'kysely'
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
migrationFolder: 'some/path/to/migrations',
path,
})
})
const { error, results } = await migrator.migrateToLatest()
results?.forEach((it) => {
if (it.status === 'Success') {
console.log(`migration "${it.migrationName}" was executed successfully`)
} else if (it.status === 'Error') {
console.error(`failed to execute migration "${it.migrationName}"`)
}
})
if (error) {
console.error('failed to run `migrateToLatest`')
console.error(error)
}
Migrate one step up.
This method returns a 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.
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { FileMigrationProvider, Migrator } from 'kysely'
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
// Path to the folder that contains all your migrations.
migrationFolder: 'some/path/to/migrations',
path,
})
})
await migrator.migrateUp()
A class for running migrations.
Example
This example uses the FileMigrationProvider that reads migrations files from a single folder. You can easily implement your own MigrationProvider if you want to provide migrations some other way.