@mikro-orm/migrations
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
322 lines (321 loc) • 13.7 kB
JavaScript
import { Utils, t, Type, UnknownType, } from '@mikro-orm/core';
import { AbstractMigrator } from '@mikro-orm/core/migrations';
import { DatabaseSchema, DatabaseTable, } from '@mikro-orm/sql';
import { MigrationRunner } from './MigrationRunner.js';
import { MigrationStorage } from './MigrationStorage.js';
import { TSMigrationGenerator } from './TSMigrationGenerator.js';
import { JSMigrationGenerator } from './JSMigrationGenerator.js';
/** Manages SQL database migrations: creation, execution, and rollback of schema changes. */
export class Migrator extends AbstractMigrator {
#schemaGenerator;
#snapshotPath;
constructor(em) {
super(em);
this.#schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
}
static register(orm) {
orm.config.registerExtension('@mikro-orm/migrator', () => new Migrator(orm.em));
}
createRunner() {
return new MigrationRunner(this.driver, this.options, this.config);
}
createStorage() {
return new MigrationStorage(this.driver, this.options);
}
getDefaultGenerator() {
if (this.options.emit === 'js' || this.options.emit === 'cjs') {
return new JSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
}
return new TSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
}
async getSnapshotPath() {
if (!this.#snapshotPath) {
const { fs } = await import('@mikro-orm/core/fs-utils');
// for snapshots, we always want to use the path based on `emit` option, regardless of whether we run in TS context
/* v8 ignore next */
const snapshotPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
const absoluteSnapshotPath = fs.absolutePath(snapshotPath, this.config.get('baseDir'));
const dbName = this.config.get('dbName').replace(/\\/g, '/').split('/').pop().replace(/:/g, '');
const snapshotName = this.options.snapshotName ?? `.snapshot-${dbName}`;
this.#snapshotPath = fs.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
}
return this.#snapshotPath;
}
async init() {
if (this.initialized) {
return;
}
await super.init();
const created = await this.#schemaGenerator.ensureDatabase();
/* v8 ignore next */
if (created) {
this.initServices();
}
// Defer tracking-table creation when wildcard-schema fan-out is enabled — pre-creating
// here would land in the default schema and leave a stray `mikro_orm_migrations` after
// `migrator.up({ schema })` against a virgin DB. For the standard flow, eager creation
// keeps the per-call query log clean (no `tableExists`/`getNamespaces`/`create` probe
// on first storage access).
if (!this.options.includeWildcardSchema) {
await this.storage.ensureTable();
}
}
/**
* @inheritDoc
*/
async create(path, blank = false, initial = false, name) {
const offline = !initial && (await this.hasSnapshot());
await (offline ? this.initPaths() : this.init());
if (initial) {
return this.createInitial(path, name, blank);
}
const diff = await this.getSchemaDiff(blank, initial);
if (diff.up.length === 0) {
return { fileName: '', code: '', diff };
}
const migration = await this.generator.generate(diff, path, name);
await this.storeCurrentSchema();
return {
fileName: migration[1],
code: migration[0],
diff,
};
}
async getPending(options) {
if (!(await this.hasSnapshot())) {
return super.getPending(options);
}
await this.initPaths();
const all = await this.discoverMigrations();
const schema = options?.schema ?? this.options.schema;
this.storage.setRunSchema?.(schema);
try {
// probe the DB via `ensureTable` — if it fails, the DB is unreachable and
// we treat every discovered migration as pending; otherwise let errors
// from `storage.executed()` propagate so real bugs are not swallowed
try {
await this.storage.ensureTable();
}
catch {
return all.map(m => ({ name: m.name, path: m.path }));
}
const executed = new Set(await this.storage.executed());
return all.filter(m => !executed.has(m.name)).map(m => ({ name: m.name, path: m.path }));
}
finally {
this.storage.unsetRunSchema?.();
}
}
async hasSnapshot() {
if (!this.options.snapshot) {
return false;
}
const { fs } = await import('@mikro-orm/core/fs-utils');
return fs.pathExists(await this.getSnapshotPath());
}
async checkSchema() {
const snapshot = await this.getSchemaFromSnapshot();
if (!snapshot) {
await this.init();
}
const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
wrap: false,
safe: this.options.safe,
dropTables: this.options.dropTables,
fromSchema: snapshot,
});
return diff.up.trim().length > 0;
}
/**
* @inheritDoc
*/
async createInitial(path, name, blank = false) {
await this.init();
const schemaExists = await this.validateInitialMigration(blank);
const diff = await this.getSchemaDiff(blank, true);
const migration = await this.generator.generate(diff, path, name);
await this.storeCurrentSchema();
if (schemaExists && !blank) {
await this.storage.logMigration({ name: migration[1] });
}
return {
fileName: migration[1],
code: migration[0],
diff,
};
}
async runMigrations(method, options) {
const result = await super.runMigrations(method, options);
if (result.length > 0 && this.options.snapshot) {
const ctx = Utils.isObject(options) ? options.transaction : undefined;
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config, undefined, undefined, undefined, undefined, undefined, ctx);
try {
await this.storeCurrentSchema(schema);
}
catch {
// Silently ignore for read-only filesystems (production).
}
}
return result;
}
getStorage() {
return this.storage;
}
/**
* Initial migration can be created only if:
* 1. no previous migrations were generated or executed
* 2. existing schema do not contain any of the tables defined by metadata
*
* If existing schema contains all of the tables already, we return true, based on that we mark the migration as already executed.
* If only some of the tables are present, exception is thrown.
*/
async validateInitialMigration(blank) {
const executed = await this.getExecuted();
const pending = await this.getPending();
if (executed.length > 0 || pending.length > 0) {
throw new Error('Initial migration cannot be created, as some migrations already exist');
}
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config);
const exists = new Set();
const expected = new Set();
[...this.em.getMetadata().getAll().values()]
.filter(meta => meta.tableName && !meta.embeddable && !meta.virtual)
.forEach(meta => {
const schema = meta.schema ?? this.config.get('schema', this.em.getPlatform().getDefaultSchemaName());
expected.add(schema ? `${schema}.${meta.collection}` : meta.collection);
});
schema.getTables().forEach(table => {
const schema = table.schema ?? this.em.getPlatform().getDefaultSchemaName();
const tableName = schema ? `${schema}.${table.name}` : table.name;
if (expected.has(tableName)) {
exists.add(table.schema ? `${table.schema}.${table.name}` : table.name);
}
});
if (expected.size === 0 && !blank) {
throw new Error('No entities found');
}
if (exists.size > 0 && expected.size !== exists.size) {
throw new Error(`Some tables already exist in your schema, remove them first to create the initial migration: ${[...exists].join(', ')}`);
}
return expected.size === exists.size;
}
async getSchemaFromSnapshot() {
if (!this.options.snapshot) {
return undefined;
}
const snapshotPath = await this.getSnapshotPath();
const { fs } = await import('@mikro-orm/core/fs-utils');
if (!fs.pathExists(snapshotPath)) {
return undefined;
}
const data = fs.readJSONSync(snapshotPath);
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
const { tables, namespaces, ...rest } = data;
// share schema-level native enums by reference; fall back to per-table copy on older snapshots
const sharedNativeEnums = rest.nativeEnums ?? {};
const hasSharedNativeEnums = Object.keys(sharedNativeEnums).length > 0;
const tableInstances = tables.map((tbl) => {
const table = new DatabaseTable(this.driver.getPlatform(), tbl.name, tbl.schema);
table.nativeEnums = hasSharedNativeEnums ? sharedNativeEnums : (tbl.nativeEnums ?? {});
table.comment = tbl.comment;
if (tbl.indexes) {
table.setIndexes(tbl.indexes);
}
if (tbl.checks) {
table.setChecks(tbl.checks);
}
if (tbl.foreignKeys) {
table.setForeignKeys(tbl.foreignKeys);
}
const cols = tbl.columns;
Object.keys(cols).forEach(col => {
const column = { ...cols[col] };
/* v8 ignore next */
column.mappedType = Type.getType(t[cols[col].mappedType] ?? UnknownType);
table.addColumn(column);
});
return table;
});
schema.setTables(tableInstances);
schema.setNamespaces(new Set(namespaces));
if (rest.nativeEnums) {
schema.setNativeEnums(rest.nativeEnums);
}
if (rest.views) {
schema.setViews(rest.views);
}
return schema;
}
async storeCurrentSchema(schema) {
if (!this.options.snapshot) {
return;
}
const snapshotPath = await this.getSnapshotPath();
schema ??= this.#schemaGenerator.getTargetSchema();
const { fs } = await import('@mikro-orm/core/fs-utils');
await fs.writeFile(snapshotPath, JSON.stringify(schema, null, 2));
}
async getSchemaDiff(blank, initial) {
const up = [];
const down = [];
// Split SQL by statement boundaries (semicolons followed by newline) rather than
// just newlines, to preserve multiline statements like view definitions.
// Blank lines (from double newlines) are preserved as empty strings for grouping.
// Splits inside single-quoted string literals are re-merged (GH #7185).
const splitStatements = (sql) => {
const result = [];
let buf = '';
for (const chunk of sql.split(/;\n/)) {
buf += (buf ? ';\n' : '') + chunk;
// odd number of single quotes means we're inside a string literal
if (buf.split(`'`).length % 2 === 0) {
continue;
}
// A chunk starting with \n indicates there was a blank line (grouping separator)
if (buf.startsWith('\n')) {
result.push('');
}
const trimmed = buf.trim();
if (trimmed) {
result.push(trimmed.endsWith(';') ? trimmed : trimmed + ';');
}
buf = '';
}
return result;
};
if (blank) {
up.push('select 1');
down.push('select 1');
}
else if (initial) {
const dump = await this.#schemaGenerator.getCreateSchemaSQL({
wrap: false,
includeWildcardSchema: this.options.includeWildcardSchema,
});
up.push(...splitStatements(dump));
}
else {
const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
wrap: false,
safe: this.options.safe,
dropTables: this.options.dropTables,
fromSchema: await this.getSchemaFromSnapshot(),
includeWildcardSchema: this.options.includeWildcardSchema,
});
up.push(...splitStatements(diff.up));
down.push(...splitStatements(diff.down));
}
const cleanUp = (diff) => {
for (let i = diff.length - 1; i >= 0; i--) {
if (diff[i]) {
break;
}
/* v8 ignore next */
diff.splice(i, 1);
}
};
cleanUp(up);
cleanUp(down);
return { up, down };
}
}