knex-migrator is a robust database migration tool built on top of knex.js, specifically designed to handle complex schema changes and initializations. Currently at version 5.3.2, it is known for its stability and is actively used in production by platforms like Ghost CMS. Key features include distinct differentiation between database initialization and subsequent migrations, support for a structured database schema, comprehensive rollback capabilities with auto-rollback on error, transactional migrations, and a migration lock mechanism to prevent concurrent execution. It supports MySQL and SQLite3. The tool emphasizes a strict separation of DDL (Data Definition Language) and DML (Data Manipulation Language) statements within migration scripts to ensure atomic operations and prevent implicit commits, which is a critical differentiator for maintaining data integrity during schema evolution.
npm install knex-migratorVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `knex-migrator` with an SQLite3 database, configure `MigratorConfig.js`, create a basic `init` migration file, and execute the initial migration using the CLI.
Implement replica-aware logic at the application level or use a separate database provisioning tool.
Ensure migrations are run in a single-process, controlled environment, especially when using SQLite.
Separate DDL and DML operations into distinct migration scripts or use transactional blocks carefully for DML within DDL if absolutely necessary and database client supports it without implicit commits.
Manually release the lock by running `knex-migrator rollback --force` (if you are sure about the state) or by directly inspecting and clearing the `migrations_lock` table in the database. Always check the database state first.
Always provide a `down` function that logically reverses the changes made by the `up` function in your migration files.
Run `npx knex-migrator rollback --force` to try and clear the lock and potentially revert partially applied migrations, or manually inspect and clear the `migrations_lock` table.
Verify that the database server is running and reachable from the application's host, and double-check all connection parameters (host, port, user, password, database name) in `MigratorConfig.js`.
Install the necessary database client driver using `npm install <client_name>` or `yarn add <client_name>`.
Ensure `MigratorConfig.js` is present in the project root or specify its path using the `--mgpath` CLI option.