@sqb/migrator
v6.8.0
Published
Database migrator for SQB
Readme
@sqb/migrator
Database migrator for SQB.
About SQB
SQB is an extensible, multi-dialect SQL query builder and Database connection wrapper for NodeJS.
About @sqb/migrator
@sqb/migrator is a schema/data migration runner for SQB. A MigrationPackage describes an
ordered set of versioned migrations, each made up of one or more tasks:
- a raw SQL script (a
.sqlfile, or an inline string/function), - a data-insert task (
{ tableName, rows }), or - a custom function that runs arbitrary code against the connection.
Migrations and tasks can be declared inline or discovered from disk via glob patterns (e.g.
v*/migration.json + *.task.sql). DbMigrator.execute() loads the package, compares its
migrations against the target database's tracked version, and applies everything up to an
optional targetVersion in order — recording progress (and per-task success/error events) in a
migration_summary / migration_events table pair it creates automatically. Migration scripts
can reference $(schema), $(tablespace) and other variables that get substituted per-run.
import { DbMigrator } from '@sqb/migrator';
const migrator = new DbMigrator();
await migrator.execute({
connection: { dialect: 'postgres', database: 'mydb' },
migrationPackage: {
name: 'my-app',
migrations: ['migrations/v*/migration.json'],
},
});PostgreSQL, Oracle, MySQL, MariaDB, SQL Server, and SQLite (both the native sqlite driver and
the WASM-based sqljs driver) are supported today, via the bundled PgMigrationAdapter,
OracleMigrationAdapter, MysqlMigrationAdapter, MariadbMigrationAdapter,
MssqlMigrationAdapter, SqliteMigrationAdapter, and SqljsMigrationAdapter. SqljsMigrationAdapter
is deprecated along with @sqb/sqljs and will be removed in SQB 7.0.0; use
SqliteMigrationAdapter. Each adapter has a few dialect-driven differences worth knowing:
- Schema auto-creation. Postgres, MySQL, MariaDB, and SQL Server all auto-create
infoSchema(CREATE SCHEMA IF NOT EXISTS, which for MySQL/MariaDB is justCREATE DATABASE). Oracle is the exception: a schema is a user there, and provisioning one is a DBA-level operation this adapter deliberately doesn't attempt —infoSchema(if given) is only used as a table-name prefix, and that schema/user must already exist. SQLite has no schema/catalog object at all (sqliteandsqljsalike) -infoSchemais likewise just a table-name prefix there, and$(schema)resolves tomain, SQLite's always-present default database name. - Multi-statement
.sqlscripts. Postgres, MySQL, MariaDB, and SQLite (sqlite/sqljs) run a whole multi-statement script - trigger/procedure bodies included - in one call. Oracle needs script-splitting: it follows the standard SQL*Plus convention where a PL/SQL block (a trigger, procedure, function, package body, or a bareBEGIN/DECLAREblock) is terminated by a lone/on its own line, and everything else is plain DDL/DML,;-separated - required becauseoracledbruns exactly one statement per call. SQL Server needs a different kind of splitting: T-SQL requiresCREATE TRIGGER/PROCEDURE/FUNCTION/VIEWto be the first statement in their batch, so a script mixing e.g. aCREATE TABLEwith aCREATE TRIGGERneeds aGOon its own line between them, same as sqlcmd/SSMS. - Locking. Postgres, MySQL, and MariaDB use their native session-scoped advisory-lock functions
(
pg_advisory_lock,GET_LOCK/RELEASE_LOCK), unaffected by the implicit commit their own DDL triggers. Oracle usesDBMS_LOCKfor the same reason, falling back to running unprotected if it isn't grantable in your environment. SQL Server DDL does not implicitly commit, soMssqlMigrationAdapteruses a transaction-scopedsp_getapplockinstead, released automatically when that transaction commits. SQLite (sqlite/sqljs) is embedded and single-process with no server to arbitrate a lock between clients, so locking is a no-op there. - MariaDB's
GET_LOCKrejects a negative timeout (it warns and returnsNULLinstead of waiting forever, unlike MySQL) - confirmed against a live server.MariadbMigrationAdapteruses a very large positive timeout (2^30 seconds) to get the same effectively-infinite wait. sqljsnever writes back to disk on its own - sql.js loads a whole file into memory once at connect time and only ever mutates that in-memory copy.SqljsMigrationAdapterexports and saves the result back to the original file when the migration run finishes (skipped for a:memory:database), otherwise every applied migration would be silently lost the moment the process exits.
Transactional runs and dry runs
On a dialect that runs DDL inside transactions (PostgreSQL, SQLite), a run can be wrapped in a transaction so that a failure leaves the database exactly as it was:
await migrator.execute({
connection: { dialect: 'postgres', database: 'mydb' },
migrationPackage,
transaction: 'run', // or 'migration', or 'none' (the default)
});transaction: 'run'- one transaction for the whole run: every pending migration, its tasks and the version/event bookkeeping, committed at the end and rolled back on any error. The failure is then recorded inmigration_eventsafter the rollback, so it does not vanish with it.transaction: 'migration'- one transaction per migration, so the migrations that succeeded stay applied when a later one fails.dryRun: true- runs everything in one transaction, emits the same events as a real run, and rolls back instead of committing. The run resolves like a real one, or rejects with the error a real run would have failed with, and leaves the database untouched either way - including the event log. For trying a package against a copy of production before the real thing. It impliestransaction: 'run'.
The adapter's schema lock stays session-level around the transaction. A script holding a statement
the dialect cannot run inside a transaction block (PostgreSQL: CREATE/DROP INDEX CONCURRENTLY,
REINDEX ... CONCURRENTLY, VACUUM, CREATE/DROP DATABASE, CREATE/DROP TABLESPACE, ALTER
SYSTEM, DISCARD ALL; SQLite: VACUUM), or an explicit BEGIN/COMMIT/ROLLBACK that would
cut the run's transaction short, fails a transactional run before anything is applied, naming the
task and file; a script produced by a function is checked when it runs. Apply such a migration with
transaction: 'none'. MySQL, MariaDB and Oracle commit implicitly on DDL, so their adapters refuse
'run' and 'migration'; SQL Server is not wired up for them yet.
No adapter implements backupDatabase()/restoreDatabase(); a migration flagged backup: true
is refused rather than run with a backup that never happened. A transactional run is the way to
make a failed run leave no trace.
Main goals
- Single code base for any sql based database
- Powerful and simplified query coding scheme
- Fast applications with low memory requirements
- Let applications work with large data tables efficiently
- Support latest JavaScript language standards
- Lightweight and extensible framework.
You can report bugs and discuss features on the GitHub issues page
Thanks to all of the great contributions to the project.
You may want to check detailed DOCUMENTATION
Installation
$ npm install @sqb/migrator --saveNode Compatibility
- Node.js 22 or later.
License
SQB is available under the BSD 3-Clause license.
