@mikro-orm/sql 7.0.0-rc.1 → 7.0.0-rc.3
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/AbstractSqlConnection.js +2 -1
- package/AbstractSqlDriver.d.ts +18 -12
- package/AbstractSqlDriver.js +187 -38
- package/AbstractSqlPlatform.d.ts +1 -0
- package/AbstractSqlPlatform.js +5 -3
- package/PivotCollectionPersister.js +2 -2
- package/SqlEntityManager.d.ts +5 -4
- package/SqlEntityManager.js +5 -5
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +2 -0
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +8 -4
- package/dialects/mysql/BaseMySqlPlatform.js +1 -2
- package/dialects/mysql/MySqlSchemaHelper.js +21 -10
- package/dialects/postgresql/BasePostgreSqlPlatform.js +38 -30
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +63 -47
- package/dialects/sqlite/BaseSqliteConnection.d.ts +4 -1
- package/dialects/sqlite/BaseSqliteConnection.js +4 -0
- package/dialects/sqlite/NodeSqliteDialect.d.ts +21 -0
- package/dialects/sqlite/NodeSqliteDialect.js +43 -0
- package/dialects/sqlite/SqliteDriver.d.ts +12 -0
- package/dialects/sqlite/SqliteDriver.js +14 -0
- package/dialects/sqlite/{BaseSqlitePlatform.d.ts → SqlitePlatform.d.ts} +5 -2
- package/dialects/sqlite/{BaseSqlitePlatform.js → SqlitePlatform.js} +30 -4
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +5 -0
- package/dialects/sqlite/SqliteSchemaHelper.js +31 -10
- package/dialects/sqlite/index.d.ts +3 -1
- package/dialects/sqlite/index.js +3 -1
- package/package.json +30 -30
- package/plugin/transformer.js +17 -16
- package/query/CriteriaNode.js +28 -10
- package/query/CriteriaNodeFactory.js +5 -1
- package/query/NativeQueryBuilder.d.ts +25 -0
- package/query/NativeQueryBuilder.js +61 -1
- package/query/ObjectCriteriaNode.js +71 -27
- package/query/QueryBuilder.d.ts +177 -48
- package/query/QueryBuilder.js +233 -54
- package/query/QueryBuilderHelper.d.ts +4 -3
- package/query/QueryBuilderHelper.js +47 -17
- package/query/ScalarCriteriaNode.js +14 -7
- package/query/raw.js +1 -1
- package/schema/DatabaseSchema.js +21 -15
- package/schema/DatabaseTable.js +114 -54
- package/schema/SchemaComparator.js +56 -32
- package/schema/SchemaHelper.js +28 -8
- package/schema/SqlSchemaGenerator.d.ts +2 -1
- package/schema/SqlSchemaGenerator.js +15 -8
- package/tsconfig.build.tsbuildinfo +1 -1
- package/typings.d.ts +15 -4
|
@@ -59,7 +59,9 @@ export class SchemaComparator {
|
|
|
59
59
|
if (toSchema.hasNativeEnum(key)) {
|
|
60
60
|
continue;
|
|
61
61
|
}
|
|
62
|
-
if (key.startsWith(`${fromSchema.name}.`) &&
|
|
62
|
+
if (key.startsWith(`${fromSchema.name}.`) &&
|
|
63
|
+
(fromSchema.name !== toSchema.name ||
|
|
64
|
+
toSchema.getNativeEnum(key.substring(fromSchema.name.length + 1))?.schema === '*')) {
|
|
63
65
|
continue;
|
|
64
66
|
}
|
|
65
67
|
diff.removedNativeEnums.push(nativeEnum);
|
|
@@ -164,7 +166,10 @@ export class SchemaComparator {
|
|
|
164
166
|
};
|
|
165
167
|
if (this.diffComment(fromTable.comment, toTable.comment)) {
|
|
166
168
|
tableDifferences.changedComment = toTable.comment;
|
|
167
|
-
this.log(`table comment changed for ${tableDifferences.name}`, {
|
|
169
|
+
this.log(`table comment changed for ${tableDifferences.name}`, {
|
|
170
|
+
fromTableComment: fromTable.comment,
|
|
171
|
+
toTableComment: toTable.comment,
|
|
172
|
+
});
|
|
168
173
|
changes++;
|
|
169
174
|
}
|
|
170
175
|
const fromTableColumns = fromTable.getColumns();
|
|
@@ -222,7 +227,7 @@ export class SchemaComparator {
|
|
|
222
227
|
// See if there are any removed indexes in "to" table
|
|
223
228
|
for (const index of fromTableIndexes) {
|
|
224
229
|
// See if index is removed in "to" table.
|
|
225
|
-
if ((index.primary && !toTable.hasPrimaryKey()) || !index.primary && !toTable.hasIndex(index.keyName)) {
|
|
230
|
+
if ((index.primary && !toTable.hasPrimaryKey()) || (!index.primary && !toTable.hasIndex(index.keyName))) {
|
|
226
231
|
tableDifferences.removedIndexes[index.keyName] = index;
|
|
227
232
|
this.log(`index ${index.keyName} removed from table ${tableDifferences.name}`);
|
|
228
233
|
changes++;
|
|
@@ -234,7 +239,10 @@ export class SchemaComparator {
|
|
|
234
239
|
continue;
|
|
235
240
|
}
|
|
236
241
|
tableDifferences.changedIndexes[index.keyName] = toTableIndex;
|
|
237
|
-
this.log(`index ${index.keyName} changed in table ${tableDifferences.name}`, {
|
|
242
|
+
this.log(`index ${index.keyName} changed in table ${tableDifferences.name}`, {
|
|
243
|
+
fromTableIndex: index,
|
|
244
|
+
toTableIndex,
|
|
245
|
+
});
|
|
238
246
|
changes++;
|
|
239
247
|
}
|
|
240
248
|
this.detectIndexRenamings(tableDifferences);
|
|
@@ -264,10 +272,15 @@ export class SchemaComparator {
|
|
|
264
272
|
if (!this.diffExpression(check.expression, toTableCheck.expression)) {
|
|
265
273
|
continue;
|
|
266
274
|
}
|
|
267
|
-
if (fromColumn?.enumItems &&
|
|
275
|
+
if (fromColumn?.enumItems &&
|
|
276
|
+
toColumn?.enumItems &&
|
|
277
|
+
!this.diffEnumItems(fromColumn.enumItems, toColumn.enumItems)) {
|
|
268
278
|
continue;
|
|
269
279
|
}
|
|
270
|
-
this.log(`check constraint ${check.name} changed in table ${tableDifferences.name}`, {
|
|
280
|
+
this.log(`check constraint ${check.name} changed in table ${tableDifferences.name}`, {
|
|
281
|
+
fromTableCheck: check,
|
|
282
|
+
toTableCheck,
|
|
283
|
+
});
|
|
271
284
|
tableDifferences.changedChecks[check.name] = toTableCheck;
|
|
272
285
|
changes++;
|
|
273
286
|
}
|
|
@@ -280,7 +293,10 @@ export class SchemaComparator {
|
|
|
280
293
|
delete toForeignKeys[toConstraint.constraintName];
|
|
281
294
|
}
|
|
282
295
|
else if (fromConstraint.constraintName.toLowerCase() === toConstraint.constraintName.toLowerCase()) {
|
|
283
|
-
this.log(`FK constraint ${fromConstraint.constraintName} changed in table ${tableDifferences.name}`, {
|
|
296
|
+
this.log(`FK constraint ${fromConstraint.constraintName} changed in table ${tableDifferences.name}`, {
|
|
297
|
+
fromConstraint,
|
|
298
|
+
toConstraint,
|
|
299
|
+
});
|
|
284
300
|
tableDifferences.changedForeignKeys[toConstraint.constraintName] = toConstraint;
|
|
285
301
|
changes++;
|
|
286
302
|
delete fromForeignKeys[fromConstraint.constraintName];
|
|
@@ -295,7 +311,9 @@ export class SchemaComparator {
|
|
|
295
311
|
}
|
|
296
312
|
for (const toConstraint of Object.values(toForeignKeys)) {
|
|
297
313
|
tableDifferences.addedForeignKeys[toConstraint.constraintName] = toConstraint;
|
|
298
|
-
this.log(`FK constraint ${toConstraint.constraintName} added to table ${tableDifferences.name}`, {
|
|
314
|
+
this.log(`FK constraint ${toConstraint.constraintName} added to table ${tableDifferences.name}`, {
|
|
315
|
+
constraint: toConstraint,
|
|
316
|
+
});
|
|
299
317
|
changes++;
|
|
300
318
|
}
|
|
301
319
|
return changes ? tableDifferences : false;
|
|
@@ -341,7 +359,10 @@ export class SchemaComparator {
|
|
|
341
359
|
tableDifferences.renamedColumns[removedColumnName] = addedColumn;
|
|
342
360
|
delete tableDifferences.addedColumns[addedColumnName];
|
|
343
361
|
delete tableDifferences.removedColumns[removedColumnName];
|
|
344
|
-
this.log(`renamed column detected in table ${tableDifferences.name}`, {
|
|
362
|
+
this.log(`renamed column detected in table ${tableDifferences.name}`, {
|
|
363
|
+
old: removedColumnName,
|
|
364
|
+
new: addedColumnName,
|
|
365
|
+
});
|
|
345
366
|
}
|
|
346
367
|
}
|
|
347
368
|
/**
|
|
@@ -375,7 +396,10 @@ export class SchemaComparator {
|
|
|
375
396
|
tableDifferences.renamedIndexes[removedIndexName] = addedIndex;
|
|
376
397
|
delete tableDifferences.addedIndexes[addedIndexName];
|
|
377
398
|
delete tableDifferences.removedIndexes[removedIndexName];
|
|
378
|
-
this.log(`renamed index detected in table ${tableDifferences.name}`, {
|
|
399
|
+
this.log(`renamed index detected in table ${tableDifferences.name}`, {
|
|
400
|
+
old: removedIndexName,
|
|
401
|
+
new: addedIndexName,
|
|
402
|
+
});
|
|
379
403
|
}
|
|
380
404
|
}
|
|
381
405
|
diffForeignKey(key1, key2, tableDifferences) {
|
|
@@ -402,10 +426,7 @@ export class SchemaComparator {
|
|
|
402
426
|
}
|
|
403
427
|
const defaultRule = ['restrict', 'no action'];
|
|
404
428
|
const rule = (key, method) => {
|
|
405
|
-
return (key[method] ?? defaultRule[0])
|
|
406
|
-
.toLowerCase()
|
|
407
|
-
.replace(defaultRule[1], defaultRule[0])
|
|
408
|
-
.replace(/"/g, '');
|
|
429
|
+
return (key[method] ?? defaultRule[0]).toLowerCase().replace(defaultRule[1], defaultRule[0]).replace(/"/g, '');
|
|
409
430
|
};
|
|
410
431
|
const compare = (method) => rule(key1, method) === rule(key2, method);
|
|
411
432
|
return !compare('updateRule') || !compare('deleteRule');
|
|
@@ -418,7 +439,8 @@ export class SchemaComparator {
|
|
|
418
439
|
const fromProp = this.mapColumnToProperty({ ...fromColumn, autoincrement: false });
|
|
419
440
|
const toProp = this.mapColumnToProperty({ ...toColumn, autoincrement: false });
|
|
420
441
|
const fromColumnType = this.platform.normalizeColumnType(fromColumn.mappedType.getColumnType(fromProp, this.platform).toLowerCase(), fromProp);
|
|
421
|
-
const fromNativeEnum = fromTable.nativeEnums[fromColumnType] ??
|
|
442
|
+
const fromNativeEnum = fromTable.nativeEnums[fromColumnType] ??
|
|
443
|
+
Object.values(fromTable.nativeEnums).find(e => e.name === fromColumnType && e.schema !== '*');
|
|
422
444
|
let toColumnType = this.platform.normalizeColumnType(toColumn.mappedType.getColumnType(toProp, this.platform).toLowerCase(), toProp);
|
|
423
445
|
const log = (msg, params) => {
|
|
424
446
|
if (logging) {
|
|
@@ -430,8 +452,11 @@ export class SchemaComparator {
|
|
|
430
452
|
if (fromColumnType !== toColumnType &&
|
|
431
453
|
(!fromNativeEnum || `${fromNativeEnum.schema}.${fromNativeEnum.name}` !== toColumnType) &&
|
|
432
454
|
!(fromColumn.ignoreSchemaChanges?.includes('type') || toColumn.ignoreSchemaChanges?.includes('type')) &&
|
|
433
|
-
!fromColumn.generated &&
|
|
434
|
-
|
|
455
|
+
!fromColumn.generated &&
|
|
456
|
+
!toColumn.generated) {
|
|
457
|
+
if (!toColumnType.includes('.') &&
|
|
458
|
+
fromTable.schema &&
|
|
459
|
+
fromTable.schema !== this.platform.getDefaultSchemaName()) {
|
|
435
460
|
toColumnType = `${fromTable.schema}.${toColumnType}`;
|
|
436
461
|
}
|
|
437
462
|
if (fromColumnType !== toColumnType) {
|
|
@@ -439,7 +464,7 @@ export class SchemaComparator {
|
|
|
439
464
|
changedProperties.add('type');
|
|
440
465
|
}
|
|
441
466
|
}
|
|
442
|
-
if (fromColumn.nullable !== toColumn.nullable && !fromColumn.generated && !toColumn.generated) {
|
|
467
|
+
if (!!fromColumn.nullable !== !!toColumn.nullable && !fromColumn.generated && !toColumn.generated) {
|
|
443
468
|
log(`'nullable' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
444
469
|
changedProperties.add('nullable');
|
|
445
470
|
}
|
|
@@ -451,12 +476,12 @@ export class SchemaComparator {
|
|
|
451
476
|
log(`'autoincrement' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
452
477
|
changedProperties.add('autoincrement');
|
|
453
478
|
}
|
|
454
|
-
if (fromColumn.unsigned !== toColumn.unsigned && this.platform.supportsUnsigned()) {
|
|
479
|
+
if (!!fromColumn.unsigned !== !!toColumn.unsigned && this.platform.supportsUnsigned()) {
|
|
455
480
|
log(`'unsigned' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
456
481
|
changedProperties.add('unsigned');
|
|
457
482
|
}
|
|
458
|
-
if (!(fromColumn.ignoreSchemaChanges?.includes('default') ||
|
|
459
|
-
|
|
483
|
+
if (!(fromColumn.ignoreSchemaChanges?.includes('default') || toColumn.ignoreSchemaChanges?.includes('default')) &&
|
|
484
|
+
!this.hasSameDefaultValue(fromColumn, toColumn)) {
|
|
460
485
|
log(`'default' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
461
486
|
changedProperties.add('default');
|
|
462
487
|
}
|
|
@@ -471,8 +496,7 @@ export class SchemaComparator {
|
|
|
471
496
|
changedProperties.add('enumItems');
|
|
472
497
|
}
|
|
473
498
|
if ((fromColumn.extra || '').toLowerCase() !== (toColumn.extra || '').toLowerCase() &&
|
|
474
|
-
!(fromColumn.ignoreSchemaChanges?.includes('extra') ||
|
|
475
|
-
toColumn.ignoreSchemaChanges?.includes('extra'))) {
|
|
499
|
+
!(fromColumn.ignoreSchemaChanges?.includes('extra') || toColumn.ignoreSchemaChanges?.includes('extra'))) {
|
|
476
500
|
log(`'extra' changed for column ${fromTable.name}.${fromColumn.name}`, { fromColumn, toColumn });
|
|
477
501
|
changedProperties.add('extra');
|
|
478
502
|
}
|
|
@@ -579,7 +603,7 @@ export class SchemaComparator {
|
|
|
579
603
|
if (sort1 !== sort2) {
|
|
580
604
|
return false;
|
|
581
605
|
}
|
|
582
|
-
const defaultNulls = (s) => s === 'DESC' ? 'FIRST' : 'LAST';
|
|
606
|
+
const defaultNulls = (s) => (s === 'DESC' ? 'FIRST' : 'LAST');
|
|
583
607
|
const nulls1 = c1.nulls?.toUpperCase() ?? defaultNulls(sort1);
|
|
584
608
|
const nulls2 = c2.nulls?.toUpperCase() ?? defaultNulls(sort2);
|
|
585
609
|
if (nulls1 !== nulls2) {
|
|
@@ -610,9 +634,9 @@ export class SchemaComparator {
|
|
|
610
634
|
// expressions like check constraints might be normalized by the driver,
|
|
611
635
|
// e.g. quotes might be added (https://github.com/mikro-orm/mikro-orm/issues/3827)
|
|
612
636
|
const simplify = (str) => {
|
|
613
|
-
return str
|
|
637
|
+
return (str
|
|
614
638
|
?.replace(/_\w+'(.*?)'/g, '$1')
|
|
615
|
-
.replace(/in\s*\((.*?)\)/
|
|
639
|
+
.replace(/in\s*\((.*?)\)/gi, '= any (array[$1])')
|
|
616
640
|
// MySQL normalizes count(*) to count(0)
|
|
617
641
|
.replace(/\bcount\s*\(\s*0\s*\)/gi, 'count(*)')
|
|
618
642
|
// Remove quotes first so we can process identifiers
|
|
@@ -628,7 +652,7 @@ export class SchemaComparator {
|
|
|
628
652
|
.replace(/\bas\b/gi, '')
|
|
629
653
|
// Remove remaining special chars, parentheses, type casts, asterisks, and normalize whitespace
|
|
630
654
|
.replace(/[()\n[\]*]|::\w+| +/g, '')
|
|
631
|
-
.replace(/anyarray\[(.*)]/
|
|
655
|
+
.replace(/anyarray\[(.*)]/gi, '$1')
|
|
632
656
|
.toLowerCase()
|
|
633
657
|
// PostgreSQL adds default aliases to aggregate functions (e.g., count(*) AS count)
|
|
634
658
|
// After removing AS and whitespace, this results in duplicate adjacent words
|
|
@@ -636,7 +660,7 @@ export class SchemaComparator {
|
|
|
636
660
|
// Use lookahead to match repeated patterns of 3+ chars (avoid false positives on short sequences)
|
|
637
661
|
.replace(/(\w{3,})\1/g, '$1')
|
|
638
662
|
// Remove trailing semicolon (PostgreSQL adds it to view definitions)
|
|
639
|
-
.replace(/;$/, '');
|
|
663
|
+
.replace(/;$/, ''));
|
|
640
664
|
};
|
|
641
665
|
return simplify(expr1) !== simplify(expr2);
|
|
642
666
|
}
|
|
@@ -645,13 +669,13 @@ export class SchemaComparator {
|
|
|
645
669
|
if (!defaultValue) {
|
|
646
670
|
return null;
|
|
647
671
|
}
|
|
648
|
-
const val = defaultValue
|
|
649
|
-
.replace(/^(_\w+\\)?'(.*?)\\?'$/, '$2')
|
|
650
|
-
.replace(/^\(?'(.*?)'\)?$/, '$1');
|
|
672
|
+
const val = defaultValue.replace(/^(_\w+\\)?'(.*?)\\?'$/, '$2').replace(/^\(?'(.*?)'\)?$/, '$1');
|
|
651
673
|
return parseJsonSafe(val);
|
|
652
674
|
}
|
|
653
675
|
hasSameDefaultValue(from, to) {
|
|
654
|
-
if (from.default == null ||
|
|
676
|
+
if (from.default == null ||
|
|
677
|
+
from.default.toString().toLowerCase() === 'null' ||
|
|
678
|
+
from.default.toString().startsWith('nextval(')) {
|
|
655
679
|
return to.default == null || to.default.toLowerCase() === 'null';
|
|
656
680
|
}
|
|
657
681
|
if (to.mappedType instanceof BooleanType) {
|
package/schema/SchemaHelper.js
CHANGED
|
@@ -78,7 +78,7 @@ export class SchemaHelper {
|
|
|
78
78
|
tableName = this.quote(tableName);
|
|
79
79
|
oldColumnName = this.quote(oldColumnName);
|
|
80
80
|
const columnName = this.quote(to.name);
|
|
81
|
-
const schemaReference =
|
|
81
|
+
const schemaReference = schemaName !== undefined && schemaName !== 'public' ? '"' + schemaName + '".' : '';
|
|
82
82
|
const tableReference = schemaReference + tableName;
|
|
83
83
|
return `alter table ${tableReference} rename column ${oldColumnName} to ${columnName}`;
|
|
84
84
|
}
|
|
@@ -124,7 +124,8 @@ export class SchemaHelper {
|
|
|
124
124
|
*/
|
|
125
125
|
getIndexColumns(index) {
|
|
126
126
|
if (index.columns?.length) {
|
|
127
|
-
return index.columns
|
|
127
|
+
return index.columns
|
|
128
|
+
.map(col => {
|
|
128
129
|
let colDef = this.quote(col.name);
|
|
129
130
|
// Collation comes after column name (SQLite syntax: column COLLATE name)
|
|
130
131
|
if (col.collation) {
|
|
@@ -139,7 +140,8 @@ export class SchemaHelper {
|
|
|
139
140
|
colDef += ` nulls ${col.nulls}`;
|
|
140
141
|
}
|
|
141
142
|
return colDef;
|
|
142
|
-
})
|
|
143
|
+
})
|
|
144
|
+
.join(', ');
|
|
143
145
|
}
|
|
144
146
|
return index.columnNames.map(c => this.quote(c)).join(', ');
|
|
145
147
|
}
|
|
@@ -256,9 +258,11 @@ export class SchemaHelper {
|
|
|
256
258
|
return ret;
|
|
257
259
|
}
|
|
258
260
|
getAddColumnsSQL(table, columns) {
|
|
259
|
-
const adds = columns
|
|
261
|
+
const adds = columns
|
|
262
|
+
.map(column => {
|
|
260
263
|
return `add ${this.createTableColumn(column, table)}`;
|
|
261
|
-
})
|
|
264
|
+
})
|
|
265
|
+
.join(', ');
|
|
262
266
|
return [`alter table ${table.getQuotedName()} ${adds}`];
|
|
263
267
|
}
|
|
264
268
|
getDropColumnsSQL(tableName, columns, schemaName) {
|
|
@@ -310,12 +314,26 @@ export class SchemaHelper {
|
|
|
310
314
|
Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
|
|
311
315
|
Utils.runIfNotEmpty(() => col.push('auto_increment'), column.autoincrement);
|
|
312
316
|
Utils.runIfNotEmpty(() => col.push('unique'), column.autoincrement && !column.primary);
|
|
313
|
-
if (column.autoincrement &&
|
|
317
|
+
if (column.autoincrement &&
|
|
318
|
+
!column.generated &&
|
|
319
|
+
!compositePK &&
|
|
320
|
+
(!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
|
|
314
321
|
Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
|
|
315
322
|
}
|
|
316
323
|
if (useDefault) {
|
|
317
324
|
// https://dev.mysql.com/doc/refman/9.0/en/data-type-defaults.html
|
|
318
|
-
const needsExpression = [
|
|
325
|
+
const needsExpression = [
|
|
326
|
+
'blob',
|
|
327
|
+
'text',
|
|
328
|
+
'json',
|
|
329
|
+
'point',
|
|
330
|
+
'linestring',
|
|
331
|
+
'polygon',
|
|
332
|
+
'multipoint',
|
|
333
|
+
'multilinestring',
|
|
334
|
+
'multipolygon',
|
|
335
|
+
'geometrycollection',
|
|
336
|
+
].some(type => column.type.toLowerCase().startsWith(type));
|
|
319
337
|
const defaultSql = needsExpression && !column.default.startsWith('(') ? `(${column.default})` : column.default;
|
|
320
338
|
col.push(`default ${defaultSql}`);
|
|
321
339
|
}
|
|
@@ -371,7 +389,9 @@ export class SchemaHelper {
|
|
|
371
389
|
columnNames: [fk.column_name],
|
|
372
390
|
constraintName: fk.constraint_name,
|
|
373
391
|
localTableName: schemaName ? `${schemaName}.${tableName}` : tableName,
|
|
374
|
-
referencedTableName: fk.referenced_schema_name
|
|
392
|
+
referencedTableName: fk.referenced_schema_name
|
|
393
|
+
? `${fk.referenced_schema_name}.${fk.referenced_table_name}`
|
|
394
|
+
: fk.referenced_table_name,
|
|
375
395
|
referencedColumnNames: [fk.referenced_column_name],
|
|
376
396
|
updateRule: fk.update_rule.toLowerCase(),
|
|
377
397
|
deleteRule: fk.delete_rule.toLowerCase(),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ClearDatabaseOptions, type CreateSchemaOptions, type Dictionary, type DropSchemaOptions, type EnsureDatabaseOptions, type EntityMetadata, type ISchemaGenerator, type MikroORM, type Transaction, type UpdateSchemaOptions } from '@mikro-orm/core';
|
|
2
|
+
import { AbstractSchemaGenerator } from '@mikro-orm/core/schema';
|
|
2
3
|
import type { SchemaDifference } from '../typings.js';
|
|
3
4
|
import { DatabaseSchema } from './DatabaseSchema.js';
|
|
4
5
|
import type { AbstractSqlDriver } from '../AbstractSqlDriver.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CommitOrderCalculator, Utils, } from '@mikro-orm/core';
|
|
2
|
+
import { AbstractSchemaGenerator } from '@mikro-orm/core/schema';
|
|
2
3
|
import { DatabaseSchema } from './DatabaseSchema.js';
|
|
3
4
|
import { SchemaComparator } from './SchemaComparator.js';
|
|
4
5
|
export class SqlSchemaGenerator extends AbstractSchemaGenerator {
|
|
@@ -127,7 +128,8 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
|
|
|
127
128
|
}
|
|
128
129
|
const schema = options?.schema ?? this.config.get('schema', this.platform.getDefaultSchemaName());
|
|
129
130
|
for (const meta of this.getOrderedMetadata(schema).reverse()) {
|
|
130
|
-
await this.driver
|
|
131
|
+
await this.driver
|
|
132
|
+
.createQueryBuilder(meta.class, this.em?.getTransactionContext(), 'write', false)
|
|
131
133
|
.withSchema(schema)
|
|
132
134
|
.truncate()
|
|
133
135
|
.execute();
|
|
@@ -223,8 +225,11 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
|
|
|
223
225
|
options.dropTables ??= true;
|
|
224
226
|
const toSchema = this.getTargetSchema(options.schema);
|
|
225
227
|
const schemas = toSchema.getNamespaces();
|
|
226
|
-
const fromSchema = options.fromSchema ??
|
|
227
|
-
|
|
228
|
+
const fromSchema = options.fromSchema ??
|
|
229
|
+
(await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas, undefined, this.options.skipTables, this.options.skipViews));
|
|
230
|
+
const wildcardSchemaTables = [...this.metadata.getAll().values()]
|
|
231
|
+
.filter(meta => meta.schema === '*')
|
|
232
|
+
.map(meta => meta.tableName);
|
|
228
233
|
fromSchema.prune(options.schema, wildcardSchemaTables);
|
|
229
234
|
toSchema.prune(options.schema, wildcardSchemaTables);
|
|
230
235
|
return { fromSchema, toSchema };
|
|
@@ -407,7 +412,11 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
|
|
|
407
412
|
return;
|
|
408
413
|
}
|
|
409
414
|
const statements = groups.flatMap(group => {
|
|
410
|
-
return group
|
|
415
|
+
return group
|
|
416
|
+
.join('\n')
|
|
417
|
+
.split(';\n')
|
|
418
|
+
.map(s => s.trim())
|
|
419
|
+
.filter(s => s);
|
|
411
420
|
});
|
|
412
421
|
await Utils.runSerial(statements, stmt => this.driver.execute(stmt));
|
|
413
422
|
}
|
|
@@ -475,9 +484,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
|
|
|
475
484
|
}
|
|
476
485
|
// Check if the definition references the other view's name
|
|
477
486
|
// Use word boundary matching to avoid false positives
|
|
478
|
-
const patterns = [
|
|
479
|
-
new RegExp(`\\b${this.escapeRegExp(otherView.name.toLowerCase())}\\b`),
|
|
480
|
-
];
|
|
487
|
+
const patterns = [new RegExp(`\\b${this.escapeRegExp(otherView.name.toLowerCase())}\\b`)];
|
|
481
488
|
if (otherView.schema) {
|
|
482
489
|
patterns.push(new RegExp(`\\b${this.escapeRegExp(`${otherView.schema}.${otherView.name}`.toLowerCase())}\\b`));
|
|
483
490
|
}
|