Registry / database / db-auto-migrate

db-auto-migrate

JSON →
library1.1.6jsnpmunverified

Automatic MySQL database migration and schema synchronization tool. Version 1.1.6. It reads 'CREATE TABLE' .sql files and automatically syncs the database schema to match those definitions. Features include upgrade (versioned, irreversible migration with logging and rollback protection) and autoSync (development-only automatic schema sync with risk assessment). Only supports MySQL. Release cadence: actively maintained with periodic updates. Key differentiator: full automation of schema synchronization based on single source of truth (DDL files), with no manual migration scripts needed for common changes.

npm install db-auto-migrate
INSTALL
IMPORT
SIG · DB-AUTO-MIGRATE
D
db-auto-migrate
databasejavascriptv1.1.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default (Migration class)
const Migration = require('db-auto-migrate');
import Migration from 'db-auto-migrate';
This package uses CommonJS, not ESM. No default export; the constructor is exported directly.
Migration options
const migration = new Migration(options);
const migration = new Migration.default(options);
Migration is a constructor; options object is required.
Upgrade method
await migration.upgrade();
migration.upgrade().then();
async/await is recommended; returns a Promise.

Basic setup to perform an upgrade migration using db-auto-migrate with configuration from environment variables.

const Migration = require('db-auto-migrate'); const options = { dir: __dirname, db: { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '', database: process.env.DB_NAME ?? 'mydb', }, prefix: '', env: process.env.NODE_ENV ?? 'development', autoSync: 'manual', tempDb: { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '', database: '__temp_sync__temp_db', }, }; async function onServerStart() { let migration = new Migration(options); await migration.upgrade(); console.log('Database synced.'); } onServerStart().catch(console.error);
Debug
Known issues
breakingautoSync must NOT be used in production as it can delete tables/columns automatically.
fix
Set `env` to anything other than 'development' (e.g., 'production') when deploying. Or set `autoSync: 'off'` or `'manual'`.
affects: >=0.0.0
gotchaThe temporary database for autoSync must have a name starting with '__temp_sync__', otherwise the library will throw an error.
fix
Ensure `tempDb.database` begins with '__temp_sync__'.
affects: >=1.0.0
gotchaUpgrade files must follow version naming (e.g., '1.0', 'v2.1.1') and cannot be modified after execution. Only append new upgrades.
fix
Do not edit old upgrade files; add new ones with higher version numbers.
affects: >=0.0.0
deprecatedThe `logs` option can be set to `false` to disable logging, but the API expects an object with `log`, `warn`, `error` methods if not false.
fix
Pass an object with those methods or set to `false` to suppress logs.
affects: >=0.0.0
Errors
Common errors & fixes
Error: tempDb.database must start with '__temp_sync__'
The temporary database name does not match the required prefix.
fix
Set `tempDb.database` to a string starting with '__temp_sync__', e.g., '__temp_sync__migration'.
TypeError: Migration is not a constructor
Trying to import the package incorrectly (e.g., using ES6 import which doesn't work with CJS module).
fix
Use `const Migration = require('db-auto-migrate');` and then `new Migration(options)`.
Error: autoSync is not allowed in non-development environment
The `env` option is set to something other than 'development' while `autoSync` is enabled.
fix
Set `env: 'development'` for development, or change `autoSync` to 'manual' or 'off' in production.
Upgrade
Version history
1.1.6latest on npm
Audit
Dependencies
mysql2optionalDatabase driver for MySQL connections
Agent activity
6 hits · last 30 days
node
6
Resources
db-auto-migrate — npm install db-auto-migrate · libregistry