@mikro-orm/migrations 7.0.0-dev.76 → 7.0.0-dev.78
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.js +1 -1
- package/MigrationGenerator.js +2 -1
- package/Migrator.d.ts +9 -0
- package/Migrator.js +33 -3
- package/package.json +3 -3
- package/typings.d.ts +1 -1
package/JSMigrationGenerator.js
CHANGED
|
@@ -11,7 +11,7 @@ export class JSMigrationGenerator extends MigrationGenerator {
|
|
|
11
11
|
ret += ` async up() {\n`;
|
|
12
12
|
diff.up.forEach(sql => ret += this.createStatement(sql, 4));
|
|
13
13
|
ret += ` }\n\n`;
|
|
14
|
-
/* v8 ignore next
|
|
14
|
+
/* v8 ignore next */
|
|
15
15
|
if (diff.down.length > 0) {
|
|
16
16
|
ret += ` async down() {\n`;
|
|
17
17
|
diff.down.forEach(sql => ret += this.createStatement(sql, 4));
|
package/MigrationGenerator.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Utils, } from '@mikro-orm/core';
|
|
2
|
+
import { fs } from '@mikro-orm/core/fs-utils';
|
|
2
3
|
import { writeFile } from 'node:fs/promises';
|
|
3
4
|
export class MigrationGenerator {
|
|
4
5
|
driver;
|
|
@@ -16,7 +17,7 @@ export class MigrationGenerator {
|
|
|
16
17
|
/* v8 ignore next */
|
|
17
18
|
const defaultPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
|
|
18
19
|
path = Utils.normalizePath(this.driver.config.get('baseDir'), path ?? defaultPath);
|
|
19
|
-
|
|
20
|
+
fs.ensureDir(path);
|
|
20
21
|
const timestamp = new Date().toISOString().replace(/[-T:]|\.\d{3}z$/ig, '');
|
|
21
22
|
const className = this.namingStrategy.classToMigrationName(timestamp, name);
|
|
22
23
|
const fileName = `${this.options.fileName(timestamp, name)}.${this.options.emit}`;
|
package/Migrator.d.ts
CHANGED
|
@@ -17,6 +17,15 @@ 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
|
package/Migrator.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Umzug, } from 'umzug';
|
|
|
2
2
|
import { basename, join } from 'node:path';
|
|
3
3
|
import { existsSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { t, Type, UnknownType, Utils, } from '@mikro-orm/core';
|
|
5
|
+
import { fs } from '@mikro-orm/core/fs-utils';
|
|
5
6
|
import { DatabaseSchema, DatabaseTable, } from '@mikro-orm/knex';
|
|
6
7
|
import { MigrationRunner } from './MigrationRunner.js';
|
|
7
8
|
import { MigrationStorage } from './MigrationStorage.js';
|
|
@@ -25,6 +26,7 @@ export class Migrator {
|
|
|
25
26
|
this.config = this.em.config;
|
|
26
27
|
this.options = this.config.get('migrations');
|
|
27
28
|
this.schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
|
|
29
|
+
this.detectSourceFolder();
|
|
28
30
|
/* v8 ignore next */
|
|
29
31
|
const key = (this.config.get('preferTs', Utils.detectTypeScriptSupport()) && this.options.pathTs) ? 'pathTs' : 'path';
|
|
30
32
|
this.absolutePath = Utils.absolutePath(this.options[key], this.config.get('baseDir'));
|
|
@@ -37,6 +39,33 @@ export class Migrator {
|
|
|
37
39
|
this.snapshotPath = Utils.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
|
|
38
40
|
this.createUmzug();
|
|
39
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
|
+
}
|
|
40
69
|
static register(orm) {
|
|
41
70
|
orm.config.registerExtension('@mikro-orm/migrator', () => new Migrator(orm.em));
|
|
42
71
|
}
|
|
@@ -117,6 +146,7 @@ export class Migrator {
|
|
|
117
146
|
logger: undefined,
|
|
118
147
|
migrations,
|
|
119
148
|
});
|
|
149
|
+
/* v8 ignore else */
|
|
120
150
|
if (!this.options.silent) {
|
|
121
151
|
const logger = this.config.getLogger();
|
|
122
152
|
this.umzug.on('migrating', event => logger.log('migrator', `Processing '${event.name}'`, { enabled: true }));
|
|
@@ -182,7 +212,7 @@ export class Migrator {
|
|
|
182
212
|
async ensureDatabase() {
|
|
183
213
|
this.ensureMigrationsDirExists();
|
|
184
214
|
const created = await this.schemaGenerator.ensureDatabase();
|
|
185
|
-
/* v8 ignore next
|
|
215
|
+
/* v8 ignore next */
|
|
186
216
|
if (created) {
|
|
187
217
|
this.createUmzug();
|
|
188
218
|
}
|
|
@@ -227,7 +257,7 @@ export class Migrator {
|
|
|
227
257
|
if (!this.options.snapshot || !existsSync(this.snapshotPath)) {
|
|
228
258
|
return undefined;
|
|
229
259
|
}
|
|
230
|
-
const data =
|
|
260
|
+
const data = fs.readJSONSync(this.snapshotPath);
|
|
231
261
|
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
|
|
232
262
|
const { tables, namespaces, ...rest } = data;
|
|
233
263
|
const tableInstances = tables.map((tbl) => {
|
|
@@ -333,7 +363,7 @@ export class Migrator {
|
|
|
333
363
|
}
|
|
334
364
|
ensureMigrationsDirExists() {
|
|
335
365
|
if (!this.options.migrationsList) {
|
|
336
|
-
|
|
366
|
+
fs.ensureDir(this.absolutePath);
|
|
337
367
|
}
|
|
338
368
|
}
|
|
339
369
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/migrations",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.0.0-dev.
|
|
4
|
+
"version": "7.0.0-dev.78",
|
|
5
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.",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./package.json": "./package.json",
|
|
@@ -50,13 +50,13 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@mikro-orm/knex": "7.0.0-dev.
|
|
53
|
+
"@mikro-orm/knex": "7.0.0-dev.78",
|
|
54
54
|
"umzug": "3.8.2"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@mikro-orm/core": "^6.6.1"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
60
|
+
"@mikro-orm/core": "7.0.0-dev.78"
|
|
61
61
|
}
|
|
62
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';
|