-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Migrator.ts
380 lines (314 loc) · 13.2 KB
/
Migrator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import type { InputMigrations, MigrateDownOptions, MigrateUpOptions, MigrationParams, RunnableMigration } from 'umzug';
import { Umzug } from 'umzug';
import { basename, join } from 'path';
import { ensureDir, pathExists, writeJSON } from 'fs-extra';
import type { Constructor, Dictionary, IMigrationGenerator, IMigrator, MikroORM, Transaction } from '@mikro-orm/core';
import { t, Type, UnknownType, Utils } from '@mikro-orm/core';
import type { EntityManager } from '@mikro-orm/knex';
import { DatabaseSchema, DatabaseTable, SchemaGenerator } from '@mikro-orm/knex';
import type { Migration } from './Migration';
import { MigrationRunner } from './MigrationRunner';
import { MigrationStorage } from './MigrationStorage';
import type { MigrateOptions, MigrationResult, MigrationRow, UmzugMigration } from './typings';
import { TSMigrationGenerator } from './TSMigrationGenerator';
import { JSMigrationGenerator } from './JSMigrationGenerator';
export class Migrator implements IMigrator {
private umzug!: Umzug;
private runner!: MigrationRunner;
private storage!: MigrationStorage;
private generator!: IMigrationGenerator;
private readonly driver = this.em.getDriver();
private readonly schemaGenerator = new SchemaGenerator(this.em);
private readonly config = this.em.config;
private readonly options = this.config.get('migrations');
private readonly absolutePath: string;
private readonly snapshotPath: string;
constructor(private readonly em: EntityManager) {
/* istanbul ignore next */
const key = (this.config.get('tsNode', Utils.detectTsNode()) && this.options.pathTs) ? 'pathTs' : 'path';
this.absolutePath = Utils.absolutePath(this.options[key]!, this.config.get('baseDir'));
// for snapshots, we always want to use the path based on `emit` option, regardless of whether we run in ts-node context
/* istanbul ignore next */
const snapshotPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path!;
const absoluteSnapshotPath = Utils.absolutePath(snapshotPath, this.config.get('baseDir'));
const dbName = basename(this.config.get('dbName'));
const snapshotName = this.options.snapshotName ?? `.snapshot-${dbName}`;
this.snapshotPath = Utils.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
this.createUmzug();
}
static register(orm: MikroORM): void {
orm.config.registerExtension('@mikro-orm/migrator', new Migrator(orm.em as EntityManager));
}
/**
* @inheritDoc
*/
async createMigration(path?: string, blank = false, initial = false, name?: string): Promise<MigrationResult> {
if (initial) {
return this.createInitialMigration(path);
}
await this.ensureMigrationsDirExists();
const diff = await this.getSchemaDiff(blank, initial);
if (diff.up.length === 0) {
return { fileName: '', code: '', diff };
}
await this.storeCurrentSchema();
const migration = await this.generator.generate(diff, path, name);
return {
fileName: migration[1],
code: migration[0],
diff,
};
}
async checkMigrationNeeded(): Promise<boolean> {
await this.ensureMigrationsDirExists();
const diff = await this.getSchemaDiff(false, false);
return diff.up.length > 0;
}
/**
* @inheritDoc
*/
async createInitialMigration(path?: string): Promise<MigrationResult> {
await this.ensureMigrationsDirExists();
const schemaExists = await this.validateInitialMigration();
const diff = await this.getSchemaDiff(false, true);
await this.storeCurrentSchema();
const migration = await this.generator.generate(diff, path);
if (schemaExists) {
await this.storage.logMigration({ name: migration[1], context: null });
}
return {
fileName: migration[1],
code: migration[0],
diff,
};
}
private createUmzug(): void {
this.runner = new MigrationRunner(this.driver, this.options, this.config);
this.storage = new MigrationStorage(this.driver, this.options);
let migrations: InputMigrations<any> = {
glob: join(this.absolutePath, this.options.glob!).replace(/\\/g, '/'),
resolve: (params: MigrationParams<any>) => this.resolve(params),
};
if (this.options.migrationsList) {
migrations = this.options.migrationsList.map(migration => this.initialize(migration.class as Constructor<Migration>, migration.name));
}
this.umzug = new Umzug({
storage: this.storage,
logger: undefined,
migrations,
});
if (!this.options.silent) {
const logger = this.config.get('logger');
this.umzug.on('migrating', event => logger(`Processing '${event.name}'`));
this.umzug.on('migrated', event => logger(`Applied '${event.name}'`));
this.umzug.on('reverting', event => logger(`Processing '${event.name}'`));
this.umzug.on('reverted', event => logger(`Reverted '${event.name}'`));
}
if (this.options.generator) {
this.generator = new this.options.generator(this.driver, this.config.getNamingStrategy(), this.options);
} else if (this.options.emit === 'js' || this.options.emit === 'cjs') {
this.generator = new JSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
} else {
this.generator = new TSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
}
}
/**
* 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.
*/
private async validateInitialMigration(): Promise<boolean> {
const executed = await this.getExecutedMigrations();
const pending = await this.getPendingMigrations();
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<string>();
const expected = new Set<string>();
Object.values(this.em.getMetadata().getAll())
.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) {
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;
}
/**
* @inheritDoc
*/
async getExecutedMigrations(): Promise<MigrationRow[]> {
await this.ensureDatabase();
return this.storage.getExecutedMigrations();
}
private async ensureDatabase(): Promise<void> {
await this.ensureMigrationsDirExists();
const created = await this.schemaGenerator.ensureDatabase();
/* istanbul ignore next */
if (created) {
this.createUmzug();
}
await this.storage.ensureTable();
}
/**
* @inheritDoc
*/
async getPendingMigrations(): Promise<UmzugMigration[]> {
await this.ensureDatabase();
return this.umzug.pending();
}
/**
* @inheritDoc
*/
async up(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]> {
return this.runMigrations('up', options);
}
/**
* @inheritDoc
*/
async down(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]> {
return this.runMigrations('down', options);
}
getStorage(): MigrationStorage {
return this.storage;
}
protected resolve(params: MigrationParams<any>): RunnableMigration<any> {
const createMigrationHandler = async (method: 'up' | 'down') => {
const migration = await Utils.dynamicImport(params.path!);
const MigrationClass = Object.values(migration)[0] as Constructor<Migration>;
const instance = new MigrationClass(this.driver, this.config);
await this.runner.run(instance, method);
};
return {
name: this.storage.getMigrationName(params.name),
up: () => createMigrationHandler('up'),
down: () => createMigrationHandler('down'),
};
}
protected async getSchemaFromSnapshot() {
if (!this.options.snapshot || !await pathExists(this.snapshotPath)) {
return undefined;
}
const data = await Utils.dynamicImport(this.snapshotPath);
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
const { tables, namespaces, ...rest } = data;
const tableInstances = tables.map((tbl: Dictionary) => {
const table = new DatabaseTable(this.driver.getPlatform(), tbl.name);
const { columns, ...restTable } = tbl;
Object.assign(table, restTable);
Object.keys(columns).forEach(col => {
const column = { ...columns[col] };
/* istanbul ignore next */
column.mappedType = Type.getType(t[columns[col].mappedType] ?? UnknownType);
table.addColumn(column);
});
return table;
});
Object.assign(schema, { tables: tableInstances, namespaces: new Set(namespaces), ...rest });
return schema;
}
protected async storeCurrentSchema(): Promise<void> {
if (!this.options.snapshot) {
return;
}
const schema = this.schemaGenerator.getTargetSchema();
await writeJSON(this.snapshotPath, schema, { spaces: 2 });
}
protected initialize(MigrationClass: Constructor<Migration>, name: string): RunnableMigration<any> {
const instance = new MigrationClass(this.driver, this.config);
return {
name: this.storage.getMigrationName(name),
up: () => this.runner.run(instance, 'up'),
down: () => this.runner.run(instance, 'down'),
};
}
private async getSchemaDiff(blank: boolean, initial: boolean): Promise<{ up: string[]; down: string[] }> {
const up: string[] = [];
const down: string[] = [];
if (blank) {
up.push('select 1');
} else if (initial) {
const dump = await this.schemaGenerator.getCreateSchemaSQL({ wrap: false });
up.push(...dump.split('\n'));
} else {
const diff = await this.schemaGenerator.getUpdateSchemaMigrationSQL({
wrap: false,
safe: this.options.safe,
dropTables: this.options.dropTables,
fromSchema: await this.getSchemaFromSnapshot(),
});
up.push(...diff.up.split('\n'));
down.push(...diff.down.split('\n'));
}
const cleanUp = (diff: string[]) => {
for (let i = diff.length - 1; i >= 0; i--) {
if (diff[i]) {
break;
}
diff.splice(i, 1);
}
};
cleanUp(up);
cleanUp(down);
return { up, down };
}
private getMigrationFilename(name: string): string {
name = name.replace(/\.[jt]s$/, '');
return name.match(/^\d{14}$/) ? this.options.fileName!(name) : name;
}
private prefix<T extends string | string[] | { from?: string | number; to?: string | number; migrations?: string[]; transaction?: Transaction }>(options?: T): MigrateUpOptions & MigrateDownOptions {
if (Utils.isString(options) || Array.isArray(options)) {
return { migrations: Utils.asArray(options).map(name => this.getMigrationFilename(name)) };
}
if (!options) {
return {};
}
if (options.migrations) {
options.migrations = options.migrations.map(name => this.getMigrationFilename(name));
}
if (options.transaction) {
delete options.transaction;
}
['from', 'to'].filter(k => options[k]).forEach(k => options[k] = this.getMigrationFilename(options[k]));
return options as MigrateUpOptions;
}
private async runMigrations(method: 'up' | 'down', options?: string | string[] | MigrateOptions) {
await this.ensureDatabase();
if (!this.options.transactional || !this.options.allOrNothing) {
return this.umzug[method](this.prefix(options as string[]));
}
if (Utils.isObject<MigrateOptions>(options) && options.transaction) {
return this.runInTransaction(options.transaction, method, options);
}
return this.driver.getConnection().transactional(trx => this.runInTransaction(trx, method, options));
}
private async runInTransaction(trx: Transaction, method: 'up' | 'down', options: string | string[] | undefined | MigrateOptions) {
this.runner.setMasterMigration(trx);
this.storage.setMasterMigration(trx);
const ret = await this.umzug[method](this.prefix(options));
this.runner.unsetMasterMigration();
this.storage.unsetMasterMigration();
return ret;
}
private async ensureMigrationsDirExists() {
if (!this.options.migrationsList) {
await ensureDir(this.absolutePath);
}
}
}