Registry / database / db-migrate

db-migrate

JSON →
library0.11.14jsnpmunverified

db-migrate is a robust database migration framework for Node.js, designed to manage schema changes across various SQL database systems. Its current stable version is 0.11.14, released in late 2019, with a `v1.0.0-beta.0` also from 2019. The project is currently in a maintenance phase, with ongoing commits to the GitHub repository but no new stable releases for several years. It supports a wide array of databases including PostgreSQL, MySQL, SQLite, and MSSQL, requiring separate driver packages for each. A key differentiator is its dual interface: a powerful command-line tool for everyday use and a comprehensive programmatic API for deeper integration into application deployment and testing workflows. It ensures transactional integrity for migrations and offers clear 'up' and 'down' scripts for reliable schema evolution and rollback capabilities.

npm install db-migrate
INSTALL
IMPORT
SIG · DB-MIGRATE
D
db-migrate
databasejavascriptv0.11.14
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DBMigrate
const DBMigrate = require('db-migrate');
import DBMigrate from 'db-migrate';
db-migrate is a CommonJS module. Direct ES module `import` syntax is not officially supported and might require transpilation or dynamic `import()` in pure ESM environments.
getInstance (default)
const dbmigrate = DBMigrate.getInstance(true);
const dbmigrate = new DBMigrate();
The primary way to interact with db-migrate programmatically is by obtaining an instance through `getInstance()`. Passing `true` as the first argument loads a default instance with default options, typically reading `database.json` from `process.cwd()`.
getInstance (custom options)
const dbmigrate = DBMigrate.getInstance(false, { cwd: __dirname, env: 'production' });
const dbmigrate = DBMigrate.getInstance({ cwd: __dirname });
To create a custom db-migrate instance with specific options (e.g., custom current working directory `cwd` or environment `env`), pass `false` as the first argument to `getInstance()`, followed by an options object.

This quickstart demonstrates the programmatic use of db-migrate to define and run both 'up' and 'down' migrations against an in-memory SQLite database, cleaning up all generated files afterward.

import DBMigrate from 'db-migrate'; import path from 'path'; import fs from 'fs'; async function runExampleMigrations() { const migrationsDirPath = path.join(__dirname, 'migrations'); // Ensure migrations directory exists for db-migrate to scan if (!fs.existsSync(migrationsDirPath)) { fs.mkdirSync(migrationsDirPath); } // Create a simple migration file (up and down) const migrationName = `create-test-table-${Date.now()}`; const migrationFilePath = path.join(migrationsDirPath, `${migrationName}.js`); const migrationContent = ` 'use strict'; var dbm; var type; var seed; exports.setup = function(options) { dbm = options.dbmigrate; type = options.type; seed = options.seed; }; exports.up = function(db) { console.log('Running UP migration: ${migrationName}'); return db.createTable('test_table', { id: { type: 'int', primaryKey: true, autoIncrement: true }, value: { type: 'string', length: 255 } }); }; exports.down = function(db) { console.log('Running DOWN migration: ${migrationName}'); return db.dropTable('test_table'); }; exports._meta = { "version": 1 }; `; fs.writeFileSync(migrationFilePath, migrationContent); console.log(`Generated migration file: ${migrationName}.js`); // Create a minimal database.json for an in-memory SQLite database const configPath = path.join(__dirname, 'database.json'); const dbConfig = { "dev": { "driver": "sqlite3", "filename": ":memory:", // Use in-memory SQLite for easy testing "host": "localhost", // Required by some db-migrate internals even for sqlite3 "database": "testdb" // Required by some db-migrate internals } }; fs.writeFileSync(configPath, JSON.stringify(dbConfig, null, 2)); console.log('Generated database.json for in-memory SQLite.'); let dbmigrate; try { // Initialize db-migrate with options dbmigrate = DBMigrate.getInstance(true, { cwd: __dirname, // Important for db-migrate to find config and migrations env: 'dev' }); console.log('\n--- Running UP migrations ---'); await dbmigrate.up(); console.log('UP migrations complete.'); console.log('\n--- Running DOWN migrations ---'); await dbmigrate.down(); console.log('DOWN migrations complete.'); } catch (error) { console.error('Migration failed:', error); process.exit(1); } finally { // Cleanup generated files if (fs.existsSync(migrationFilePath)) { fs.unlinkSync(migrationFilePath); } if (fs.existsSync(configPath)) { fs.unlinkSync(configPath); } if (fs.existsSync(migrationsDirPath)) { fs.rmdirSync(migrationsDirPath, { recursive: true }); } } } runExampleMigrations();
db-migrate --version
Debug
Known issues
gotchadb-migrate is primarily a CommonJS module. While it can be used in ESM projects, direct `import DBMigrate from 'db-migrate'` might require build tool configuration or dynamic `import()` for seamless integration in a pure ESM environment.
fix
For CommonJS, use `const DBMigrate = require('db-migrate');`. For ESM, consider `import('./path/to/db-migrate.js').then(mod => mod.default)` or ensure your bundler handles CommonJS interoperability.
affects: >=0.11.0
gotchaEach database type (PostgreSQL, MySQL, SQLite, MSSQL) requires its own `db-migrate-*` driver package (e.g., `db-migrate-pg`, `db-migrate-mysql`). Forgetting to install the correct driver for your configured database will lead to runtime errors.
fix
Install the appropriate driver: `npm install db-migrate-<driver-name>`. For example, `npm install db-migrate-pg` for PostgreSQL.
affects: all
gotchaConfiguration for db-migrate is typically done via a `database.json` file. Ensure this file is correctly located in your project's root or specified via the `cwd` option when using the programmatic API, otherwise, db-migrate might fail to initialize or connect to the database.
fix
Create a `database.json` in your project root or pass `{ cwd: path.resolve(__dirname, 'path/to/config') }` to `DBMigrate.getInstance()`.
affects: all
deprecatedThe `v1.0.0-beta.0` release from 2019 was a beta and the project has not released a stable v1.0.0 since. While active, the core functionality resides in the 0.11.x branch, and future breaking changes are possible if a stable v1 is ever released.
fix
Be aware that using `db-migrate` with current Node.js versions might uncover compatibility issues not covered by older test suites. Monitor the GitHub repository for updates and test thoroughly.
affects: >=0.11.0
Errors
Common errors & fixes
Error: Cannot find module 'db-migrate-<driver>'
The required database driver package (e.g., 'db-migrate-pg' for PostgreSQL) is not installed or incorrectly named.
fix
Install the missing driver: `npm install db-migrate-<driver>` (e.g., `npm install db-migrate-pg`). Ensure the driver name in `database.json` matches the installed package suffix.
Error: No database config found!
db-migrate could not find a `database.json` file in the current working directory or the directory specified by the `cwd` option.
fix
Create a `database.json` file in your project root or ensure the `cwd` option for `DBMigrate.getInstance()` points to the correct directory where `database.json` resides.
Error: db-migrate connection error. Please check your database credentials or connection string.
db-migrate failed to establish a connection to the database. This is typically due to incorrect credentials, host, port, database name in `database.json`, or the database server not running/being inaccessible.
fix
Verify all connection details in your `database.json` for the selected environment. Ensure the database server is running and accessible from the machine running db-migrate.
TypeError: db.createTable is not a function (or similar for other DBMigrate API methods)
This error usually indicates an issue with how the migration file `exports.up` or `exports.down` is structured, specifically if the `db` object passed to these functions is not being correctly used.
fix
Ensure your migration files correctly define `exports.setup = function(options) { dbm = options.dbmigrate; ... };` and that `exports.up = function(db) { ... };` and `exports.down = function(db) { ... };` use the `db` object passed to them, which contains the API methods like `createTable`, `addColumn`, etc.
Upgrade
Version history
0.11.14latest on npm
Audit
Dependencies
db-migrate-pgoptionalRequired for PostgreSQL database support.
db-migrate-mysqloptionalRequired for MySQL database support.
db-migrate-sqlite3optionalRequired for SQLite database support.
db-migrate-mssqloptionalRequired for Microsoft SQL Server database support.
Agent activity
10 hits · last 30 days
node
10
Resources
db-migrate — npm install db-migrate · libregistry