@mikro-orm/migrations 7.0.0-dev.1 → 7.0.0-dev.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/JSMigrationGenerator.d.ts +1 -1
- package/JSMigrationGenerator.js +3 -7
- package/Migration.d.ts +1 -1
- package/Migration.js +1 -5
- package/MigrationGenerator.d.ts +1 -1
- package/MigrationGenerator.js +8 -11
- package/MigrationRunner.d.ts +2 -2
- package/MigrationRunner.js +5 -9
- package/MigrationStorage.d.ts +2 -2
- package/MigrationStorage.js +6 -43
- package/Migrator.d.ts +19 -10
- package/Migrator.js +83 -57
- package/README.md +3 -2
- package/TSMigrationGenerator.d.ts +1 -1
- package/TSMigrationGenerator.js +2 -6
- package/index.d.ts +8 -8
- package/index.js +8 -24
- package/package.json +8 -17
- package/typings.d.ts +1 -1
- package/typings.js +1 -2
- package/index.mjs +0 -10
package/JSMigrationGenerator.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.JSMigrationGenerator = void 0;
|
|
4
|
-
const MigrationGenerator_1 = require("./MigrationGenerator");
|
|
5
|
-
class JSMigrationGenerator extends MigrationGenerator_1.MigrationGenerator {
|
|
1
|
+
import { MigrationGenerator } from './MigrationGenerator.js';
|
|
2
|
+
export class JSMigrationGenerator extends MigrationGenerator {
|
|
6
3
|
/**
|
|
7
4
|
* @inheritDoc
|
|
8
5
|
*/
|
|
@@ -14,7 +11,7 @@ class JSMigrationGenerator extends MigrationGenerator_1.MigrationGenerator {
|
|
|
14
11
|
ret += ` async up() {\n`;
|
|
15
12
|
diff.up.forEach(sql => ret += this.createStatement(sql, 4));
|
|
16
13
|
ret += ` }\n\n`;
|
|
17
|
-
/*
|
|
14
|
+
/* v8 ignore next */
|
|
18
15
|
if (diff.down.length > 0) {
|
|
19
16
|
ret += ` async down() {\n`;
|
|
20
17
|
diff.down.forEach(sql => ret += this.createStatement(sql, 4));
|
|
@@ -25,4 +22,3 @@ class JSMigrationGenerator extends MigrationGenerator_1.MigrationGenerator {
|
|
|
25
22
|
return ret;
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
|
-
exports.JSMigrationGenerator = JSMigrationGenerator;
|
package/Migration.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Configuration, type RawQueryFragment, type Transaction } from '@mikro-orm/core';
|
|
2
|
-
import type { AbstractSqlDriver, EntityManager, NativeQueryBuilder } from '@mikro-orm/
|
|
2
|
+
import type { AbstractSqlDriver, EntityManager, NativeQueryBuilder } from '@mikro-orm/sql';
|
|
3
3
|
export type Query = string | NativeQueryBuilder | RawQueryFragment;
|
|
4
4
|
export declare abstract class Migration {
|
|
5
5
|
protected readonly driver: AbstractSqlDriver;
|
package/Migration.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Migration = void 0;
|
|
4
|
-
class Migration {
|
|
1
|
+
export class Migration {
|
|
5
2
|
driver;
|
|
6
3
|
config;
|
|
7
4
|
queries = [];
|
|
@@ -49,4 +46,3 @@ class Migration {
|
|
|
49
46
|
return this.queries;
|
|
50
47
|
}
|
|
51
48
|
}
|
|
52
|
-
exports.Migration = Migration;
|
package/MigrationGenerator.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type IMigrationGenerator, type MaybePromise, type MigrationsOptions, type NamingStrategy } from '@mikro-orm/core';
|
|
2
|
-
import type { AbstractSqlDriver } from '@mikro-orm/
|
|
2
|
+
import type { AbstractSqlDriver } from '@mikro-orm/sql';
|
|
3
3
|
export declare abstract class MigrationGenerator implements IMigrationGenerator {
|
|
4
4
|
protected readonly driver: AbstractSqlDriver;
|
|
5
5
|
protected readonly namingStrategy: NamingStrategy;
|
package/MigrationGenerator.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const fs_extra_1 = require("fs-extra");
|
|
6
|
-
class MigrationGenerator {
|
|
1
|
+
import { Utils, } from '@mikro-orm/core';
|
|
2
|
+
import { fs } from '@mikro-orm/core/fs-utils';
|
|
3
|
+
import { writeFile } from 'node:fs/promises';
|
|
4
|
+
export class MigrationGenerator {
|
|
7
5
|
driver;
|
|
8
6
|
namingStrategy;
|
|
9
7
|
options;
|
|
@@ -16,15 +14,15 @@ class MigrationGenerator {
|
|
|
16
14
|
* @inheritDoc
|
|
17
15
|
*/
|
|
18
16
|
async generate(diff, path, name) {
|
|
19
|
-
/*
|
|
17
|
+
/* v8 ignore next */
|
|
20
18
|
const defaultPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
|
|
21
|
-
path =
|
|
22
|
-
|
|
19
|
+
path = Utils.normalizePath(this.driver.config.get('baseDir'), path ?? defaultPath);
|
|
20
|
+
fs.ensureDir(path);
|
|
23
21
|
const timestamp = new Date().toISOString().replace(/[-T:]|\.\d{3}z$/ig, '');
|
|
24
22
|
const className = this.namingStrategy.classToMigrationName(timestamp, name);
|
|
25
23
|
const fileName = `${this.options.fileName(timestamp, name)}.${this.options.emit}`;
|
|
26
24
|
const ret = await this.generateMigrationFile(className, diff);
|
|
27
|
-
await
|
|
25
|
+
await writeFile(path + '/' + fileName, ret, { flush: true });
|
|
28
26
|
return [ret, fileName];
|
|
29
27
|
}
|
|
30
28
|
/**
|
|
@@ -38,4 +36,3 @@ class MigrationGenerator {
|
|
|
38
36
|
return '\n';
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
|
-
exports.MigrationGenerator = MigrationGenerator;
|
package/MigrationRunner.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Configuration, type MigrationsOptions, type Transaction } from '@mikro-orm/core';
|
|
2
|
-
import type { AbstractSqlDriver } from '@mikro-orm/
|
|
3
|
-
import type { Migration } from './Migration';
|
|
2
|
+
import type { AbstractSqlDriver } from '@mikro-orm/sql';
|
|
3
|
+
import type { Migration } from './Migration.js';
|
|
4
4
|
export declare class MigrationRunner {
|
|
5
5
|
protected readonly driver: AbstractSqlDriver;
|
|
6
6
|
protected readonly options: MigrationsOptions;
|
package/MigrationRunner.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.MigrationRunner = void 0;
|
|
4
|
-
const core_1 = require("@mikro-orm/core");
|
|
5
|
-
class MigrationRunner {
|
|
1
|
+
import { Utils } from '@mikro-orm/core';
|
|
2
|
+
export class MigrationRunner {
|
|
6
3
|
driver;
|
|
7
4
|
options;
|
|
8
5
|
config;
|
|
@@ -20,13 +17,13 @@ class MigrationRunner {
|
|
|
20
17
|
migration.reset();
|
|
21
18
|
if (!this.options.transactional || !migration.isTransactional()) {
|
|
22
19
|
const queries = await this.getQueries(migration, method);
|
|
23
|
-
await
|
|
20
|
+
await Utils.runSerial(queries, sql => this.driver.execute(sql));
|
|
24
21
|
}
|
|
25
22
|
else {
|
|
26
23
|
await this.connection.transactional(async (tx) => {
|
|
27
24
|
migration.setTransactionContext(tx);
|
|
28
25
|
const queries = await this.getQueries(migration, method);
|
|
29
|
-
await
|
|
26
|
+
await Utils.runSerial(queries, sql => this.driver.execute(sql, undefined, 'all', tx));
|
|
30
27
|
}, { ctx: this.masterTransaction });
|
|
31
28
|
}
|
|
32
29
|
}
|
|
@@ -42,8 +39,7 @@ class MigrationRunner {
|
|
|
42
39
|
let queries = migration.getQueries();
|
|
43
40
|
queries.unshift(...this.helper.getSchemaBeginning(charset, this.options.disableForeignKeys).split('\n'));
|
|
44
41
|
queries.push(...this.helper.getSchemaEnd(this.options.disableForeignKeys).split('\n'));
|
|
45
|
-
queries = queries.filter(sql =>
|
|
42
|
+
queries = queries.filter(sql => typeof sql !== 'string' || sql.trim().length > 0);
|
|
46
43
|
return queries;
|
|
47
44
|
}
|
|
48
45
|
}
|
|
49
|
-
exports.MigrationRunner = MigrationRunner;
|
package/MigrationStorage.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MigrationsOptions, Transaction } from '@mikro-orm/core';
|
|
2
|
-
import { type AbstractSqlDriver } from '@mikro-orm/
|
|
2
|
+
import { type AbstractSqlDriver } from '@mikro-orm/sql';
|
|
3
3
|
import type { MigrationParams, UmzugStorage } from 'umzug';
|
|
4
|
-
import type { MigrationRow } from './typings';
|
|
4
|
+
import type { MigrationRow } from './typings.js';
|
|
5
5
|
export declare class MigrationStorage implements UmzugStorage {
|
|
6
6
|
protected readonly driver: AbstractSqlDriver;
|
|
7
7
|
protected readonly options: MigrationsOptions;
|
package/MigrationStorage.js
CHANGED
|
@@ -1,42 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.MigrationStorage = void 0;
|
|
37
|
-
const knex_1 = require("@mikro-orm/knex");
|
|
38
|
-
const path = __importStar(require("node:path"));
|
|
39
|
-
class MigrationStorage {
|
|
1
|
+
import { DatabaseTable, } from '@mikro-orm/sql';
|
|
2
|
+
import { parse } from 'node:path';
|
|
3
|
+
export class MigrationStorage {
|
|
40
4
|
driver;
|
|
41
5
|
options;
|
|
42
6
|
connection;
|
|
@@ -89,7 +53,7 @@ class MigrationStorage {
|
|
|
89
53
|
const sql = this.helper.getCreateNamespaceSQL(schemaName);
|
|
90
54
|
await this.connection.execute(sql);
|
|
91
55
|
}
|
|
92
|
-
const table = new
|
|
56
|
+
const table = new DatabaseTable(this.platform, tableName, schemaName);
|
|
93
57
|
table.addColumn({
|
|
94
58
|
name: 'id',
|
|
95
59
|
type: this.platform.getIntegerTypeDeclarationSQL({ autoincrement: true, unsigned: true }),
|
|
@@ -111,7 +75,7 @@ class MigrationStorage {
|
|
|
111
75
|
length,
|
|
112
76
|
});
|
|
113
77
|
const sql = this.helper.createTable(table);
|
|
114
|
-
await this.connection.execute(sql.join(';\n'));
|
|
78
|
+
await this.connection.execute(sql.join(';\n'), [], 'run', this.masterTransaction);
|
|
115
79
|
}
|
|
116
80
|
setMasterMigration(trx) {
|
|
117
81
|
this.masterTransaction = trx;
|
|
@@ -123,7 +87,7 @@ class MigrationStorage {
|
|
|
123
87
|
* @internal
|
|
124
88
|
*/
|
|
125
89
|
getMigrationName(name) {
|
|
126
|
-
const parsedName =
|
|
90
|
+
const parsedName = parse(name);
|
|
127
91
|
if (['.js', '.ts'].includes(parsedName.ext)) {
|
|
128
92
|
// strip extension
|
|
129
93
|
return parsedName.name;
|
|
@@ -140,4 +104,3 @@ class MigrationStorage {
|
|
|
140
104
|
return { tableName, schemaName };
|
|
141
105
|
}
|
|
142
106
|
}
|
|
143
|
-
exports.MigrationStorage = MigrationStorage;
|
package/Migrator.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type MigrationParams, type RunnableMigration } from 'umzug';
|
|
2
|
-
import { type Constructor, type IMigrator, type
|
|
3
|
-
import { DatabaseSchema, type EntityManager } from '@mikro-orm/
|
|
4
|
-
import type { Migration } from './Migration';
|
|
5
|
-
import { MigrationStorage } from './MigrationStorage';
|
|
6
|
-
import type { MigrateOptions, MigrationResult, MigrationRow, UmzugMigration } from './typings';
|
|
2
|
+
import { type Constructor, type IMigrator, type MaybePromise, type MigratorEvent, type MikroORM } from '@mikro-orm/core';
|
|
3
|
+
import { DatabaseSchema, type EntityManager } from '@mikro-orm/sql';
|
|
4
|
+
import type { Migration } from './Migration.js';
|
|
5
|
+
import { MigrationStorage } from './MigrationStorage.js';
|
|
6
|
+
import type { MigrateOptions, MigrationResult, MigrationRow, UmzugMigration } from './typings.js';
|
|
7
7
|
export declare class Migrator implements IMigrator {
|
|
8
8
|
private readonly em;
|
|
9
9
|
private umzug;
|
|
@@ -17,16 +17,25 @@ export declare class Migrator implements IMigrator {
|
|
|
17
17
|
private readonly absolutePath;
|
|
18
18
|
private readonly snapshotPath;
|
|
19
19
|
constructor(em: EntityManager);
|
|
20
|
+
/**
|
|
21
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
22
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
23
|
+
* used for the TS variant (`pathTs` option).
|
|
24
|
+
*
|
|
25
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
26
|
+
* break existing projects, only help with the new ones.
|
|
27
|
+
*/
|
|
28
|
+
private detectSourceFolder;
|
|
20
29
|
static register(orm: MikroORM): void;
|
|
21
30
|
/**
|
|
22
31
|
* @inheritDoc
|
|
23
32
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
create(path?: string, blank?: boolean, initial?: boolean, name?: string): Promise<MigrationResult>;
|
|
34
|
+
checkSchema(): Promise<boolean>;
|
|
26
35
|
/**
|
|
27
36
|
* @inheritDoc
|
|
28
37
|
*/
|
|
29
|
-
|
|
38
|
+
createInitial(path?: string, name?: string, blank?: boolean): Promise<MigrationResult>;
|
|
30
39
|
/**
|
|
31
40
|
* @inheritDoc
|
|
32
41
|
*/
|
|
@@ -48,12 +57,12 @@ export declare class Migrator implements IMigrator {
|
|
|
48
57
|
/**
|
|
49
58
|
* @inheritDoc
|
|
50
59
|
*/
|
|
51
|
-
|
|
60
|
+
getExecuted(): Promise<MigrationRow[]>;
|
|
52
61
|
private ensureDatabase;
|
|
53
62
|
/**
|
|
54
63
|
* @inheritDoc
|
|
55
64
|
*/
|
|
56
|
-
|
|
65
|
+
getPending(): Promise<UmzugMigration[]>;
|
|
57
66
|
/**
|
|
58
67
|
* @inheritDoc
|
|
59
68
|
*/
|
package/Migrator.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const JSMigrationGenerator_1 = require("./JSMigrationGenerator");
|
|
13
|
-
class Migrator {
|
|
1
|
+
import { Umzug, } from 'umzug';
|
|
2
|
+
import { basename, join } from 'node:path';
|
|
3
|
+
import { existsSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { t, Type, UnknownType, Utils, } from '@mikro-orm/core';
|
|
5
|
+
import { fs } from '@mikro-orm/core/fs-utils';
|
|
6
|
+
import { DatabaseSchema, DatabaseTable, } from '@mikro-orm/sql';
|
|
7
|
+
import { MigrationRunner } from './MigrationRunner.js';
|
|
8
|
+
import { MigrationStorage } from './MigrationStorage.js';
|
|
9
|
+
import { TSMigrationGenerator } from './TSMigrationGenerator.js';
|
|
10
|
+
import { JSMigrationGenerator } from './JSMigrationGenerator.js';
|
|
11
|
+
export class Migrator {
|
|
14
12
|
em;
|
|
15
13
|
umzug;
|
|
16
14
|
runner;
|
|
@@ -25,32 +23,60 @@ class Migrator {
|
|
|
25
23
|
constructor(em) {
|
|
26
24
|
this.em = em;
|
|
27
25
|
this.driver = this.em.getDriver();
|
|
28
|
-
this.schemaGenerator = new knex_1.SqlSchemaGenerator(this.em);
|
|
29
26
|
this.config = this.em.config;
|
|
30
27
|
this.options = this.config.get('migrations');
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
this.schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
|
|
29
|
+
this.detectSourceFolder();
|
|
30
|
+
/* v8 ignore next */
|
|
31
|
+
const key = (this.config.get('preferTs', Utils.detectTypeScriptSupport()) && this.options.pathTs) ? 'pathTs' : 'path';
|
|
32
|
+
this.absolutePath = Utils.absolutePath(this.options[key], this.config.get('baseDir'));
|
|
33
|
+
// for snapshots, we always want to use the path based on `emit` option, regardless of whether we run in TS context
|
|
34
|
+
/* v8 ignore next */
|
|
36
35
|
const snapshotPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
|
|
37
|
-
const absoluteSnapshotPath =
|
|
38
|
-
const dbName =
|
|
36
|
+
const absoluteSnapshotPath = Utils.absolutePath(snapshotPath, this.config.get('baseDir'));
|
|
37
|
+
const dbName = basename(this.config.get('dbName'));
|
|
39
38
|
const snapshotName = this.options.snapshotName ?? `.snapshot-${dbName}`;
|
|
40
|
-
this.snapshotPath =
|
|
39
|
+
this.snapshotPath = Utils.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
|
|
41
40
|
this.createUmzug();
|
|
42
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
44
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
45
|
+
* used for the TS variant (`pathTs` option).
|
|
46
|
+
*
|
|
47
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
48
|
+
* break existing projects, only help with the new ones.
|
|
49
|
+
*/
|
|
50
|
+
detectSourceFolder() {
|
|
51
|
+
const baseDir = this.config.get('baseDir');
|
|
52
|
+
const defaultPath = './migrations';
|
|
53
|
+
if (!fs.pathExists(baseDir + '/src')) {
|
|
54
|
+
this.options.path ??= defaultPath;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const exists = fs.pathExists(`${baseDir}/${defaultPath}`);
|
|
58
|
+
const distDir = fs.pathExists(baseDir + '/dist');
|
|
59
|
+
const buildDir = fs.pathExists(baseDir + '/build');
|
|
60
|
+
// if neither `dist` nor `build` exist, we use the `src` folder as it might be a JS project without building, but with `src` folder
|
|
61
|
+
/* v8 ignore next */
|
|
62
|
+
const path = distDir ? './dist' : (buildDir ? './build' : './src');
|
|
63
|
+
// only if the user did not provide any values and if the default path does not exist
|
|
64
|
+
if (!this.options.path && !this.options.pathTs && !exists) {
|
|
65
|
+
this.options.path = `${path}/migrations`;
|
|
66
|
+
this.options.pathTs = './src/migrations';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
43
69
|
static register(orm) {
|
|
44
70
|
orm.config.registerExtension('@mikro-orm/migrator', () => new Migrator(orm.em));
|
|
45
71
|
}
|
|
46
72
|
/**
|
|
47
73
|
* @inheritDoc
|
|
48
74
|
*/
|
|
49
|
-
async
|
|
75
|
+
async create(path, blank = false, initial = false, name) {
|
|
50
76
|
if (initial) {
|
|
51
|
-
return this.
|
|
77
|
+
return this.createInitial(path, name, blank);
|
|
52
78
|
}
|
|
53
|
-
|
|
79
|
+
this.ensureMigrationsDirExists();
|
|
54
80
|
const diff = await this.getSchemaDiff(blank, initial);
|
|
55
81
|
if (diff.up.length === 0) {
|
|
56
82
|
return { fileName: '', code: '', diff };
|
|
@@ -63,16 +89,16 @@ class Migrator {
|
|
|
63
89
|
diff,
|
|
64
90
|
};
|
|
65
91
|
}
|
|
66
|
-
async
|
|
67
|
-
|
|
92
|
+
async checkSchema() {
|
|
93
|
+
this.ensureMigrationsDirExists();
|
|
68
94
|
const diff = await this.getSchemaDiff(false, false);
|
|
69
95
|
return diff.up.length > 0;
|
|
70
96
|
}
|
|
71
97
|
/**
|
|
72
98
|
* @inheritDoc
|
|
73
99
|
*/
|
|
74
|
-
async
|
|
75
|
-
|
|
100
|
+
async createInitial(path, name, blank = false) {
|
|
101
|
+
this.ensureMigrationsDirExists();
|
|
76
102
|
const schemaExists = await this.validateInitialMigration(blank);
|
|
77
103
|
const diff = await this.getSchemaDiff(blank, true);
|
|
78
104
|
const migration = await this.generator.generate(diff, path, name);
|
|
@@ -101,10 +127,10 @@ class Migrator {
|
|
|
101
127
|
return this;
|
|
102
128
|
}
|
|
103
129
|
createUmzug() {
|
|
104
|
-
this.runner = new
|
|
105
|
-
this.storage = new
|
|
130
|
+
this.runner = new MigrationRunner(this.driver, this.options, this.config);
|
|
131
|
+
this.storage = new MigrationStorage(this.driver, this.options);
|
|
106
132
|
let migrations = {
|
|
107
|
-
glob:
|
|
133
|
+
glob: join(this.absolutePath, this.options.glob).replace(/\\/g, '/'),
|
|
108
134
|
resolve: (params) => this.resolve(params),
|
|
109
135
|
};
|
|
110
136
|
if (this.options.migrationsList) {
|
|
@@ -115,11 +141,12 @@ class Migrator {
|
|
|
115
141
|
return this.initialize(migration.class, migration.name);
|
|
116
142
|
});
|
|
117
143
|
}
|
|
118
|
-
this.umzug = new
|
|
144
|
+
this.umzug = new Umzug({
|
|
119
145
|
storage: this.storage,
|
|
120
146
|
logger: undefined,
|
|
121
147
|
migrations,
|
|
122
148
|
});
|
|
149
|
+
/* v8 ignore else */
|
|
123
150
|
if (!this.options.silent) {
|
|
124
151
|
const logger = this.config.getLogger();
|
|
125
152
|
this.umzug.on('migrating', event => logger.log('migrator', `Processing '${event.name}'`, { enabled: true }));
|
|
@@ -131,10 +158,10 @@ class Migrator {
|
|
|
131
158
|
this.generator = new this.options.generator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
132
159
|
}
|
|
133
160
|
else if (this.options.emit === 'js' || this.options.emit === 'cjs') {
|
|
134
|
-
this.generator = new
|
|
161
|
+
this.generator = new JSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
135
162
|
}
|
|
136
163
|
else {
|
|
137
|
-
this.generator = new
|
|
164
|
+
this.generator = new TSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
138
165
|
}
|
|
139
166
|
}
|
|
140
167
|
/**
|
|
@@ -146,12 +173,12 @@ class Migrator {
|
|
|
146
173
|
* If only some of the tables are present, exception is thrown.
|
|
147
174
|
*/
|
|
148
175
|
async validateInitialMigration(blank) {
|
|
149
|
-
const executed = await this.
|
|
150
|
-
const pending = await this.
|
|
176
|
+
const executed = await this.getExecuted();
|
|
177
|
+
const pending = await this.getPending();
|
|
151
178
|
if (executed.length > 0 || pending.length > 0) {
|
|
152
179
|
throw new Error('Initial migration cannot be created, as some migrations already exist');
|
|
153
180
|
}
|
|
154
|
-
const schema = await
|
|
181
|
+
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config);
|
|
155
182
|
const exists = new Set();
|
|
156
183
|
const expected = new Set();
|
|
157
184
|
Object.values(this.em.getMetadata().getAll())
|
|
@@ -178,14 +205,14 @@ class Migrator {
|
|
|
178
205
|
/**
|
|
179
206
|
* @inheritDoc
|
|
180
207
|
*/
|
|
181
|
-
async
|
|
208
|
+
async getExecuted() {
|
|
182
209
|
await this.ensureDatabase();
|
|
183
210
|
return this.storage.getExecutedMigrations();
|
|
184
211
|
}
|
|
185
212
|
async ensureDatabase() {
|
|
186
|
-
|
|
213
|
+
this.ensureMigrationsDirExists();
|
|
187
214
|
const created = await this.schemaGenerator.ensureDatabase();
|
|
188
|
-
/*
|
|
215
|
+
/* v8 ignore next */
|
|
189
216
|
if (created) {
|
|
190
217
|
this.createUmzug();
|
|
191
218
|
}
|
|
@@ -194,7 +221,7 @@ class Migrator {
|
|
|
194
221
|
/**
|
|
195
222
|
* @inheritDoc
|
|
196
223
|
*/
|
|
197
|
-
async
|
|
224
|
+
async getPending() {
|
|
198
225
|
await this.ensureDatabase();
|
|
199
226
|
return this.umzug.pending();
|
|
200
227
|
}
|
|
@@ -215,8 +242,8 @@ class Migrator {
|
|
|
215
242
|
}
|
|
216
243
|
resolve(params) {
|
|
217
244
|
const createMigrationHandler = async (method) => {
|
|
218
|
-
const migration = await
|
|
219
|
-
const MigrationClass = Object.values(migration)
|
|
245
|
+
const migration = await Utils.dynamicImport(params.path);
|
|
246
|
+
const MigrationClass = Object.values(migration).find(cls => typeof cls === 'function' && typeof cls.constructor === 'function');
|
|
220
247
|
const instance = new MigrationClass(this.driver, this.config);
|
|
221
248
|
await this.runner.run(instance, method);
|
|
222
249
|
};
|
|
@@ -227,20 +254,20 @@ class Migrator {
|
|
|
227
254
|
};
|
|
228
255
|
}
|
|
229
256
|
getSchemaFromSnapshot() {
|
|
230
|
-
if (!this.options.snapshot || !(
|
|
257
|
+
if (!this.options.snapshot || !existsSync(this.snapshotPath)) {
|
|
231
258
|
return undefined;
|
|
232
259
|
}
|
|
233
|
-
const data =
|
|
234
|
-
const schema = new
|
|
260
|
+
const data = fs.readJSONSync(this.snapshotPath);
|
|
261
|
+
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
|
|
235
262
|
const { tables, namespaces, ...rest } = data;
|
|
236
263
|
const tableInstances = tables.map((tbl) => {
|
|
237
|
-
const table = new
|
|
264
|
+
const table = new DatabaseTable(this.driver.getPlatform(), tbl.name);
|
|
238
265
|
const { columns, ...restTable } = tbl;
|
|
239
266
|
Object.assign(table, restTable);
|
|
240
267
|
Object.keys(columns).forEach(col => {
|
|
241
268
|
const column = { ...columns[col] };
|
|
242
|
-
/*
|
|
243
|
-
column.mappedType =
|
|
269
|
+
/* v8 ignore next */
|
|
270
|
+
column.mappedType = Type.getType(t[columns[col].mappedType] ?? UnknownType);
|
|
244
271
|
table.addColumn(column);
|
|
245
272
|
});
|
|
246
273
|
return table;
|
|
@@ -253,7 +280,7 @@ class Migrator {
|
|
|
253
280
|
return;
|
|
254
281
|
}
|
|
255
282
|
const schema = this.schemaGenerator.getTargetSchema();
|
|
256
|
-
|
|
283
|
+
writeFileSync(this.snapshotPath, JSON.stringify(schema, null, 2));
|
|
257
284
|
}
|
|
258
285
|
initialize(MigrationClass, name) {
|
|
259
286
|
const instance = new MigrationClass(this.driver, this.config);
|
|
@@ -301,8 +328,8 @@ class Migrator {
|
|
|
301
328
|
return name.match(/^\d{14}$/) ? this.options.fileName(name) : name;
|
|
302
329
|
}
|
|
303
330
|
prefix(options) {
|
|
304
|
-
if (
|
|
305
|
-
return { migrations:
|
|
331
|
+
if (typeof options === 'string' || Array.isArray(options)) {
|
|
332
|
+
return { migrations: Utils.asArray(options).map(name => this.getMigrationFilename(name)) };
|
|
306
333
|
}
|
|
307
334
|
if (!options) {
|
|
308
335
|
return {};
|
|
@@ -321,7 +348,7 @@ class Migrator {
|
|
|
321
348
|
if (!this.options.transactional || !this.options.allOrNothing) {
|
|
322
349
|
return this.umzug[method](this.prefix(options));
|
|
323
350
|
}
|
|
324
|
-
if (
|
|
351
|
+
if (Utils.isObject(options) && options.transaction) {
|
|
325
352
|
return this.runInTransaction(options.transaction, method, options);
|
|
326
353
|
}
|
|
327
354
|
return this.driver.getConnection().transactional(trx => this.runInTransaction(trx, method, options));
|
|
@@ -334,10 +361,9 @@ class Migrator {
|
|
|
334
361
|
this.storage.unsetMasterMigration();
|
|
335
362
|
return ret;
|
|
336
363
|
}
|
|
337
|
-
|
|
364
|
+
ensureMigrationsDirExists() {
|
|
338
365
|
if (!this.options.migrationsList) {
|
|
339
|
-
|
|
366
|
+
fs.ensureDir(this.absolutePath);
|
|
340
367
|
}
|
|
341
368
|
}
|
|
342
369
|
}
|
|
343
|
-
exports.Migrator = Migrator;
|
package/README.md
CHANGED
|
@@ -11,7 +11,6 @@ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-or
|
|
|
11
11
|
[](https://discord.gg/w8bjxFHS7X)
|
|
12
12
|
[](https://www.npmjs.com/package/@mikro-orm/core)
|
|
13
13
|
[](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
|
|
14
|
-
[](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
|
|
15
14
|
[](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
|
|
16
15
|
|
|
17
16
|
## 🤔 Unit of What?
|
|
@@ -141,7 +140,7 @@ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit m
|
|
|
141
140
|
- [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
|
|
142
141
|
- [Filters](https://mikro-orm.io/docs/filters)
|
|
143
142
|
- [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
|
|
144
|
-
- [
|
|
143
|
+
- [Populating relations](https://mikro-orm.io/docs/populating-relations)
|
|
145
144
|
- [Property Validation](https://mikro-orm.io/docs/property-validation)
|
|
146
145
|
- [Lifecycle Hooks](https://mikro-orm.io/docs/events#hooks)
|
|
147
146
|
- [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
|
|
@@ -382,6 +381,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
|
|
|
382
381
|
|
|
383
382
|
Please ⭐️ this repository if this project helped you!
|
|
384
383
|
|
|
384
|
+
> If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
|
|
385
|
+
|
|
385
386
|
## 📝 License
|
|
386
387
|
|
|
387
388
|
Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
|
package/TSMigrationGenerator.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.TSMigrationGenerator = void 0;
|
|
4
|
-
const MigrationGenerator_1 = require("./MigrationGenerator");
|
|
5
|
-
class TSMigrationGenerator extends MigrationGenerator_1.MigrationGenerator {
|
|
1
|
+
import { MigrationGenerator } from './MigrationGenerator.js';
|
|
2
|
+
export class TSMigrationGenerator extends MigrationGenerator {
|
|
6
3
|
/**
|
|
7
4
|
* @inheritDoc
|
|
8
5
|
*/
|
|
@@ -21,4 +18,3 @@ class TSMigrationGenerator extends MigrationGenerator_1.MigrationGenerator {
|
|
|
21
18
|
return ret;
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
|
-
exports.TSMigrationGenerator = TSMigrationGenerator;
|
package/index.d.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
* @module migrations
|
|
4
4
|
*/
|
|
5
|
-
export * from './Migrator';
|
|
6
|
-
export * from './Migration';
|
|
7
|
-
export * from './MigrationRunner';
|
|
8
|
-
export * from './MigrationGenerator';
|
|
9
|
-
export * from './JSMigrationGenerator';
|
|
10
|
-
export * from './TSMigrationGenerator';
|
|
11
|
-
export * from './MigrationStorage';
|
|
12
|
-
export * from './typings';
|
|
5
|
+
export * from './Migrator.js';
|
|
6
|
+
export * from './Migration.js';
|
|
7
|
+
export * from './MigrationRunner.js';
|
|
8
|
+
export * from './MigrationGenerator.js';
|
|
9
|
+
export * from './JSMigrationGenerator.js';
|
|
10
|
+
export * from './TSMigrationGenerator.js';
|
|
11
|
+
export * from './MigrationStorage.js';
|
|
12
|
+
export * from './typings.js';
|
package/index.js
CHANGED
|
@@ -1,28 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
1
|
/**
|
|
18
2
|
* @packageDocumentation
|
|
19
3
|
* @module migrations
|
|
20
4
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
5
|
+
export * from './Migrator.js';
|
|
6
|
+
export * from './Migration.js';
|
|
7
|
+
export * from './MigrationRunner.js';
|
|
8
|
+
export * from './MigrationGenerator.js';
|
|
9
|
+
export * from './JSMigrationGenerator.js';
|
|
10
|
+
export * from './TSMigrationGenerator.js';
|
|
11
|
+
export * from './MigrationStorage.js';
|
|
12
|
+
export * from './typings.js';
|
package/package.json
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/migrations",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "7.0.0-dev.100",
|
|
4
5
|
"description": "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.",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"module": "index.mjs",
|
|
7
|
-
"typings": "index.d.ts",
|
|
8
6
|
"exports": {
|
|
9
7
|
"./package.json": "./package.json",
|
|
10
|
-
".":
|
|
11
|
-
"import": {
|
|
12
|
-
"types": "./index.d.ts",
|
|
13
|
-
"default": "./index.mjs"
|
|
14
|
-
},
|
|
15
|
-
"require": "./index.js"
|
|
16
|
-
}
|
|
8
|
+
".": "./index.js"
|
|
17
9
|
},
|
|
18
10
|
"repository": {
|
|
19
11
|
"type": "git",
|
|
@@ -46,10 +38,10 @@
|
|
|
46
38
|
},
|
|
47
39
|
"homepage": "https://mikro-orm.io",
|
|
48
40
|
"engines": {
|
|
49
|
-
"node": ">= 22.
|
|
41
|
+
"node": ">= 22.17.0"
|
|
50
42
|
},
|
|
51
43
|
"scripts": {
|
|
52
|
-
"build": "yarn clean && yarn compile && yarn copy
|
|
44
|
+
"build": "yarn clean && yarn compile && yarn copy",
|
|
53
45
|
"clean": "yarn run -T rimraf ./dist",
|
|
54
46
|
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
55
47
|
"copy": "node ../../scripts/copy.mjs"
|
|
@@ -58,14 +50,13 @@
|
|
|
58
50
|
"access": "public"
|
|
59
51
|
},
|
|
60
52
|
"dependencies": {
|
|
61
|
-
"@mikro-orm/
|
|
62
|
-
"fs-extra": "11.3.0",
|
|
53
|
+
"@mikro-orm/sql": "7.0.0-dev.100",
|
|
63
54
|
"umzug": "3.8.2"
|
|
64
55
|
},
|
|
65
56
|
"devDependencies": {
|
|
66
|
-
"@mikro-orm/core": "^6.
|
|
57
|
+
"@mikro-orm/core": "^6.6.2"
|
|
67
58
|
},
|
|
68
59
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
60
|
+
"@mikro-orm/core": "7.0.0-dev.100"
|
|
70
61
|
}
|
|
71
62
|
}
|
package/typings.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { UmzugMigration, MigrateOptions, MigrationResult, MigrationRow } from '@mikro-orm/core';
|
|
1
|
+
export type { UmzugMigration, MigrateOptions, MigrationResult, MigrationRow } from '@mikro-orm/core';
|
package/typings.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/index.mjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import mod from "./index.js";
|
|
2
|
-
|
|
3
|
-
export default mod;
|
|
4
|
-
export const JSMigrationGenerator = mod.JSMigrationGenerator;
|
|
5
|
-
export const Migration = mod.Migration;
|
|
6
|
-
export const MigrationGenerator = mod.MigrationGenerator;
|
|
7
|
-
export const MigrationRunner = mod.MigrationRunner;
|
|
8
|
-
export const MigrationStorage = mod.MigrationStorage;
|
|
9
|
-
export const Migrator = mod.Migrator;
|
|
10
|
-
export const TSMigrationGenerator = mod.TSMigrationGenerator;
|