@yandjin-mikro-orm/migrations 6.1.4-rc-sti-changes-1
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 +10 -0
- package/JSMigrationGenerator.js +28 -0
- package/LICENSE +21 -0
- package/Migration.d.ts +29 -0
- package/Migration.js +55 -0
- package/MigrationGenerator.d.ts +26 -0
- package/MigrationGenerator.js +43 -0
- package/MigrationRunner.d.ts +16 -0
- package/MigrationRunner.js +51 -0
- package/MigrationStorage.d.ts +31 -0
- package/MigrationStorage.js +132 -0
- package/Migrator.d.ts +68 -0
- package/Migrator.js +340 -0
- package/README.md +383 -0
- package/TSMigrationGenerator.d.ts +10 -0
- package/TSMigrationGenerator.js +24 -0
- package/index.d.ts +12 -0
- package/index.js +28 -0
- package/index.mjs +10 -0
- package/package.json +71 -0
- package/typings.d.ts +1 -0
- package/typings.js +2 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MigrationGenerator } from './MigrationGenerator';
|
|
2
|
+
export declare class JSMigrationGenerator extends MigrationGenerator {
|
|
3
|
+
/**
|
|
4
|
+
* @inheritDoc
|
|
5
|
+
*/
|
|
6
|
+
generateMigrationFile(className: string, diff: {
|
|
7
|
+
up: string[];
|
|
8
|
+
down: string[];
|
|
9
|
+
}): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JSMigrationGenerator = void 0;
|
|
4
|
+
const MigrationGenerator_1 = require("./MigrationGenerator");
|
|
5
|
+
class JSMigrationGenerator extends MigrationGenerator_1.MigrationGenerator {
|
|
6
|
+
/**
|
|
7
|
+
* @inheritDoc
|
|
8
|
+
*/
|
|
9
|
+
generateMigrationFile(className, diff) {
|
|
10
|
+
let ret = `'use strict';\n`;
|
|
11
|
+
ret += `Object.defineProperty(exports, '__esModule', { value: true });\n`;
|
|
12
|
+
ret += `const { Migration } = require('@mikro-orm/migrations');\n\n`;
|
|
13
|
+
ret += `class ${className} extends Migration {\n\n`;
|
|
14
|
+
ret += ` async up() {\n`;
|
|
15
|
+
diff.up.forEach(sql => ret += this.createStatement(sql, 4));
|
|
16
|
+
ret += ` }\n\n`;
|
|
17
|
+
/* istanbul ignore else */
|
|
18
|
+
if (diff.down.length > 0) {
|
|
19
|
+
ret += ` async down() {\n`;
|
|
20
|
+
diff.down.forEach(sql => ret += this.createStatement(sql, 4));
|
|
21
|
+
ret += ` }\n\n`;
|
|
22
|
+
}
|
|
23
|
+
ret += `}\n`;
|
|
24
|
+
ret += `exports.${className} = ${className};\n`;
|
|
25
|
+
return ret;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.JSMigrationGenerator = JSMigrationGenerator;
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Martin Adámek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Migration.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Configuration, Transaction } from "@yandjin-mikro-orm/core";
|
|
2
|
+
import type { AbstractSqlDriver, Knex, EntityManager } from "@yandjin-mikro-orm/knex";
|
|
3
|
+
export type Query = string | Knex.QueryBuilder | Knex.Raw;
|
|
4
|
+
export declare abstract class Migration {
|
|
5
|
+
protected readonly driver: AbstractSqlDriver;
|
|
6
|
+
protected readonly config: Configuration;
|
|
7
|
+
private readonly queries;
|
|
8
|
+
protected ctx?: Transaction<Knex.Transaction>;
|
|
9
|
+
private em?;
|
|
10
|
+
constructor(driver: AbstractSqlDriver, config: Configuration);
|
|
11
|
+
abstract up(): Promise<void>;
|
|
12
|
+
down(): Promise<void>;
|
|
13
|
+
isTransactional(): boolean;
|
|
14
|
+
addSql(sql: Query): void;
|
|
15
|
+
reset(): void;
|
|
16
|
+
setTransactionContext(ctx: Transaction): void;
|
|
17
|
+
/**
|
|
18
|
+
* Executes a raw SQL query. Accepts a string SQL or a knex query builder instance.
|
|
19
|
+
* The `params` parameter is respected only if you use string SQL in the first parameter.
|
|
20
|
+
*/
|
|
21
|
+
execute(sql: Query, params?: unknown[]): Promise<import("@yandjin-mikro-orm/core").EntityData<Partial<any>>[]>;
|
|
22
|
+
getKnex(): Knex<any, any[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a cached `EntityManager` instance for this migration, which will respect
|
|
25
|
+
* the current transaction context.
|
|
26
|
+
*/
|
|
27
|
+
getEntityManager(): EntityManager;
|
|
28
|
+
getQueries(): Query[];
|
|
29
|
+
}
|
package/Migration.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Migration = void 0;
|
|
4
|
+
class Migration {
|
|
5
|
+
driver;
|
|
6
|
+
config;
|
|
7
|
+
queries = [];
|
|
8
|
+
ctx;
|
|
9
|
+
em;
|
|
10
|
+
constructor(driver, config) {
|
|
11
|
+
this.driver = driver;
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
async down() {
|
|
15
|
+
throw new Error("This migration cannot be reverted");
|
|
16
|
+
}
|
|
17
|
+
isTransactional() {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
addSql(sql) {
|
|
21
|
+
this.queries.push(sql);
|
|
22
|
+
}
|
|
23
|
+
reset() {
|
|
24
|
+
this.queries.length = 0;
|
|
25
|
+
this.ctx = undefined;
|
|
26
|
+
}
|
|
27
|
+
setTransactionContext(ctx) {
|
|
28
|
+
this.ctx = ctx;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Executes a raw SQL query. Accepts a string SQL or a knex query builder instance.
|
|
32
|
+
* The `params` parameter is respected only if you use string SQL in the first parameter.
|
|
33
|
+
*/
|
|
34
|
+
async execute(sql, params) {
|
|
35
|
+
return this.driver.execute(sql, params, "all", this.ctx);
|
|
36
|
+
}
|
|
37
|
+
getKnex() {
|
|
38
|
+
return this.driver.getConnection("write").getKnex();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a cached `EntityManager` instance for this migration, which will respect
|
|
42
|
+
* the current transaction context.
|
|
43
|
+
*/
|
|
44
|
+
getEntityManager() {
|
|
45
|
+
if (!this.em) {
|
|
46
|
+
this.em = this.driver.createEntityManager();
|
|
47
|
+
this.em.setTransactionContext(this.ctx);
|
|
48
|
+
}
|
|
49
|
+
return this.em;
|
|
50
|
+
}
|
|
51
|
+
getQueries() {
|
|
52
|
+
return this.queries;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.Migration = Migration;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type IMigrationGenerator, type MigrationsOptions, type NamingStrategy } from "@yandjin-mikro-orm/core";
|
|
2
|
+
import type { AbstractSqlDriver } from "@yandjin-mikro-orm/knex";
|
|
3
|
+
export declare abstract class MigrationGenerator implements IMigrationGenerator {
|
|
4
|
+
protected readonly driver: AbstractSqlDriver;
|
|
5
|
+
protected readonly namingStrategy: NamingStrategy;
|
|
6
|
+
protected readonly options: MigrationsOptions;
|
|
7
|
+
constructor(driver: AbstractSqlDriver, namingStrategy: NamingStrategy, options: MigrationsOptions);
|
|
8
|
+
/**
|
|
9
|
+
* @inheritDoc
|
|
10
|
+
*/
|
|
11
|
+
generate(diff: {
|
|
12
|
+
up: string[];
|
|
13
|
+
down: string[];
|
|
14
|
+
}, path?: string, name?: string): Promise<[string, string]>;
|
|
15
|
+
/**
|
|
16
|
+
* @inheritDoc
|
|
17
|
+
*/
|
|
18
|
+
createStatement(sql: string, padLeft: number): string;
|
|
19
|
+
/**
|
|
20
|
+
* @inheritDoc
|
|
21
|
+
*/
|
|
22
|
+
abstract generateMigrationFile(className: string, diff: {
|
|
23
|
+
up: string[];
|
|
24
|
+
down: string[];
|
|
25
|
+
}): string;
|
|
26
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MigrationGenerator = void 0;
|
|
4
|
+
const core_1 = require("@yandjin-mikro-orm/core");
|
|
5
|
+
const fs_extra_1 = require("fs-extra");
|
|
6
|
+
class MigrationGenerator {
|
|
7
|
+
driver;
|
|
8
|
+
namingStrategy;
|
|
9
|
+
options;
|
|
10
|
+
constructor(driver, namingStrategy, options) {
|
|
11
|
+
this.driver = driver;
|
|
12
|
+
this.namingStrategy = namingStrategy;
|
|
13
|
+
this.options = options;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @inheritDoc
|
|
17
|
+
*/
|
|
18
|
+
async generate(diff, path, name) {
|
|
19
|
+
/* istanbul ignore next */
|
|
20
|
+
const defaultPath = this.options.emit === "ts" && this.options.pathTs
|
|
21
|
+
? this.options.pathTs
|
|
22
|
+
: this.options.path;
|
|
23
|
+
path = core_1.Utils.normalizePath(this.driver.config.get("baseDir"), path ?? defaultPath);
|
|
24
|
+
await (0, fs_extra_1.ensureDir)(path);
|
|
25
|
+
const timestamp = new Date().toISOString().replace(/[-T:]|\.\d{3}z$/gi, "");
|
|
26
|
+
const className = this.namingStrategy.classToMigrationName(timestamp, name);
|
|
27
|
+
const fileName = `${this.options.fileName(timestamp, name)}.${this.options.emit}`;
|
|
28
|
+
const ret = this.generateMigrationFile(className, diff);
|
|
29
|
+
await (0, fs_extra_1.writeFile)(path + "/" + fileName, ret, { flush: true });
|
|
30
|
+
return [ret, fileName];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @inheritDoc
|
|
34
|
+
*/
|
|
35
|
+
createStatement(sql, padLeft) {
|
|
36
|
+
if (sql) {
|
|
37
|
+
const padding = " ".repeat(padLeft);
|
|
38
|
+
return `${padding}this.addSql('${sql.replace(/['\\]/g, "\\'")}');\n`;
|
|
39
|
+
}
|
|
40
|
+
return "\n";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.MigrationGenerator = MigrationGenerator;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Configuration, type MigrationsOptions, type Transaction } from "@yandjin-mikro-orm/core";
|
|
2
|
+
import type { AbstractSqlDriver } from "@yandjin-mikro-orm/knex";
|
|
3
|
+
import type { Migration } from "./Migration";
|
|
4
|
+
export declare class MigrationRunner {
|
|
5
|
+
protected readonly driver: AbstractSqlDriver;
|
|
6
|
+
protected readonly options: MigrationsOptions;
|
|
7
|
+
protected readonly config: Configuration;
|
|
8
|
+
private readonly connection;
|
|
9
|
+
private readonly helper;
|
|
10
|
+
private masterTransaction?;
|
|
11
|
+
constructor(driver: AbstractSqlDriver, options: MigrationsOptions, config: Configuration);
|
|
12
|
+
run(migration: Migration, method: "up" | "down"): Promise<void>;
|
|
13
|
+
setMasterMigration(trx: Transaction): void;
|
|
14
|
+
unsetMasterMigration(): void;
|
|
15
|
+
private getQueries;
|
|
16
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MigrationRunner = void 0;
|
|
4
|
+
const core_1 = require("@yandjin-mikro-orm/core");
|
|
5
|
+
class MigrationRunner {
|
|
6
|
+
driver;
|
|
7
|
+
options;
|
|
8
|
+
config;
|
|
9
|
+
connection;
|
|
10
|
+
helper;
|
|
11
|
+
masterTransaction;
|
|
12
|
+
constructor(driver, options, config) {
|
|
13
|
+
this.driver = driver;
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.connection = this.driver.getConnection();
|
|
17
|
+
this.helper = this.driver.getPlatform().getSchemaHelper();
|
|
18
|
+
}
|
|
19
|
+
async run(migration, method) {
|
|
20
|
+
migration.reset();
|
|
21
|
+
if (!this.options.transactional || !migration.isTransactional()) {
|
|
22
|
+
const queries = await this.getQueries(migration, method);
|
|
23
|
+
await core_1.Utils.runSerial(queries, (sql) => this.driver.execute(sql));
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
await this.connection.transactional(async (tx) => {
|
|
27
|
+
migration.setTransactionContext(tx);
|
|
28
|
+
const queries = await this.getQueries(migration, method);
|
|
29
|
+
await core_1.Utils.runSerial(queries, (sql) => this.driver.execute(sql, undefined, "all", tx));
|
|
30
|
+
}, { ctx: this.masterTransaction });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
setMasterMigration(trx) {
|
|
34
|
+
this.masterTransaction = trx;
|
|
35
|
+
}
|
|
36
|
+
unsetMasterMigration() {
|
|
37
|
+
delete this.masterTransaction;
|
|
38
|
+
}
|
|
39
|
+
async getQueries(migration, method) {
|
|
40
|
+
await migration[method]();
|
|
41
|
+
let queries = migration.getQueries();
|
|
42
|
+
if (this.options.disableForeignKeys) {
|
|
43
|
+
const charset = this.config.get("charset");
|
|
44
|
+
queries.unshift(...this.helper.getSchemaBeginning(charset).split("\n"));
|
|
45
|
+
queries.push(...this.helper.getSchemaEnd().split("\n"));
|
|
46
|
+
}
|
|
47
|
+
queries = queries.filter((sql) => !core_1.Utils.isString(sql) || sql.trim().length > 0);
|
|
48
|
+
return queries;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.MigrationRunner = MigrationRunner;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { MigrationsOptions, Transaction } from "@yandjin-mikro-orm/core";
|
|
2
|
+
import type { AbstractSqlDriver } from "@yandjin-mikro-orm/knex";
|
|
3
|
+
import type { MigrationParams, UmzugStorage } from "umzug";
|
|
4
|
+
import type { MigrationRow } from "./typings";
|
|
5
|
+
export declare class MigrationStorage implements UmzugStorage {
|
|
6
|
+
protected readonly driver: AbstractSqlDriver;
|
|
7
|
+
protected readonly options: MigrationsOptions;
|
|
8
|
+
private readonly connection;
|
|
9
|
+
private readonly helper;
|
|
10
|
+
private masterTransaction?;
|
|
11
|
+
constructor(driver: AbstractSqlDriver, options: MigrationsOptions);
|
|
12
|
+
executed(): Promise<string[]>;
|
|
13
|
+
logMigration(params: MigrationParams<any>): Promise<void>;
|
|
14
|
+
unlogMigration(params: MigrationParams<any>): Promise<void>;
|
|
15
|
+
getExecutedMigrations(): Promise<MigrationRow[]>;
|
|
16
|
+
ensureTable(): Promise<void>;
|
|
17
|
+
setMasterMigration(trx: Transaction): void;
|
|
18
|
+
unsetMasterMigration(): void;
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
getMigrationName(name: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
getTableName(): {
|
|
27
|
+
tableName: string;
|
|
28
|
+
schemaName: string;
|
|
29
|
+
};
|
|
30
|
+
private get knex();
|
|
31
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
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 __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 (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.MigrationStorage = void 0;
|
|
27
|
+
const path = __importStar(require("path"));
|
|
28
|
+
class MigrationStorage {
|
|
29
|
+
driver;
|
|
30
|
+
options;
|
|
31
|
+
connection;
|
|
32
|
+
helper;
|
|
33
|
+
masterTransaction;
|
|
34
|
+
constructor(driver, options) {
|
|
35
|
+
this.driver = driver;
|
|
36
|
+
this.options = options;
|
|
37
|
+
this.connection = this.driver.getConnection();
|
|
38
|
+
this.helper = this.driver.getPlatform().getSchemaHelper();
|
|
39
|
+
}
|
|
40
|
+
async executed() {
|
|
41
|
+
const migrations = await this.getExecutedMigrations();
|
|
42
|
+
return migrations.map(({ name }) => `${this.getMigrationName(name)}`);
|
|
43
|
+
}
|
|
44
|
+
async logMigration(params) {
|
|
45
|
+
const { tableName, schemaName } = this.getTableName();
|
|
46
|
+
const name = this.getMigrationName(params.name);
|
|
47
|
+
await this.driver.nativeInsert(tableName, { name }, { schema: schemaName, ctx: this.masterTransaction });
|
|
48
|
+
}
|
|
49
|
+
async unlogMigration(params) {
|
|
50
|
+
const { tableName, schemaName } = this.getTableName();
|
|
51
|
+
const withoutExt = this.getMigrationName(params.name);
|
|
52
|
+
const names = [withoutExt, withoutExt + ".js", withoutExt + ".ts"];
|
|
53
|
+
const qb = this.knex
|
|
54
|
+
.delete()
|
|
55
|
+
.from(tableName)
|
|
56
|
+
.withSchema(schemaName)
|
|
57
|
+
.where("name", "in", [params.name, ...names]);
|
|
58
|
+
if (this.masterTransaction) {
|
|
59
|
+
qb.transacting(this.masterTransaction);
|
|
60
|
+
}
|
|
61
|
+
await this.connection.execute(qb);
|
|
62
|
+
}
|
|
63
|
+
async getExecutedMigrations() {
|
|
64
|
+
const { tableName, schemaName } = this.getTableName();
|
|
65
|
+
const qb = this.knex
|
|
66
|
+
.select("*")
|
|
67
|
+
.from(tableName)
|
|
68
|
+
.withSchema(schemaName)
|
|
69
|
+
.orderBy("id", "asc");
|
|
70
|
+
if (this.masterTransaction) {
|
|
71
|
+
qb.transacting(this.masterTransaction);
|
|
72
|
+
}
|
|
73
|
+
const res = await this.connection.execute(qb);
|
|
74
|
+
return res.map((row) => {
|
|
75
|
+
if (typeof row.executed_at === "string") {
|
|
76
|
+
row.executed_at = new Date(row.executed_at);
|
|
77
|
+
}
|
|
78
|
+
return row;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async ensureTable() {
|
|
82
|
+
const tables = await this.connection.execute(this.helper.getListTablesSQL(), [], "all", this.masterTransaction);
|
|
83
|
+
const { tableName, schemaName } = this.getTableName();
|
|
84
|
+
if (tables.find((t) => t.table_name === tableName &&
|
|
85
|
+
(!t.schema_name || t.schema_name === schemaName))) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const schemas = await this.helper.getNamespaces(this.connection);
|
|
89
|
+
if (schemaName && !schemas.includes(schemaName)) {
|
|
90
|
+
await this.knex.schema.createSchema(schemaName);
|
|
91
|
+
}
|
|
92
|
+
await this.knex.schema
|
|
93
|
+
.createTable(tableName, (table) => {
|
|
94
|
+
table.increments();
|
|
95
|
+
table.string("name");
|
|
96
|
+
table.dateTime("executed_at").defaultTo(this.knex.fn.now());
|
|
97
|
+
})
|
|
98
|
+
.withSchema(schemaName);
|
|
99
|
+
}
|
|
100
|
+
setMasterMigration(trx) {
|
|
101
|
+
this.masterTransaction = trx;
|
|
102
|
+
}
|
|
103
|
+
unsetMasterMigration() {
|
|
104
|
+
delete this.masterTransaction;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @internal
|
|
108
|
+
*/
|
|
109
|
+
getMigrationName(name) {
|
|
110
|
+
const parsedName = path.parse(name);
|
|
111
|
+
if ([".js", ".ts"].includes(parsedName.ext)) {
|
|
112
|
+
// strip extension
|
|
113
|
+
return parsedName.name;
|
|
114
|
+
}
|
|
115
|
+
return name;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
120
|
+
getTableName() {
|
|
121
|
+
const parts = this.options.tableName.split(".");
|
|
122
|
+
const tableName = parts.length > 1 ? parts[1] : parts[0];
|
|
123
|
+
const schemaName = parts.length > 1
|
|
124
|
+
? parts[0]
|
|
125
|
+
: this.driver.config.get("schema", this.driver.getPlatform().getDefaultSchemaName());
|
|
126
|
+
return { tableName, schemaName };
|
|
127
|
+
}
|
|
128
|
+
get knex() {
|
|
129
|
+
return this.connection.getKnex();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.MigrationStorage = MigrationStorage;
|
package/Migrator.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type MigrationParams, type RunnableMigration } from "umzug";
|
|
2
|
+
import { type Constructor, type IMigrator, type MikroORM } from "@yandjin-mikro-orm/core";
|
|
3
|
+
import { DatabaseSchema, type EntityManager } from "@yandjin-mikro-orm/knex";
|
|
4
|
+
import type { Migration } from "./Migration";
|
|
5
|
+
import { MigrationStorage } from "./MigrationStorage";
|
|
6
|
+
import type { MigrateOptions, MigrationResult, MigrationRow, UmzugMigration } from "./typings";
|
|
7
|
+
export declare class Migrator implements IMigrator {
|
|
8
|
+
private readonly em;
|
|
9
|
+
private umzug;
|
|
10
|
+
private runner;
|
|
11
|
+
private storage;
|
|
12
|
+
private generator;
|
|
13
|
+
private readonly driver;
|
|
14
|
+
private readonly schemaGenerator;
|
|
15
|
+
private readonly config;
|
|
16
|
+
private readonly options;
|
|
17
|
+
private readonly absolutePath;
|
|
18
|
+
private readonly snapshotPath;
|
|
19
|
+
constructor(em: EntityManager);
|
|
20
|
+
static register(orm: MikroORM): void;
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
createMigration(path?: string, blank?: boolean, initial?: boolean, name?: string): Promise<MigrationResult>;
|
|
25
|
+
checkMigrationNeeded(): Promise<boolean>;
|
|
26
|
+
/**
|
|
27
|
+
* @inheritDoc
|
|
28
|
+
*/
|
|
29
|
+
createInitialMigration(path?: string, name?: string): Promise<MigrationResult>;
|
|
30
|
+
private createUmzug;
|
|
31
|
+
/**
|
|
32
|
+
* Initial migration can be created only if:
|
|
33
|
+
* 1. no previous migrations were generated or executed
|
|
34
|
+
* 2. existing schema do not contain any of the tables defined by metadata
|
|
35
|
+
*
|
|
36
|
+
* If existing schema contains all of the tables already, we return true, based on that we mark the migration as already executed.
|
|
37
|
+
* If only some of the tables are present, exception is thrown.
|
|
38
|
+
*/
|
|
39
|
+
private validateInitialMigration;
|
|
40
|
+
/**
|
|
41
|
+
* @inheritDoc
|
|
42
|
+
*/
|
|
43
|
+
getExecutedMigrations(): Promise<MigrationRow[]>;
|
|
44
|
+
private ensureDatabase;
|
|
45
|
+
/**
|
|
46
|
+
* @inheritDoc
|
|
47
|
+
*/
|
|
48
|
+
getPendingMigrations(): Promise<UmzugMigration[]>;
|
|
49
|
+
/**
|
|
50
|
+
* @inheritDoc
|
|
51
|
+
*/
|
|
52
|
+
up(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
|
|
53
|
+
/**
|
|
54
|
+
* @inheritDoc
|
|
55
|
+
*/
|
|
56
|
+
down(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
|
|
57
|
+
getStorage(): MigrationStorage;
|
|
58
|
+
protected resolve(params: MigrationParams<any>): RunnableMigration<any>;
|
|
59
|
+
protected getSchemaFromSnapshot(): Promise<DatabaseSchema | undefined>;
|
|
60
|
+
protected storeCurrentSchema(): Promise<void>;
|
|
61
|
+
protected initialize(MigrationClass: Constructor<Migration>, name: string): RunnableMigration<any>;
|
|
62
|
+
private getSchemaDiff;
|
|
63
|
+
private getMigrationFilename;
|
|
64
|
+
private prefix;
|
|
65
|
+
private runMigrations;
|
|
66
|
+
private runInTransaction;
|
|
67
|
+
private ensureMigrationsDirExists;
|
|
68
|
+
}
|