node-db-migration is a focused Node.js library designed for managing database schema evolution through simple SQL-based migration scripts. Currently at version 1.4.0, it offers robust support for popular relational databases including SQLite3, MySQL (or its modern fork `mysql2`), and PostgreSQL. Its core philosophy emphasizes using bare SQL files, allowing developers to maintain direct control over their database schemas without being tied to an ORM's migration DSL. The package operates by maintaining a dedicated `migrations` table within the database to track which scripts have been applied. It scans a specified directory for `.sql` files, enforcing a strict `YYYYMMDDHHmm-name.sql` naming convention to ensure chronological execution. Key features include sequential script execution, robust tracking of successful and failed migrations, and the ability to prevent further migrations upon failure until manual intervention. This helps ensure data consistency and provides a clear audit trail of schema changes. While its release cadence isn't rapid, it offers a stable and reliable solution for teams preferring a 'SQL-first' approach to database version control.
npm install node-db-migrationVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up and run database migrations using `node-db-migration` with a PostgreSQL database. It connects to a database, initializes the `CommandsRunner` with a `PsqlDriver` and a directory for SQL migration scripts, then executes the `migrate` command to apply pending changes.
Ensure all `.sql` migration files in the `directoryWithScripts` follow the `date-name.sql` pattern, where the date is in `YYYYMMDDHHmm` format.
Use the `resolve` command (`migrations.run('resolve')`) after fixing the SQL error, or manually update the `migrations` table to clear the failed status for the specific script, then rerun the `migrate` command.When creating your MySQL connection, add `{ multipleStatements: true }` to the connection options: `mysql.createConnection({ ..., multipleStatements: true });`Install the appropriate database client: `npm install pg`, `npm install mysql` (or `mysql2`), or `npm install sqlite3`.
Install the 'pg' package: `npm install pg`
Carefully review the SQL code in the indicated migration file for syntax errors specific to your database (e.g., MySQL, PostgreSQL, SQLite).
Execute the `init` command to create the migration tracking table: `migrations.run('init');`