Command-line interface providing comprehensive migration and database management tools. The CLI supports all programmatic operations through terminal commands, making it ideal for development workflows, CI/CD pipelines, and server deployments.
# Install globally
npm install -g db-migrate
# Use from anywhere
db-migrate --help# Install locally
npm install --save db-migrate
# Use via npm scripts or npx
npx db-migrate --help
node node_modules/db-migrate/bin/db-migrate --helpdb-migrate [command] [arguments] [options]Execute pending migrations to apply schema changes.
# Run all pending migrations
db-migrate up
# Run up to specific migration
db-migrate up [migration-name]
# Run specific number of migrations
db-migrate up -c [count]
# Run with specific environment
db-migrate up --env production
# Run with verbose output
db-migrate up --verbose
# Dry run (show SQL without executing)
db-migrate up --dry-runExamples:
# Run all pending migrations
db-migrate up
# Run up to specific migration
db-migrate up 20231201120000-add-users-table
# Run only next 3 migrations
db-migrate up -c 3
# Run in production environment
db-migrate up --env production
# Preview changes without executing
db-migrate up --dry-run --verboseExecute migration rollbacks to undo schema changes.
# Roll back last migration
db-migrate down
# Roll back specific number of migrations
db-migrate down -c [count]
# Roll back with specific environment
db-migrate down --env development
# Roll back with verbose output
db-migrate down --verboseExamples:
# Roll back last migration
db-migrate down
# Roll back last 3 migrations
db-migrate down -c 3
# Roll back in test environment
db-migrate down --env testCreate new migration files with proper naming and templates.
# Create new migration
db-migrate create [migration-name]
# Create SQL migration
db-migrate create [migration-name] --sql-file
# Create CoffeeScript migration
db-migrate create [migration-name] --coffee-file
# Create conditional migration
db-migrate create [migration-name] --ignore-on-initExamples:
# Create JavaScript migration
db-migrate create add-users-table
# Create SQL migration
db-migrate create add-indexes --sql-file
# Create migration in specific directory
db-migrate create add-permissions -m ./db/migrationsCheck pending migrations without executing them.
# Check all pending migrations
db-migrate check
# Check specific number of pending migrations
db-migrate check -c [count]
# Check with specific environment
db-migrate check --env stagingReset database by rolling back all migrations.
# Reset all migrations
db-migrate reset
# Reset with specific environment
db-migrate reset --env testSynchronize database to specific migration state.
# Sync to specific migration
db-migrate sync [migration-name]
# Sync with environment
db-migrate sync [migration-name] --env productionCreate a new database.
# Create database
db-migrate db:create [database-name]
# Create with specific environment
db-migrate db:create [database-name] --env production
# Create with specific config
db-migrate db:create [database-name] --config ./config/database.jsonExamples:
# Create development database
db-migrate db:create myapp_development
# Create production database
db-migrate db:create myapp_production --env productionDrop (delete) an existing database.
# Drop database
db-migrate db:drop [database-name]
# Drop with specific environment
db-migrate db:drop [database-name] --env testExamples:
# Drop test database
db-migrate db:drop myapp_test
# Drop staging database
db-migrate db:drop myapp_staging --env stagingExecute database seeding operations.
# Run version-controlled seeds (default)
db-migrate seed
# Run version-controlled seeds explicitly
db-migrate seed:vc
# Run static seeds
db-migrate seed:static
# Run seeds with environment
db-migrate seed --env developmentRollback seeding operations.
# Undo last seed
db-migrate seed:down
# Undo specific number of seeds
db-migrate seed:down -c [count]
# Undo with environment
db-migrate seed:down --env testReset all seeds.
# Reset all seeds
db-migrate seed:reset
# Reset with environment
db-migrate seed:reset --env testTransition migrations to latest protocol version.
# Transition to latest protocol
db-migrate transitionDisplay help information.
# General help
db-migrate --help
db-migrate -h
# Command-specific help
db-migrate up --help
db-migrate create --helpThese options work with most commands:
# Environment selection
-e, --env [environment] # Set environment (dev, test, prod, etc.)
# Configuration
-c, --config [path] # Config file path (default: database.json)
-m, --migrations-dir [path] # Migrations directory path
--vcseeder-dir [path] # Version controlled seeds directory
--staticseeder-dir [path] # Static seeds directory
# Operation control
-c, --count [number] # Number of migrations/seeds to process
--dry-run # Show SQL without executing
--verbose, -v # Verbose output
--force-exit # Force exit after completion
# Table configuration
-t, --table [name] # Migration table name
--seeds-table [name] # Seeds table name
# Transaction control
--non-transactional # Disable transaction wrapping
# Output control
--log-level [level] # Set log level (error, warn, info, verbose, debug, silly)# File generation options
--sql-file # Create SQL migration file
--coffee-file # Create CoffeeScript migration file
--ignore-on-init # Create conditional migration file
# Execution options
--ignore-completed-migrations # Start from first migration regardless of historydb-migrate looks for database.json in the current directory by default:
{
"development": {
"driver": "mysql",
"host": "localhost",
"user": "root",
"password": "password",
"database": "myapp_development"
},
"test": {
"driver": "mysql",
"host": "localhost",
"user": "test",
"password": "test",
"database": "myapp_test"
},
"production": {
"driver": "mysql",
"host": "prod-db.example.com",
"user": "prod_user",
"password": "secure_password",
"database": "myapp_production"
}
}# Use custom config file
db-migrate up --config ./config/database.json
# Use different config for different commands
db-migrate up --config ./config/dev-db.json
db-migrate seed --config ./config/seed-db.jsonCLI respects environment variables for configuration:
# Database connection
export DATABASE_URL=mysql://user:pass@host:port/db
# Migration settings
export DB_MIGRATE_TABLE=custom_migrations
export DB_MIGRATE_SEEDS_TABLE=custom_seeds
# Directory paths
export DB_MIGRATE_MIGRATIONS_DIR=./database/migrations
export DB_MIGRATE_SEEDS_DIR=./database/seeds# Setup development environment
db-migrate db:create myapp_development --env development
db-migrate up --env development
db-migrate seed --env development
# Create and apply new migration
db-migrate create add-products-table
db-migrate up --env development
# Test rollback
db-migrate down -c 1 --env development
db-migrate up --env development# Setup test environment
db-migrate db:create myapp_test --env test
db-migrate up --env test
db-migrate seed:static --env test
# Reset for clean tests
db-migrate reset --env test
db-migrate up --env test
db-migrate seed:static --env test# Check what will be applied
db-migrate check --env production --verbose
# Apply migrations (with dry run first)
db-migrate up --env production --dry-run
db-migrate up --env production
# Apply minimal production seeds
db-migrate seed --env production#!/bin/bash
set -e
# Test environment
echo "Setting up test database..."
db-migrate db:create myapp_test --env test
db-migrate up --env test
db-migrate seed:static --env test
# Run tests
npm test
# Production deployment
if [ "$DEPLOY_ENV" = "production" ]; then
echo "Deploying to production..."
db-migrate up --env production
echo "Production migration completed"
fi# Success
0 # Command completed successfully
# Error codes
1 # General error (migration failed, config error, etc.)
2 # Invalid arguments or usage
3 # Database connection error
4 # Migration file error# Check exit status
db-migrate up --env production
if [ $? -eq 0 ]; then
echo "Migrations completed successfully"
else
echo "Migrations failed with exit code $?"
exit 1
fi
# Verbose error information
db-migrate up --env production --verbose 2>&1 | tee migration.logAdd to package.json:
{
"scripts": {
"migrate:up": "db-migrate up",
"migrate:down": "db-migrate down",
"migrate:create": "db-migrate create",
"migrate:reset": "db-migrate reset",
"migrate:check": "db-migrate check",
"seed:run": "db-migrate seed",
"seed:undo": "db-migrate seed:down",
"db:create": "db-migrate db:create",
"db:drop": "db-migrate db:drop",
"db:setup": "npm run db:create && npm run migrate:up && npm run seed:run"
}
}Usage:
npm run migrate:up
npm run migrate:create add-users-table
npm run db:setup# Command syntax patterns
db-migrate <command> [arguments] [options]
db-migrate up [migration-name] [options]
db-migrate down [options]
db-migrate create <migration-name> [options]
db-migrate check [options]
db-migrate reset [options]
db-migrate sync <migration-name> [options]
db-migrate db:create <database-name> [options]
db-migrate db:drop <database-name> [options]
db-migrate seed [options]
db-migrate seed:down [options]
db-migrate seed:reset [options]
db-migrate transition [options]