@mikro-orm/migrations 7.0.0-dev.32 → 7.0.0-dev.321

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.
@@ -9,12 +9,12 @@ export class JSMigrationGenerator extends MigrationGenerator {
9
9
  ret += `const { Migration } = require('@mikro-orm/migrations');\n\n`;
10
10
  ret += `class ${className} extends Migration {\n\n`;
11
11
  ret += ` async up() {\n`;
12
- diff.up.forEach(sql => ret += this.createStatement(sql, 4));
12
+ diff.up.forEach(sql => (ret += this.createStatement(sql, 4)));
13
13
  ret += ` }\n\n`;
14
- /* v8 ignore next 5 */
14
+ /* v8 ignore next */
15
15
  if (diff.down.length > 0) {
16
16
  ret += ` async down() {\n`;
17
- diff.down.forEach(sql => ret += this.createStatement(sql, 4));
17
+ diff.down.forEach(sql => (ret += this.createStatement(sql, 4)));
18
18
  ret += ` }\n\n`;
19
19
  }
20
20
  ret += `}\n`;
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/knex';
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;
@@ -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/knex';
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;
@@ -1,5 +1,3 @@
1
- import { Utils, } from '@mikro-orm/core';
2
- import { writeFile } from 'node:fs/promises';
3
1
  export class MigrationGenerator {
4
2
  driver;
5
3
  namingStrategy;
@@ -13,15 +11,16 @@ export class MigrationGenerator {
13
11
  * @inheritDoc
14
12
  */
15
13
  async generate(diff, path, name) {
14
+ const { fs } = await import('@mikro-orm/core/fs-utils');
16
15
  /* v8 ignore next */
17
16
  const defaultPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
18
- path = Utils.normalizePath(this.driver.config.get('baseDir'), path ?? defaultPath);
19
- Utils.ensureDir(path);
20
- const timestamp = new Date().toISOString().replace(/[-T:]|\.\d{3}z$/ig, '');
17
+ path = fs.normalizePath(this.driver.config.get('baseDir'), path ?? defaultPath);
18
+ fs.ensureDir(path);
19
+ const timestamp = new Date().toISOString().replace(/[-T:]|\.\d{3}z$/gi, '');
21
20
  const className = this.namingStrategy.classToMigrationName(timestamp, name);
22
21
  const fileName = `${this.options.fileName(timestamp, name)}.${this.options.emit}`;
23
22
  const ret = await this.generateMigrationFile(className, diff);
24
- await writeFile(path + '/' + fileName, ret, { flush: true });
23
+ await fs.writeFile(path + '/' + fileName, ret, { flush: true });
25
24
  return [ret, fileName];
26
25
  }
27
26
  /**
@@ -1,5 +1,5 @@
1
1
  import { type Configuration, type MigrationsOptions, type Transaction } from '@mikro-orm/core';
2
- import type { AbstractSqlDriver } from '@mikro-orm/knex';
2
+ import type { AbstractSqlDriver } from '@mikro-orm/sql';
3
3
  import type { Migration } from './Migration.js';
4
4
  export declare class MigrationRunner {
5
5
  protected readonly driver: AbstractSqlDriver;
@@ -39,7 +39,7 @@ export class MigrationRunner {
39
39
  let queries = migration.getQueries();
40
40
  queries.unshift(...this.helper.getSchemaBeginning(charset, this.options.disableForeignKeys).split('\n'));
41
41
  queries.push(...this.helper.getSchemaEnd(this.options.disableForeignKeys).split('\n'));
42
- queries = queries.filter(sql => !Utils.isString(sql) || sql.trim().length > 0);
42
+ queries = queries.filter(sql => typeof sql !== 'string' || sql.trim().length > 0);
43
43
  return queries;
44
44
  }
45
45
  }
@@ -1,8 +1,7 @@
1
- import type { MigrationsOptions, Transaction } from '@mikro-orm/core';
2
- import { type AbstractSqlDriver } from '@mikro-orm/knex';
3
- import type { MigrationParams, UmzugStorage } from 'umzug';
1
+ import { type MigrationsOptions, type Transaction, type EntitySchema } from '@mikro-orm/core';
2
+ import { type AbstractSqlDriver } from '@mikro-orm/sql';
4
3
  import type { MigrationRow } from './typings.js';
5
- export declare class MigrationStorage implements UmzugStorage {
4
+ export declare class MigrationStorage {
6
5
  protected readonly driver: AbstractSqlDriver;
7
6
  protected readonly options: MigrationsOptions;
8
7
  private readonly connection;
@@ -11,8 +10,12 @@ export declare class MigrationStorage implements UmzugStorage {
11
10
  private readonly platform;
12
11
  constructor(driver: AbstractSqlDriver, options: MigrationsOptions);
13
12
  executed(): Promise<string[]>;
14
- logMigration(params: MigrationParams<any>): Promise<void>;
15
- unlogMigration(params: MigrationParams<any>): Promise<void>;
13
+ logMigration(params: {
14
+ name: string;
15
+ }): Promise<void>;
16
+ unlogMigration(params: {
17
+ name: string;
18
+ }): Promise<void>;
16
19
  getExecutedMigrations(): Promise<MigrationRow[]>;
17
20
  ensureTable(): Promise<void>;
18
21
  setMasterMigration(trx: Transaction): void;
@@ -27,5 +30,6 @@ export declare class MigrationStorage implements UmzugStorage {
27
30
  getTableName(): {
28
31
  tableName: string;
29
32
  schemaName: string;
33
+ entity: EntitySchema;
30
34
  };
31
35
  }
@@ -1,5 +1,5 @@
1
- import { DatabaseTable, } from '@mikro-orm/knex';
2
- import * as path from 'node:path';
1
+ import { defineEntity, p } from '@mikro-orm/core';
2
+ import { DatabaseTable, } from '@mikro-orm/sql';
3
3
  export class MigrationStorage {
4
4
  driver;
5
5
  options;
@@ -16,27 +16,28 @@ export class MigrationStorage {
16
16
  }
17
17
  async executed() {
18
18
  const migrations = await this.getExecutedMigrations();
19
- return migrations.map(({ name }) => `${this.getMigrationName(name)}`);
19
+ return migrations.map(({ name }) => this.getMigrationName(name));
20
20
  }
21
21
  async logMigration(params) {
22
- const { tableName, schemaName } = this.getTableName();
22
+ const { entity } = this.getTableName();
23
23
  const name = this.getMigrationName(params.name);
24
- await this.driver.nativeInsert(tableName, { name }, { schema: schemaName, ctx: this.masterTransaction });
24
+ await this.driver.nativeInsert(entity, { name }, { ctx: this.masterTransaction });
25
25
  }
26
26
  async unlogMigration(params) {
27
- const { tableName, schemaName } = this.getTableName();
27
+ const { entity } = this.getTableName();
28
28
  const withoutExt = this.getMigrationName(params.name);
29
29
  const names = [withoutExt, withoutExt + '.js', withoutExt + '.ts'];
30
- await this.driver.nativeDelete(tableName, { name: { $in: [params.name, ...names] } }, { schema: schemaName, ctx: this.masterTransaction });
30
+ await this.driver.nativeDelete(entity, { name: { $in: [params.name, ...names] } }, { ctx: this.masterTransaction });
31
31
  }
32
32
  async getExecutedMigrations() {
33
- const { tableName, schemaName } = this.getTableName();
34
- const res = await this.driver.createQueryBuilder(tableName, this.masterTransaction)
33
+ const { entity, schemaName } = this.getTableName();
34
+ const res = await this.driver
35
+ .createQueryBuilder(entity, this.masterTransaction)
35
36
  .withSchema(schemaName)
36
37
  .orderBy({ id: 'asc' })
37
- .execute();
38
+ .execute('all', false);
38
39
  return res.map(row => {
39
- if (typeof row.executed_at === 'string') {
40
+ if (typeof row.executed_at === 'string' || typeof row.executed_at === 'number') {
40
41
  row.executed_at = new Date(row.executed_at);
41
42
  }
42
43
  return row;
@@ -87,12 +88,7 @@ export class MigrationStorage {
87
88
  * @internal
88
89
  */
89
90
  getMigrationName(name) {
90
- const parsedName = path.parse(name);
91
- if (['.js', '.ts'].includes(parsedName.ext)) {
92
- // strip extension
93
- return parsedName.name;
94
- }
95
- return name;
91
+ return name.replace(/\.[jt]s$/, '');
96
92
  }
97
93
  /**
98
94
  * @internal
@@ -101,6 +97,17 @@ export class MigrationStorage {
101
97
  const parts = this.options.tableName.split('.');
102
98
  const tableName = parts.length > 1 ? parts[1] : parts[0];
103
99
  const schemaName = parts.length > 1 ? parts[0] : this.driver.config.get('schema', this.driver.getPlatform().getDefaultSchemaName());
104
- return { tableName, schemaName };
100
+ const entity = defineEntity({
101
+ name: 'Migration',
102
+ tableName,
103
+ schema: schemaName,
104
+ properties: {
105
+ id: p.integer().primary().fieldNames('id'),
106
+ name: p.string().fieldNames('name'),
107
+ executedAt: p.datetime().defaultRaw('current_timestamp').fieldNames('executed_at'),
108
+ },
109
+ }).init();
110
+ entity.meta.sync();
111
+ return { tableName, schemaName, entity };
105
112
  }
106
113
  }
package/Migrator.d.ts CHANGED
@@ -1,41 +1,29 @@
1
- import { type MigrationParams, type RunnableMigration } from 'umzug';
2
- import { type Constructor, type IMigrator, type MikroORM, type MigratorEvent, type MaybePromise } from '@mikro-orm/core';
3
- import { DatabaseSchema, type EntityManager } from '@mikro-orm/knex';
4
- import type { Migration } from './Migration.js';
1
+ import { type IMigrationGenerator, type IMigrationRunner, type IMigratorStorage, type MigrateOptions, type MigrationInfo, type MikroORM } from '@mikro-orm/core';
2
+ import { AbstractMigrator } from '@mikro-orm/core/migrations';
3
+ import { type AbstractSqlDriver, DatabaseSchema, type EntityManager } from '@mikro-orm/sql';
5
4
  import { MigrationStorage } from './MigrationStorage.js';
6
- import type { MigrateOptions, MigrationResult, MigrationRow, UmzugMigration } from './typings.js';
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;
5
+ import type { MigrationResult } from './typings.js';
6
+ export declare class Migrator extends AbstractMigrator<AbstractSqlDriver> {
14
7
  private readonly schemaGenerator;
15
- private readonly config;
16
- private readonly options;
17
- private readonly absolutePath;
18
- private readonly snapshotPath;
8
+ private snapshotPath?;
19
9
  constructor(em: EntityManager);
20
10
  static register(orm: MikroORM): void;
11
+ protected createRunner(): IMigrationRunner;
12
+ protected createStorage(): IMigratorStorage;
13
+ protected getDefaultGenerator(): IMigrationGenerator;
14
+ private getSnapshotPath;
15
+ protected init(): Promise<void>;
21
16
  /**
22
17
  * @inheritDoc
23
18
  */
24
- createMigration(path?: string, blank?: boolean, initial?: boolean, name?: string): Promise<MigrationResult>;
25
- checkMigrationNeeded(): Promise<boolean>;
19
+ create(path?: string, blank?: boolean, initial?: boolean, name?: string): Promise<MigrationResult>;
20
+ checkSchema(): Promise<boolean>;
26
21
  /**
27
22
  * @inheritDoc
28
23
  */
29
- createInitialMigration(path?: string, name?: string, blank?: boolean): Promise<MigrationResult>;
30
- /**
31
- * @inheritDoc
32
- */
33
- on(eventName: MigratorEvent, listener: (event: UmzugMigration) => MaybePromise<void>): this;
34
- /**
35
- * @inheritDoc
36
- */
37
- off(eventName: MigratorEvent, listener: (event: UmzugMigration) => MaybePromise<void>): this;
38
- private createUmzug;
24
+ createInitial(path?: string, name?: string, blank?: boolean): Promise<MigrationResult>;
25
+ protected runMigrations(method: 'up' | 'down', options?: string | string[] | MigrateOptions): Promise<MigrationInfo[]>;
26
+ getStorage(): MigrationStorage;
39
27
  /**
40
28
  * Initial migration can be created only if:
41
29
  * 1. no previous migrations were generated or executed
@@ -45,32 +33,7 @@ export declare class Migrator implements IMigrator {
45
33
  * If only some of the tables are present, exception is thrown.
46
34
  */
47
35
  private validateInitialMigration;
48
- /**
49
- * @inheritDoc
50
- */
51
- getExecutedMigrations(): Promise<MigrationRow[]>;
52
- private ensureDatabase;
53
- /**
54
- * @inheritDoc
55
- */
56
- getPendingMigrations(): Promise<UmzugMigration[]>;
57
- /**
58
- * @inheritDoc
59
- */
60
- up(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
61
- /**
62
- * @inheritDoc
63
- */
64
- down(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
65
- getStorage(): MigrationStorage;
66
- protected resolve(params: MigrationParams<any>): RunnableMigration<any>;
67
- protected getSchemaFromSnapshot(): DatabaseSchema | undefined;
68
- protected storeCurrentSchema(): Promise<void>;
69
- protected initialize(MigrationClass: Constructor<Migration>, name: string): RunnableMigration<any>;
36
+ protected getSchemaFromSnapshot(): Promise<DatabaseSchema | undefined>;
37
+ protected storeCurrentSchema(schema?: DatabaseSchema): Promise<void>;
70
38
  private getSchemaDiff;
71
- private getMigrationFilename;
72
- private prefix;
73
- private runMigrations;
74
- private runInTransaction;
75
- private ensureMigrationsDirExists;
76
39
  }