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

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/Migration.d.ts CHANGED
@@ -2,11 +2,10 @@ import { type Configuration, type RawQueryFragment, type Transaction } from '@mi
2
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
+ #private;
5
6
  protected readonly driver: AbstractSqlDriver;
6
7
  protected readonly config: Configuration;
7
- private readonly queries;
8
8
  protected ctx?: Transaction;
9
- private em?;
10
9
  constructor(driver: AbstractSqlDriver, config: Configuration);
11
10
  abstract up(): Promise<void> | void;
12
11
  down(): Promise<void> | void;
package/Migration.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export class Migration {
2
2
  driver;
3
3
  config;
4
- queries = [];
4
+ #queries = [];
5
5
  ctx;
6
- em;
6
+ #em;
7
7
  constructor(driver, config) {
8
8
  this.driver = driver;
9
9
  this.config = config;
@@ -15,10 +15,10 @@ export class Migration {
15
15
  return true;
16
16
  }
17
17
  addSql(sql) {
18
- this.queries.push(sql);
18
+ this.#queries.push(sql);
19
19
  }
20
20
  reset() {
21
- this.queries.length = 0;
21
+ this.#queries.length = 0;
22
22
  this.ctx = undefined;
23
23
  }
24
24
  setTransactionContext(ctx) {
@@ -36,13 +36,13 @@ export class Migration {
36
36
  * the current transaction context.
37
37
  */
38
38
  getEntityManager() {
39
- if (!this.em) {
40
- this.em = this.driver.createEntityManager();
41
- this.em.setTransactionContext(this.ctx);
39
+ if (!this.#em) {
40
+ this.#em = this.driver.createEntityManager();
41
+ this.#em.setTransactionContext(this.ctx);
42
42
  }
43
- return this.em;
43
+ return this.#em;
44
44
  }
45
45
  getQueries() {
46
- return this.queries;
46
+ return this.#queries;
47
47
  }
48
48
  }
@@ -2,12 +2,10 @@ import { type Configuration, type MigrationsOptions, type Transaction } from '@m
2
2
  import type { AbstractSqlDriver } from '@mikro-orm/sql';
3
3
  import type { Migration } from './Migration.js';
4
4
  export declare class MigrationRunner {
5
+ #private;
5
6
  protected readonly driver: AbstractSqlDriver;
6
7
  protected readonly options: MigrationsOptions;
7
8
  protected readonly config: Configuration;
8
- private readonly connection;
9
- private readonly helper;
10
- private masterTransaction?;
11
9
  constructor(driver: AbstractSqlDriver, options: MigrationsOptions, config: Configuration);
12
10
  run(migration: Migration, method: 'up' | 'down'): Promise<void>;
13
11
  setMasterMigration(trx: Transaction): void;
@@ -3,15 +3,15 @@ export class MigrationRunner {
3
3
  driver;
4
4
  options;
5
5
  config;
6
- connection;
7
- helper;
8
- masterTransaction;
6
+ #connection;
7
+ #helper;
8
+ #masterTransaction;
9
9
  constructor(driver, options, config) {
10
10
  this.driver = driver;
11
11
  this.options = options;
12
12
  this.config = config;
13
- this.connection = this.driver.getConnection();
14
- this.helper = this.driver.getPlatform().getSchemaHelper();
13
+ this.#connection = this.driver.getConnection();
14
+ this.#helper = this.driver.getPlatform().getSchemaHelper();
15
15
  }
16
16
  async run(migration, method) {
17
17
  migration.reset();
@@ -20,25 +20,25 @@ export class MigrationRunner {
20
20
  await Utils.runSerial(queries, sql => this.driver.execute(sql));
21
21
  }
22
22
  else {
23
- await this.connection.transactional(async (tx) => {
23
+ await this.#connection.transactional(async (tx) => {
24
24
  migration.setTransactionContext(tx);
25
25
  const queries = await this.getQueries(migration, method);
26
26
  await Utils.runSerial(queries, sql => this.driver.execute(sql, undefined, 'all', tx));
27
- }, { ctx: this.masterTransaction });
27
+ }, { ctx: this.#masterTransaction });
28
28
  }
29
29
  }
30
30
  setMasterMigration(trx) {
31
- this.masterTransaction = trx;
31
+ this.#masterTransaction = trx;
32
32
  }
33
33
  unsetMasterMigration() {
34
- delete this.masterTransaction;
34
+ this.#masterTransaction = undefined;
35
35
  }
36
36
  async getQueries(migration, method) {
37
37
  await migration[method]();
38
38
  const charset = this.config.get('charset');
39
39
  let queries = migration.getQueries();
40
- queries.unshift(...this.helper.getSchemaBeginning(charset, this.options.disableForeignKeys).split('\n'));
41
- queries.push(...this.helper.getSchemaEnd(this.options.disableForeignKeys).split('\n'));
40
+ queries.unshift(...this.#helper.getSchemaBeginning(charset, this.options.disableForeignKeys).split('\n'));
41
+ queries.push(...this.#helper.getSchemaEnd(this.options.disableForeignKeys).split('\n'));
42
42
  queries = queries.filter(sql => typeof sql !== 'string' || sql.trim().length > 0);
43
43
  return queries;
44
44
  }
@@ -2,12 +2,9 @@ import { type MigrationsOptions, type Transaction, type EntitySchema } from '@mi
2
2
  import { type AbstractSqlDriver } from '@mikro-orm/sql';
3
3
  import type { MigrationRow } from './typings.js';
4
4
  export declare class MigrationStorage {
5
+ #private;
5
6
  protected readonly driver: AbstractSqlDriver;
6
7
  protected readonly options: MigrationsOptions;
7
- private readonly connection;
8
- private readonly helper;
9
- private masterTransaction?;
10
- private readonly platform;
11
8
  constructor(driver: AbstractSqlDriver, options: MigrationsOptions);
12
9
  executed(): Promise<string[]>;
13
10
  logMigration(params: {
@@ -3,16 +3,16 @@ import { DatabaseTable, } from '@mikro-orm/sql';
3
3
  export class MigrationStorage {
4
4
  driver;
5
5
  options;
6
- connection;
7
- helper;
8
- masterTransaction;
9
- platform;
6
+ #connection;
7
+ #helper;
8
+ #masterTransaction;
9
+ #platform;
10
10
  constructor(driver, options) {
11
11
  this.driver = driver;
12
12
  this.options = options;
13
- this.connection = this.driver.getConnection();
14
- this.platform = this.driver.getPlatform();
15
- this.helper = this.platform.getSchemaHelper();
13
+ this.#connection = this.driver.getConnection();
14
+ this.#platform = this.driver.getPlatform();
15
+ this.#helper = this.#platform.getSchemaHelper();
16
16
  }
17
17
  async executed() {
18
18
  const migrations = await this.getExecutedMigrations();
@@ -21,18 +21,18 @@ export class MigrationStorage {
21
21
  async logMigration(params) {
22
22
  const { entity } = this.getTableName();
23
23
  const name = this.getMigrationName(params.name);
24
- await this.driver.nativeInsert(entity, { name }, { ctx: this.masterTransaction });
24
+ await this.driver.nativeInsert(entity, { name }, { ctx: this.#masterTransaction });
25
25
  }
26
26
  async unlogMigration(params) {
27
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(entity, { name: { $in: [params.name, ...names] } }, { ctx: this.masterTransaction });
30
+ await this.driver.nativeDelete(entity, { name: { $in: [params.name, ...names] } }, { ctx: this.#masterTransaction });
31
31
  }
32
32
  async getExecutedMigrations() {
33
33
  const { entity, schemaName } = this.getTableName();
34
34
  const res = await this.driver
35
- .createQueryBuilder(entity, this.masterTransaction)
35
+ .createQueryBuilder(entity, this.#masterTransaction)
36
36
  .withSchema(schemaName)
37
37
  .orderBy({ id: 'asc' })
38
38
  .execute('all', false);
@@ -44,45 +44,45 @@ export class MigrationStorage {
44
44
  });
45
45
  }
46
46
  async ensureTable() {
47
- const tables = await this.connection.execute(this.helper.getListTablesSQL(), [], 'all', this.masterTransaction);
47
+ const tables = await this.#connection.execute(this.#helper.getListTablesSQL(), [], 'all', this.#masterTransaction);
48
48
  const { tableName, schemaName } = this.getTableName();
49
49
  if (tables.find(t => t.table_name === tableName && (!t.schema_name || t.schema_name === schemaName))) {
50
50
  return;
51
51
  }
52
- const schemas = await this.helper.getNamespaces(this.connection);
52
+ const schemas = await this.#helper.getNamespaces(this.#connection);
53
53
  if (schemaName && !schemas.includes(schemaName)) {
54
- const sql = this.helper.getCreateNamespaceSQL(schemaName);
55
- await this.connection.execute(sql);
54
+ const sql = this.#helper.getCreateNamespaceSQL(schemaName);
55
+ await this.#connection.execute(sql);
56
56
  }
57
- const table = new DatabaseTable(this.platform, tableName, schemaName);
57
+ const table = new DatabaseTable(this.#platform, tableName, schemaName);
58
58
  table.addColumn({
59
59
  name: 'id',
60
- type: this.platform.getIntegerTypeDeclarationSQL({ autoincrement: true, unsigned: true }),
61
- mappedType: this.platform.getMappedType('number'),
60
+ type: this.#platform.getIntegerTypeDeclarationSQL({ autoincrement: true, unsigned: true }),
61
+ mappedType: this.#platform.getMappedType('number'),
62
62
  primary: true,
63
63
  autoincrement: true,
64
64
  });
65
65
  table.addColumn({
66
66
  name: 'name',
67
- type: this.platform.getVarcharTypeDeclarationSQL({}),
68
- mappedType: this.platform.getMappedType('string'),
67
+ type: this.#platform.getVarcharTypeDeclarationSQL({}),
68
+ mappedType: this.#platform.getMappedType('string'),
69
69
  });
70
- const length = this.platform.getDefaultDateTimeLength();
70
+ const length = this.#platform.getDefaultDateTimeLength();
71
71
  table.addColumn({
72
72
  name: 'executed_at',
73
- type: this.platform.getDateTimeTypeDeclarationSQL({ length }),
74
- mappedType: this.platform.getMappedType('datetime'),
75
- default: this.platform.getCurrentTimestampSQL(length),
73
+ type: this.#platform.getDateTimeTypeDeclarationSQL({ length }),
74
+ mappedType: this.#platform.getMappedType('datetime'),
75
+ default: this.#platform.getCurrentTimestampSQL(length),
76
76
  length,
77
77
  });
78
- const sql = this.helper.createTable(table);
79
- await this.connection.execute(sql.join(';\n'), [], 'run', this.masterTransaction);
78
+ const sql = this.#helper.createTable(table);
79
+ await this.#connection.execute(sql.join(';\n'), [], 'run', this.#masterTransaction);
80
80
  }
81
81
  setMasterMigration(trx) {
82
- this.masterTransaction = trx;
82
+ this.#masterTransaction = trx;
83
83
  }
84
84
  unsetMasterMigration() {
85
- delete this.masterTransaction;
85
+ this.#masterTransaction = undefined;
86
86
  }
87
87
  /**
88
88
  * @internal
package/Migrator.d.ts CHANGED
@@ -4,8 +4,7 @@ import { type AbstractSqlDriver, DatabaseSchema, type EntityManager } from '@mik
4
4
  import { MigrationStorage } from './MigrationStorage.js';
5
5
  import type { MigrationResult } from './typings.js';
6
6
  export declare class Migrator extends AbstractMigrator<AbstractSqlDriver> {
7
- private readonly schemaGenerator;
8
- private snapshotPath?;
7
+ #private;
9
8
  constructor(em: EntityManager);
10
9
  static register(orm: MikroORM): void;
11
10
  protected createRunner(): IMigrationRunner;
package/Migrator.js CHANGED
@@ -6,11 +6,11 @@ import { MigrationStorage } from './MigrationStorage.js';
6
6
  import { TSMigrationGenerator } from './TSMigrationGenerator.js';
7
7
  import { JSMigrationGenerator } from './JSMigrationGenerator.js';
8
8
  export class Migrator extends AbstractMigrator {
9
- schemaGenerator;
10
- snapshotPath;
9
+ #schemaGenerator;
10
+ #snapshotPath;
11
11
  constructor(em) {
12
12
  super(em);
13
- this.schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
13
+ this.#schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
14
14
  }
15
15
  static register(orm) {
16
16
  orm.config.registerExtension('@mikro-orm/migrator', () => new Migrator(orm.em));
@@ -28,7 +28,7 @@ export class Migrator extends AbstractMigrator {
28
28
  return new TSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
29
29
  }
30
30
  async getSnapshotPath() {
31
- if (!this.snapshotPath) {
31
+ if (!this.#snapshotPath) {
32
32
  const { fs } = await import('@mikro-orm/core/fs-utils');
33
33
  // for snapshots, we always want to use the path based on `emit` option, regardless of whether we run in TS context
34
34
  /* v8 ignore next */
@@ -36,16 +36,16 @@ export class Migrator extends AbstractMigrator {
36
36
  const absoluteSnapshotPath = fs.absolutePath(snapshotPath, this.config.get('baseDir'));
37
37
  const dbName = this.config.get('dbName').replace(/\\/g, '/').split('/').pop().replace(/:/g, '');
38
38
  const snapshotName = this.options.snapshotName ?? `.snapshot-${dbName}`;
39
- this.snapshotPath = fs.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
39
+ this.#snapshotPath = fs.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
40
40
  }
41
- return this.snapshotPath;
41
+ return this.#snapshotPath;
42
42
  }
43
43
  async init() {
44
44
  if (this.initialized) {
45
45
  return;
46
46
  }
47
47
  await super.init();
48
- const created = await this.schemaGenerator.ensureDatabase();
48
+ const created = await this.#schemaGenerator.ensureDatabase();
49
49
  /* v8 ignore next */
50
50
  if (created) {
51
51
  this.initServices();
@@ -162,18 +162,35 @@ export class Migrator extends AbstractMigrator {
162
162
  const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
163
163
  const { tables, namespaces, ...rest } = data;
164
164
  const tableInstances = tables.map((tbl) => {
165
- const table = new DatabaseTable(this.driver.getPlatform(), tbl.name);
166
- const { columns, ...restTable } = tbl;
167
- Object.assign(table, restTable);
168
- Object.keys(columns).forEach(col => {
169
- const column = { ...columns[col] };
165
+ const table = new DatabaseTable(this.driver.getPlatform(), tbl.name, tbl.schema);
166
+ table.nativeEnums = tbl.nativeEnums ?? {};
167
+ table.comment = tbl.comment;
168
+ if (tbl.indexes) {
169
+ table.setIndexes(tbl.indexes);
170
+ }
171
+ if (tbl.checks) {
172
+ table.setChecks(tbl.checks);
173
+ }
174
+ if (tbl.foreignKeys) {
175
+ table.setForeignKeys(tbl.foreignKeys);
176
+ }
177
+ const cols = tbl.columns;
178
+ Object.keys(cols).forEach(col => {
179
+ const column = { ...cols[col] };
170
180
  /* v8 ignore next */
171
- column.mappedType = Type.getType(t[columns[col].mappedType] ?? UnknownType);
181
+ column.mappedType = Type.getType(t[cols[col].mappedType] ?? UnknownType);
172
182
  table.addColumn(column);
173
183
  });
174
184
  return table;
175
185
  });
176
- Object.assign(schema, { tables: tableInstances, namespaces: new Set(namespaces), ...rest });
186
+ schema.setTables(tableInstances);
187
+ schema.setNamespaces(new Set(namespaces));
188
+ if (rest.nativeEnums) {
189
+ schema.setNativeEnums(rest.nativeEnums);
190
+ }
191
+ if (rest.views) {
192
+ schema.setViews(rest.views);
193
+ }
177
194
  return schema;
178
195
  }
179
196
  async storeCurrentSchema(schema) {
@@ -181,7 +198,7 @@ export class Migrator extends AbstractMigrator {
181
198
  return;
182
199
  }
183
200
  const snapshotPath = await this.getSnapshotPath();
184
- schema ??= this.schemaGenerator.getTargetSchema();
201
+ schema ??= this.#schemaGenerator.getTargetSchema();
185
202
  const { fs } = await import('@mikro-orm/core/fs-utils');
186
203
  await fs.writeFile(snapshotPath, JSON.stringify(schema, null, 2));
187
204
  }
@@ -218,11 +235,11 @@ export class Migrator extends AbstractMigrator {
218
235
  down.push('select 1');
219
236
  }
220
237
  else if (initial) {
221
- const dump = await this.schemaGenerator.getCreateSchemaSQL({ wrap: false });
238
+ const dump = await this.#schemaGenerator.getCreateSchemaSQL({ wrap: false });
222
239
  up.push(...splitStatements(dump));
223
240
  }
224
241
  else {
225
- const diff = await this.schemaGenerator.getUpdateSchemaMigrationSQL({
242
+ const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
226
243
  wrap: false,
227
244
  safe: this.options.safe,
228
245
  dropTables: this.options.dropTables,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/migrations",
3
- "version": "7.0.0-dev.321",
3
+ "version": "7.0.0-dev.323",
4
4
  "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
5
  "keywords": [
6
6
  "data-mapper",
@@ -47,13 +47,13 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.0.0-dev.321"
50
+ "@mikro-orm/sql": "7.0.0-dev.323"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@mikro-orm/core": "^6.6.9"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.0.0-dev.321"
56
+ "@mikro-orm/core": "7.0.0-dev.323"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"