DB Migrator

2.4.0 · active · verified Wed Apr 22

db-migrator is a complete and easy-to-use database migration tool for Node.js projects, offering robust support for both PostgreSQL and MySQL databases. The current stable version is 2.4.0, and based on its active GitHub repository, it appears to maintain an ongoing, though not explicitly defined, release cadence. A key differentiator is its comprehensive feature set, including automatic migration from scratch to the latest version, granular step-by-step forward and backward migration capabilities, and the ability to migrate to a specific database version. It supports deep searching for migration scripts within subfolders and ensures data integrity through transactional "all or nothing" execution, where failures result in a full rollback. This tool builds upon its `pg-migrator` predecessor by introducing timestamp-based version IDs, storing execution times, allowing descriptive migration file names, favoring `npm` scripts for execution, and crucially, adding support for MySQL. It mandates Node.js v7.6.0 or higher due to its reliance on `async/await` syntax in its codebase.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to integrate db-migrator into your project using npm scripts, configure your database connection via .npmrc, and use the primary CLI commands for creating, migrating, rolling back, and checking migration status.

{
  "scripts": {
    "db-migrate": "db-migrate",
    "db-rollback": "db-rollback",
    "db-create": "db-create",
    "db-status": "db-status"
  }
}

// .npmrc configuration for database connection
// For PostgreSQL:
db_migrator_db_url=postgresql://mydatabase@localhost?ssl=false
// For MySQL:
db_migrator_db_url=mysql://user:pass@host/db

// Create a directory for your migration files (default is 'migrations')
// mkdir migrations

// Example usage via npm scripts:
// To generate new migration files:
// npm run db-create "add_users_table"

// To apply all pending migrations:
// npm run db-migrate

// To rollback the most recent migration:
// npm run db-rollback

// To view the current migration status:
// npm run db-status

view raw JSON →