@mikro-orm/knex 6.0.0-dev.1 → 6.0.0-dev.100
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/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +8 -4
- package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +35 -18
- package/AbstractSqlDriver.d.ts +66 -0
- package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +422 -129
- package/AbstractSqlPlatform.d.ts +22 -0
- package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +46 -19
- package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +1 -0
- package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +3 -0
- package/{dist/README.md → README.md} +66 -109
- package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +6 -10
- package/{dist/SqlEntityManager.js → SqlEntityManager.js} +5 -10
- package/SqlEntityRepository.d.ts +24 -0
- package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +11 -8
- package/{dist/index.d.ts → index.d.ts} +1 -0
- package/{dist/index.js → index.js} +6 -1
- package/index.mjs +208 -0
- package/package.json +20 -49
- package/query/ArrayCriteriaNode.d.ts +9 -0
- package/{dist/query → query}/CriteriaNode.d.ts +8 -8
- package/{dist/query → query}/CriteriaNode.js +23 -13
- package/query/CriteriaNodeFactory.d.ts +12 -0
- package/{dist/query → query}/CriteriaNodeFactory.js +10 -13
- package/{dist/query → query}/ObjectCriteriaNode.d.ts +3 -3
- package/{dist/query → query}/ObjectCriteriaNode.js +26 -9
- package/{dist/query → query}/QueryBuilder.d.ts +90 -34
- package/query/QueryBuilder.js +1098 -0
- package/{dist/query → query}/QueryBuilderHelper.d.ts +23 -19
- package/{dist/query → query}/QueryBuilderHelper.js +300 -176
- package/query/ScalarCriteriaNode.d.ts +10 -0
- package/{dist/query → query}/ScalarCriteriaNode.js +9 -6
- package/{dist/query → query}/enums.js +1 -1
- package/{dist/query → query}/index.js +5 -1
- package/{dist/schema → schema}/DatabaseSchema.d.ts +6 -3
- package/{dist/schema → schema}/DatabaseSchema.js +33 -23
- package/{dist/schema → schema}/DatabaseTable.d.ts +12 -10
- package/{dist/schema → schema}/DatabaseTable.js +55 -29
- package/{dist/schema → schema}/SchemaComparator.d.ts +7 -5
- package/{dist/schema → schema}/SchemaComparator.js +68 -10
- package/{dist/schema → schema}/SchemaHelper.d.ts +20 -13
- package/{dist/schema → schema}/SchemaHelper.js +56 -11
- package/{dist/schema/SchemaGenerator.d.ts → schema/SqlSchemaGenerator.d.ts} +11 -14
- package/{dist/schema/SchemaGenerator.js → schema/SqlSchemaGenerator.js} +139 -55
- package/{dist/schema → schema}/index.d.ts +1 -1
- package/{dist/schema → schema}/index.js +6 -2
- package/{dist/typings.d.ts → typings.d.ts} +27 -18
- package/CHANGELOG.md +0 -661
- package/dist/AbstractSqlDriver.d.ts +0 -55
- package/dist/AbstractSqlPlatform.d.ts +0 -21
- package/dist/LICENSE +0 -21
- package/dist/SqlEntityRepository.d.ts +0 -23
- package/dist/index.mjs +0 -26
- package/dist/package.json +0 -99
- package/dist/query/ArrayCriteriaNode.d.ts +0 -9
- package/dist/query/CriteriaNodeFactory.d.ts +0 -12
- package/dist/query/QueryBuilder.js +0 -688
- package/dist/query/ScalarCriteriaNode.d.ts +0 -9
- /package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
- /package/{dist/query → query}/enums.d.ts +0 -0
- /package/{dist/query → query}/index.d.ts +0 -0
- /package/{dist/typings.js → typings.js} +0 -0
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SchemaComparator = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
4
5
|
const core_1 = require("@mikro-orm/core");
|
|
5
6
|
/**
|
|
6
7
|
* Compares two Schemas and return an instance of SchemaDifference.
|
|
7
8
|
*/
|
|
8
9
|
class SchemaComparator {
|
|
10
|
+
platform;
|
|
11
|
+
helper;
|
|
12
|
+
logger;
|
|
9
13
|
constructor(platform) {
|
|
10
14
|
this.platform = platform;
|
|
11
15
|
this.helper = this.platform.getSchemaHelper();
|
|
16
|
+
this.logger = this.platform.getConfig().getLogger();
|
|
12
17
|
}
|
|
13
18
|
/**
|
|
14
19
|
* Returns a SchemaDifference object containing the differences between the schemas fromSchema and toSchema.
|
|
@@ -105,18 +110,22 @@ class SchemaComparator {
|
|
|
105
110
|
renamedColumns: {},
|
|
106
111
|
renamedIndexes: {},
|
|
107
112
|
fromTable,
|
|
113
|
+
toTable,
|
|
108
114
|
};
|
|
109
115
|
if (this.diffComment(fromTable.comment, toTable.comment)) {
|
|
110
116
|
tableDifferences.changedComment = toTable.comment;
|
|
117
|
+
this.log(`table comment changed for ${tableDifferences.name}`, { fromTableComment: fromTable.comment, toTableComment: toTable.comment });
|
|
111
118
|
}
|
|
112
119
|
const fromTableColumns = fromTable.getColumns();
|
|
113
120
|
const toTableColumns = toTable.getColumns();
|
|
121
|
+
const tableName = (toTable.schema ? toTable.schema + '.' : '') + toTable.name;
|
|
114
122
|
// See if all the columns in "from" table exist in "to" table
|
|
115
123
|
for (const column of toTableColumns) {
|
|
116
124
|
if (fromTable.hasColumn(column.name)) {
|
|
117
125
|
continue;
|
|
118
126
|
}
|
|
119
127
|
tableDifferences.addedColumns[column.name] = column;
|
|
128
|
+
this.log(`column ${tableDifferences.name}.${column.name} of type ${column.type} added`);
|
|
120
129
|
changes++;
|
|
121
130
|
}
|
|
122
131
|
/* See if there are any removed columns in "to" table */
|
|
@@ -124,11 +133,12 @@ class SchemaComparator {
|
|
|
124
133
|
// See if column is removed in "to" table.
|
|
125
134
|
if (!toTable.hasColumn(column.name)) {
|
|
126
135
|
tableDifferences.removedColumns[column.name] = column;
|
|
136
|
+
this.log(`column ${tableDifferences.name}.${column.name} removed`);
|
|
127
137
|
changes++;
|
|
128
138
|
continue;
|
|
129
139
|
}
|
|
130
140
|
// See if column has changed properties in "to" table.
|
|
131
|
-
const changedProperties = this.diffColumn(column, toTable.getColumn(column.name));
|
|
141
|
+
const changedProperties = this.diffColumn(column, toTable.getColumn(column.name), tableName);
|
|
132
142
|
if (changedProperties.size === 0) {
|
|
133
143
|
continue;
|
|
134
144
|
}
|
|
@@ -138,6 +148,7 @@ class SchemaComparator {
|
|
|
138
148
|
column: toTable.getColumn(column.name),
|
|
139
149
|
changedProperties,
|
|
140
150
|
};
|
|
151
|
+
this.log(`column ${tableDifferences.name}.${column.name} changed`, { changedProperties });
|
|
141
152
|
changes++;
|
|
142
153
|
}
|
|
143
154
|
this.detectColumnRenamings(tableDifferences);
|
|
@@ -149,6 +160,7 @@ class SchemaComparator {
|
|
|
149
160
|
continue;
|
|
150
161
|
}
|
|
151
162
|
tableDifferences.addedIndexes[index.keyName] = index;
|
|
163
|
+
this.log(`index ${index.keyName} added to table ${tableDifferences.name}`, { index });
|
|
152
164
|
changes++;
|
|
153
165
|
}
|
|
154
166
|
// See if there are any removed indexes in "to" table
|
|
@@ -156,6 +168,7 @@ class SchemaComparator {
|
|
|
156
168
|
// See if index is removed in "to" table.
|
|
157
169
|
if ((index.primary && !toTable.hasPrimaryKey()) || !index.primary && !toTable.hasIndex(index.keyName)) {
|
|
158
170
|
tableDifferences.removedIndexes[index.keyName] = index;
|
|
171
|
+
this.log(`index ${index.keyName} removed from table ${tableDifferences.name}`);
|
|
159
172
|
changes++;
|
|
160
173
|
continue;
|
|
161
174
|
}
|
|
@@ -165,6 +178,7 @@ class SchemaComparator {
|
|
|
165
178
|
continue;
|
|
166
179
|
}
|
|
167
180
|
tableDifferences.changedIndexes[index.keyName] = toTableIndex;
|
|
181
|
+
this.log(`index ${index.keyName} changed in table ${tableDifferences.name}`, { fromTableIndex: index, toTableIndex });
|
|
168
182
|
changes++;
|
|
169
183
|
}
|
|
170
184
|
this.detectIndexRenamings(tableDifferences);
|
|
@@ -176,12 +190,14 @@ class SchemaComparator {
|
|
|
176
190
|
continue;
|
|
177
191
|
}
|
|
178
192
|
tableDifferences.addedChecks[check.name] = check;
|
|
193
|
+
this.log(`check constraint ${check.name} added to table ${tableDifferences.name}`, { check });
|
|
179
194
|
changes++;
|
|
180
195
|
}
|
|
181
196
|
// See if there are any removed checks in "to" table
|
|
182
197
|
for (const check of fromTableChecks) {
|
|
183
198
|
if (!toTable.hasCheck(check.name)) {
|
|
184
199
|
tableDifferences.removedChecks[check.name] = check;
|
|
200
|
+
this.log(`check constraint ${check.name} removed from table ${tableDifferences.name}`);
|
|
185
201
|
changes++;
|
|
186
202
|
continue;
|
|
187
203
|
}
|
|
@@ -190,6 +206,7 @@ class SchemaComparator {
|
|
|
190
206
|
if (!this.diffCheck(check, toTableCheck)) {
|
|
191
207
|
continue;
|
|
192
208
|
}
|
|
209
|
+
this.log(`check constraint ${check.name} changed in table ${tableDifferences.name}`, { fromTableCheck: check, toTableCheck });
|
|
193
210
|
tableDifferences.changedChecks[check.name] = toTableCheck;
|
|
194
211
|
changes++;
|
|
195
212
|
}
|
|
@@ -202,6 +219,7 @@ class SchemaComparator {
|
|
|
202
219
|
delete toForeignKeys[toConstraint.constraintName];
|
|
203
220
|
}
|
|
204
221
|
else if (fromConstraint.constraintName.toLowerCase() === toConstraint.constraintName.toLowerCase()) {
|
|
222
|
+
this.log(`FK constraint ${fromConstraint.constraintName} changed in table ${tableDifferences.name}`, { fromConstraint, toConstraint });
|
|
205
223
|
tableDifferences.changedForeignKeys[toConstraint.constraintName] = toConstraint;
|
|
206
224
|
changes++;
|
|
207
225
|
delete fromForeignKeys[fromConstraint.constraintName];
|
|
@@ -211,10 +229,12 @@ class SchemaComparator {
|
|
|
211
229
|
}
|
|
212
230
|
for (const fromConstraint of Object.values(fromForeignKeys)) {
|
|
213
231
|
tableDifferences.removedForeignKeys[fromConstraint.constraintName] = fromConstraint;
|
|
232
|
+
this.log(`FK constraint ${fromConstraint.constraintName} removed from table ${tableDifferences.name}`);
|
|
214
233
|
changes++;
|
|
215
234
|
}
|
|
216
235
|
for (const toConstraint of Object.values(toForeignKeys)) {
|
|
217
236
|
tableDifferences.addedForeignKeys[toConstraint.constraintName] = toConstraint;
|
|
237
|
+
this.log(`FK constraint ${toConstraint.constraintName} added from table ${tableDifferences.name}`, { constraint: toConstraint });
|
|
218
238
|
changes++;
|
|
219
239
|
}
|
|
220
240
|
return changes ? tableDifferences : false;
|
|
@@ -249,6 +269,7 @@ class SchemaComparator {
|
|
|
249
269
|
tableDifferences.renamedColumns[removedColumnName] = addedColumn;
|
|
250
270
|
delete tableDifferences.addedColumns[addedColumnName];
|
|
251
271
|
delete tableDifferences.removedColumns[removedColumnName];
|
|
272
|
+
this.log(`renamed column detected in table ${tableDifferences.name}`, { old: removedColumnName, new: addedColumnName });
|
|
252
273
|
}
|
|
253
274
|
}
|
|
254
275
|
/**
|
|
@@ -282,6 +303,7 @@ class SchemaComparator {
|
|
|
282
303
|
tableDifferences.renamedIndexes[removedIndexName] = addedIndex;
|
|
283
304
|
delete tableDifferences.addedIndexes[addedIndexName];
|
|
284
305
|
delete tableDifferences.removedIndexes[removedIndexName];
|
|
306
|
+
this.log(`renamed index detected in table ${tableDifferences.name}`, { old: removedIndexName, new: addedIndexName });
|
|
285
307
|
}
|
|
286
308
|
}
|
|
287
309
|
diffForeignKey(key1, key2) {
|
|
@@ -294,7 +316,10 @@ class SchemaComparator {
|
|
|
294
316
|
if (key1.referencedTableName !== key2.referencedTableName) {
|
|
295
317
|
return true;
|
|
296
318
|
}
|
|
297
|
-
const
|
|
319
|
+
const defaultRule = ['restrict', 'no action'];
|
|
320
|
+
const rule = (key, method) => {
|
|
321
|
+
return (key[method] ?? defaultRule[0]).toLowerCase().replace(defaultRule[1], defaultRule[0]);
|
|
322
|
+
};
|
|
298
323
|
const compare = (method) => rule(key1, method) === rule(key2, method);
|
|
299
324
|
return !compare('updateRule') || !compare('deleteRule');
|
|
300
325
|
}
|
|
@@ -302,34 +327,53 @@ class SchemaComparator {
|
|
|
302
327
|
* Returns the difference between the columns
|
|
303
328
|
* If there are differences this method returns field2, otherwise the boolean false.
|
|
304
329
|
*/
|
|
305
|
-
diffColumn(column1, column2) {
|
|
330
|
+
diffColumn(column1, column2, tableName) {
|
|
306
331
|
const changedProperties = new Set();
|
|
307
332
|
const prop1 = this.mapColumnToProperty({ ...column1, autoincrement: false });
|
|
308
333
|
const prop2 = this.mapColumnToProperty({ ...column2, autoincrement: false });
|
|
309
|
-
const columnType1 = column1.mappedType.getColumnType(prop1, this.platform);
|
|
310
|
-
const columnType2 = column2.mappedType.getColumnType(prop2, this.platform);
|
|
311
|
-
|
|
334
|
+
const columnType1 = column1.mappedType.getColumnType(prop1, this.platform).toLowerCase();
|
|
335
|
+
const columnType2 = column2.mappedType.getColumnType(prop2, this.platform).toLowerCase();
|
|
336
|
+
const log = (msg, params) => {
|
|
337
|
+
if (tableName) {
|
|
338
|
+
const copy = core_1.Utils.copy(params);
|
|
339
|
+
core_1.Utils.dropUndefinedProperties(copy);
|
|
340
|
+
this.log(msg, copy);
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
if (columnType1 !== columnType2 &&
|
|
344
|
+
!(column1.ignoreSchemaChanges?.includes('type') ||
|
|
345
|
+
column2.ignoreSchemaChanges?.includes('type'))) {
|
|
346
|
+
log(`'type' changed for column ${tableName}.${column1.name}`, { columnType1, columnType2 });
|
|
312
347
|
changedProperties.add('type');
|
|
313
348
|
}
|
|
314
349
|
if (column1.nullable !== column2.nullable) {
|
|
350
|
+
log(`'nullable' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
315
351
|
changedProperties.add('nullable');
|
|
316
352
|
}
|
|
317
353
|
if (!!column1.autoincrement !== !!column2.autoincrement) {
|
|
354
|
+
log(`'autoincrement' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
318
355
|
changedProperties.add('autoincrement');
|
|
319
356
|
}
|
|
320
357
|
if (column1.unsigned !== column2.unsigned && this.platform.supportsUnsigned()) {
|
|
358
|
+
log(`'unsigned' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
321
359
|
changedProperties.add('unsigned');
|
|
322
360
|
}
|
|
323
361
|
if (!this.hasSameDefaultValue(column1, column2)) {
|
|
362
|
+
log(`'default' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
324
363
|
changedProperties.add('default');
|
|
325
364
|
}
|
|
326
365
|
if (this.diffComment(column1.comment, column2.comment)) {
|
|
366
|
+
log(`'comment' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
327
367
|
changedProperties.add('comment');
|
|
328
368
|
}
|
|
329
369
|
if (this.diffEnumItems(column1.enumItems, column2.enumItems)) {
|
|
370
|
+
log(`'enumItems' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
330
371
|
changedProperties.add('enumItems');
|
|
331
372
|
}
|
|
332
|
-
if ((column1.extra || '').toLowerCase() !== (column2.extra || '').toLowerCase()
|
|
373
|
+
if ((column1.extra || '').toLowerCase() !== (column2.extra || '').toLowerCase() &&
|
|
374
|
+
!(column1.ignoreSchemaChanges?.includes('extra') ||
|
|
375
|
+
column2.ignoreSchemaChanges?.includes('extra'))) {
|
|
376
|
+
log(`'extra' changed for column ${tableName}.${column1.name}`, { column1, column2 });
|
|
333
377
|
changedProperties.add('extra');
|
|
334
378
|
}
|
|
335
379
|
return changedProperties;
|
|
@@ -347,8 +391,8 @@ class SchemaComparator {
|
|
|
347
391
|
* Compares index1 with index2 and returns index2 if there are any differences or false in case there are no differences.
|
|
348
392
|
*/
|
|
349
393
|
diffIndex(index1, index2) {
|
|
350
|
-
// if one of them is a custom expression, compare only by name
|
|
351
|
-
if (index1.expression || index2.expression) {
|
|
394
|
+
// if one of them is a custom expression or full text index, compare only by name
|
|
395
|
+
if (index1.expression || index2.expression || index1.type === 'fulltext' || index2.type === 'fulltext') {
|
|
352
396
|
return index1.keyName !== index2.keyName;
|
|
353
397
|
}
|
|
354
398
|
return !this.isIndexFulfilledBy(index1, index2) || !this.isIndexFulfilledBy(index2, index1);
|
|
@@ -384,7 +428,10 @@ class SchemaComparator {
|
|
|
384
428
|
return index1.primary === index2.primary && index1.unique === index2.unique;
|
|
385
429
|
}
|
|
386
430
|
diffCheck(check1, check2) {
|
|
387
|
-
|
|
431
|
+
// check constraint definition might be normalized by the driver,
|
|
432
|
+
// e.g. quotes might be added (https://github.com/mikro-orm/mikro-orm/issues/3827)
|
|
433
|
+
const simplify = (str) => str?.replace(/['"`()]/g, '').toLowerCase();
|
|
434
|
+
return simplify(check1.expression) !== simplify(check2.expression);
|
|
388
435
|
}
|
|
389
436
|
hasSameDefaultValue(from, to) {
|
|
390
437
|
if (from.default == null || from.default.toString().toLowerCase() === 'null' || from.default.toString().startsWith('nextval(')) {
|
|
@@ -395,6 +442,11 @@ class SchemaComparator {
|
|
|
395
442
|
const defaultValueTo = !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + to.default);
|
|
396
443
|
return defaultValueFrom === defaultValueTo;
|
|
397
444
|
}
|
|
445
|
+
if (to.mappedType instanceof core_1.JsonType) {
|
|
446
|
+
const defaultValueFrom = (0, core_1.parseJsonSafe)(from.default.replace(/^'(.*)'$/, '$1'));
|
|
447
|
+
const defaultValueTo = (0, core_1.parseJsonSafe)(to.default?.replace(/^'(.*)'$/, '$1'));
|
|
448
|
+
return core_1.Utils.equals(defaultValueFrom, defaultValueTo);
|
|
449
|
+
}
|
|
398
450
|
if (to.mappedType instanceof core_1.DateTimeType && from.default && to.default) {
|
|
399
451
|
// normalize now/current_timestamp defaults, also remove `()` from the end of default expression
|
|
400
452
|
const defaultValueFrom = from.default.toLowerCase().replace('current_timestamp', 'now').replace(/\(\)$/, '');
|
|
@@ -423,5 +475,11 @@ class SchemaComparator {
|
|
|
423
475
|
scale: match ? +match[2] : column.scale,
|
|
424
476
|
};
|
|
425
477
|
}
|
|
478
|
+
log(message, params) {
|
|
479
|
+
if (params) {
|
|
480
|
+
message += ' ' + (0, util_1.inspect)(params);
|
|
481
|
+
}
|
|
482
|
+
this.logger.log('schema', message);
|
|
483
|
+
}
|
|
426
484
|
}
|
|
427
485
|
exports.SchemaComparator = SchemaComparator;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type
|
|
2
|
+
import { type Connection, type Dictionary } from '@mikro-orm/core';
|
|
3
3
|
import type { AbstractSqlConnection } from '../AbstractSqlConnection';
|
|
4
4
|
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
|
|
5
|
-
import type {
|
|
5
|
+
import type { CheckDef, Column, IndexDef, Table, TableDifference } from '../typings';
|
|
6
6
|
import type { DatabaseTable } from './DatabaseTable';
|
|
7
|
+
import type { DatabaseSchema } from './DatabaseSchema';
|
|
7
8
|
export declare abstract class SchemaHelper {
|
|
8
9
|
protected readonly platform: AbstractSqlPlatform;
|
|
9
10
|
constructor(platform: AbstractSqlPlatform);
|
|
@@ -13,24 +14,30 @@ export declare abstract class SchemaHelper {
|
|
|
13
14
|
getSchemaEnd(): string;
|
|
14
15
|
finalizeTable(table: Knex.TableBuilder, charset: string, collate?: string): void;
|
|
15
16
|
supportsSchemaConstraints(): boolean;
|
|
16
|
-
getPrimaryKeys(connection: AbstractSqlConnection, indexes:
|
|
17
|
+
getPrimaryKeys(connection: AbstractSqlConnection, indexes: IndexDef[] | undefined, tableName: string, schemaName?: string): Promise<string[]>;
|
|
17
18
|
getForeignKeys(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Dictionary>;
|
|
18
|
-
|
|
19
|
+
protected getTableKey(t: Table): string;
|
|
20
|
+
getEnumDefinitions(connection: AbstractSqlConnection, checks: CheckDef[], tableName: string, schemaName?: string): Promise<Dictionary<string[]>>;
|
|
21
|
+
getDropNativeEnumSQL(name: string, schema?: string): string;
|
|
22
|
+
getAlterNativeEnumSQL(name: string, schema?: string, value?: string): string;
|
|
23
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
|
|
19
24
|
getListTablesSQL(schemaName?: string): string;
|
|
20
|
-
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column): string;
|
|
21
|
-
getCreateIndexSQL(tableName: string, index:
|
|
22
|
-
getDropIndexSQL(tableName: string, index:
|
|
23
|
-
getRenameIndexSQL(tableName: string, index:
|
|
25
|
+
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column, schemaName?: string): string;
|
|
26
|
+
getCreateIndexSQL(tableName: string, index: IndexDef): string;
|
|
27
|
+
getDropIndexSQL(tableName: string, index: IndexDef): string;
|
|
28
|
+
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
|
|
29
|
+
hasNonDefaultPrimaryKeyName(table: DatabaseTable): boolean;
|
|
24
30
|
createTableColumn(table: Knex.TableBuilder, column: Column, fromTable: DatabaseTable, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
25
31
|
configureColumn(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
26
32
|
configureColumnDefault(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
27
33
|
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string;
|
|
28
|
-
getAlterColumnAutoincrement(tableName: string, column: Column): string;
|
|
29
|
-
getChangeColumnCommentSQL(tableName: string, to: Column): string;
|
|
34
|
+
getAlterColumnAutoincrement(tableName: string, column: Column, schemaName?: string): string;
|
|
35
|
+
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
36
|
+
getNamespaces(connection: AbstractSqlConnection): Promise<string[]>;
|
|
30
37
|
getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Column[]>;
|
|
31
|
-
getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<
|
|
32
|
-
getChecks(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<
|
|
33
|
-
protected mapIndexes(indexes:
|
|
38
|
+
getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<IndexDef[]>;
|
|
39
|
+
getChecks(connection: AbstractSqlConnection, tableName: string, schemaName?: string, columns?: Column[]): Promise<CheckDef[]>;
|
|
40
|
+
protected mapIndexes(indexes: IndexDef[]): Promise<IndexDef[]>;
|
|
34
41
|
getForeignKeysSQL(tableName: string, schemaName?: string): string;
|
|
35
42
|
mapForeignKeys(fks: any[], tableName: string, schemaName?: string): Dictionary;
|
|
36
43
|
normalizeDefaultValue(defaultValue: string, length?: number, defaultValues?: Dictionary<string[]>): string | number;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SchemaHelper = void 0;
|
|
4
4
|
const core_1 = require("@mikro-orm/core");
|
|
5
5
|
class SchemaHelper {
|
|
6
|
+
platform;
|
|
6
7
|
constructor(platform) {
|
|
7
8
|
this.platform = platform;
|
|
8
9
|
}
|
|
@@ -24,25 +25,57 @@ class SchemaHelper {
|
|
|
24
25
|
supportsSchemaConstraints() {
|
|
25
26
|
return true;
|
|
26
27
|
}
|
|
27
|
-
async getPrimaryKeys(connection, indexes, tableName, schemaName) {
|
|
28
|
-
const
|
|
29
|
-
return
|
|
28
|
+
async getPrimaryKeys(connection, indexes = [], tableName, schemaName) {
|
|
29
|
+
const pks = indexes.filter(i => i.primary).map(pk => pk.columnNames);
|
|
30
|
+
return core_1.Utils.flatten(pks);
|
|
30
31
|
}
|
|
31
32
|
async getForeignKeys(connection, tableName, schemaName) {
|
|
32
33
|
const fks = await connection.execute(this.getForeignKeysSQL(tableName, schemaName));
|
|
33
34
|
return this.mapForeignKeys(fks, tableName, schemaName);
|
|
34
35
|
}
|
|
36
|
+
getTableKey(t) {
|
|
37
|
+
const unquote = (str) => str.replace(/['"`]/g, '');
|
|
38
|
+
const parts = t.table_name.split('.');
|
|
39
|
+
if (parts.length > 1) {
|
|
40
|
+
return `${unquote(parts[0])}.${unquote(parts[1])}`;
|
|
41
|
+
}
|
|
42
|
+
if (t.schema_name) {
|
|
43
|
+
return `${unquote(t.schema_name)}.${unquote(t.table_name)}`;
|
|
44
|
+
}
|
|
45
|
+
return unquote(t.table_name);
|
|
46
|
+
}
|
|
35
47
|
async getEnumDefinitions(connection, checks, tableName, schemaName) {
|
|
36
48
|
return {};
|
|
37
49
|
}
|
|
50
|
+
getDropNativeEnumSQL(name, schema) {
|
|
51
|
+
throw new Error('Not supported by given driver');
|
|
52
|
+
}
|
|
53
|
+
getAlterNativeEnumSQL(name, schema, value) {
|
|
54
|
+
throw new Error('Not supported by given driver');
|
|
55
|
+
}
|
|
56
|
+
async loadInformationSchema(schema, connection, tables, schemas) {
|
|
57
|
+
for (const t of tables) {
|
|
58
|
+
const table = schema.addTable(t.table_name, t.schema_name);
|
|
59
|
+
table.comment = t.table_comment;
|
|
60
|
+
const cols = await this.getColumns(connection, table.name, table.schema);
|
|
61
|
+
const indexes = await this.getIndexes(connection, table.name, table.schema);
|
|
62
|
+
const checks = await this.getChecks(connection, table.name, table.schema, cols);
|
|
63
|
+
const pks = await this.getPrimaryKeys(connection, indexes, table.name, table.schema);
|
|
64
|
+
const fks = await this.getForeignKeys(connection, table.name, table.schema);
|
|
65
|
+
const enums = await this.getEnumDefinitions(connection, checks, table.name, table.schema);
|
|
66
|
+
table.init(cols, indexes, checks, pks, fks, enums);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
38
69
|
getListTablesSQL(schemaName) {
|
|
39
70
|
throw new Error('Not supported by given driver');
|
|
40
71
|
}
|
|
41
|
-
getRenameColumnSQL(tableName, oldColumnName, to) {
|
|
72
|
+
getRenameColumnSQL(tableName, oldColumnName, to, schemaName) {
|
|
42
73
|
tableName = this.platform.quoteIdentifier(tableName);
|
|
43
74
|
oldColumnName = this.platform.quoteIdentifier(oldColumnName);
|
|
44
75
|
const columnName = this.platform.quoteIdentifier(to.name);
|
|
45
|
-
|
|
76
|
+
const schemaReference = (schemaName !== undefined && schemaName !== 'public') ? ('"' + schemaName + '".') : '';
|
|
77
|
+
const tableReference = schemaReference + tableName;
|
|
78
|
+
return `alter table ${tableReference} rename column ${oldColumnName} to ${columnName}`;
|
|
46
79
|
}
|
|
47
80
|
getCreateIndexSQL(tableName, index) {
|
|
48
81
|
/* istanbul ignore if */
|
|
@@ -59,13 +92,22 @@ class SchemaHelper {
|
|
|
59
92
|
getRenameIndexSQL(tableName, index, oldIndexName) {
|
|
60
93
|
return [this.getDropIndexSQL(tableName, { ...index, keyName: oldIndexName }), this.getCreateIndexSQL(tableName, index)].join(';\n');
|
|
61
94
|
}
|
|
95
|
+
hasNonDefaultPrimaryKeyName(table) {
|
|
96
|
+
const pkIndex = table.getPrimaryKey();
|
|
97
|
+
if (!pkIndex || !this.platform.supportsCustomPrimaryKeyNames()) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const defaultName = this.platform.getDefaultPrimaryName(table.name, pkIndex.columnNames);
|
|
101
|
+
return pkIndex?.keyName !== defaultName;
|
|
102
|
+
}
|
|
62
103
|
createTableColumn(table, column, fromTable, changedProperties) {
|
|
63
104
|
const compositePK = fromTable.getPrimaryKey()?.composite;
|
|
64
105
|
if (column.autoincrement && !compositePK && (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
|
|
106
|
+
const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(fromTable);
|
|
65
107
|
if (column.mappedType instanceof core_1.BigIntType) {
|
|
66
|
-
return table.bigIncrements(column.name, { primaryKey
|
|
108
|
+
return table.bigIncrements(column.name, { primaryKey });
|
|
67
109
|
}
|
|
68
|
-
return table.increments(column.name, { primaryKey
|
|
110
|
+
return table.increments(column.name, { primaryKey });
|
|
69
111
|
}
|
|
70
112
|
if (column.mappedType instanceof core_1.EnumType && column.enumItems?.every(item => core_1.Utils.isString(item))) {
|
|
71
113
|
return table.enum(column.name, column.enumItems);
|
|
@@ -87,26 +129,29 @@ class SchemaHelper {
|
|
|
87
129
|
core_1.Utils.runIfNotEmpty(() => col.defaultTo(column.default == null ? null : knex.raw(column.default)), guard('default'));
|
|
88
130
|
}
|
|
89
131
|
else {
|
|
90
|
-
core_1.Utils.runIfNotEmpty(() => col.defaultTo(
|
|
132
|
+
core_1.Utils.runIfNotEmpty(() => col.defaultTo(knex.raw(column.default)), column.default != null && column.default !== 'null');
|
|
91
133
|
}
|
|
92
134
|
return col;
|
|
93
135
|
}
|
|
94
136
|
getPreAlterTable(tableDiff, safe) {
|
|
95
137
|
return '';
|
|
96
138
|
}
|
|
97
|
-
getAlterColumnAutoincrement(tableName, column) {
|
|
139
|
+
getAlterColumnAutoincrement(tableName, column, schemaName) {
|
|
98
140
|
return '';
|
|
99
141
|
}
|
|
100
|
-
getChangeColumnCommentSQL(tableName, to) {
|
|
142
|
+
getChangeColumnCommentSQL(tableName, to, schemaName) {
|
|
101
143
|
return '';
|
|
102
144
|
}
|
|
145
|
+
async getNamespaces(connection) {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
103
148
|
async getColumns(connection, tableName, schemaName) {
|
|
104
149
|
throw new Error('Not supported by given driver');
|
|
105
150
|
}
|
|
106
151
|
async getIndexes(connection, tableName, schemaName) {
|
|
107
152
|
throw new Error('Not supported by given driver');
|
|
108
153
|
}
|
|
109
|
-
async getChecks(connection, tableName, schemaName) {
|
|
154
|
+
async getChecks(connection, tableName, schemaName, columns) {
|
|
110
155
|
throw new Error('Not supported by given driver');
|
|
111
156
|
}
|
|
112
157
|
async mapIndexes(indexes) {
|
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import { AbstractSchemaGenerator } from '@mikro-orm/core';
|
|
1
|
+
import { AbstractSchemaGenerator, type MikroORM, type ISchemaGenerator } from '@mikro-orm/core';
|
|
2
2
|
import type { SchemaDifference } from '../typings';
|
|
3
3
|
import { DatabaseSchema } from './DatabaseSchema';
|
|
4
4
|
import type { AbstractSqlDriver } from '../AbstractSqlDriver';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Should be renamed to `SqlSchemaGenerator` in v6
|
|
8
|
-
*/
|
|
9
|
-
export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> {
|
|
5
|
+
export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> implements ISchemaGenerator {
|
|
10
6
|
private readonly helper;
|
|
11
|
-
private readonly knex;
|
|
12
7
|
private readonly options;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*/
|
|
16
|
-
constructor(em: SqlEntityManager);
|
|
17
|
-
generate(): Promise<string>;
|
|
8
|
+
protected lastEnsuredDatabase?: string;
|
|
9
|
+
static register(orm: MikroORM): void;
|
|
18
10
|
createSchema(options?: {
|
|
19
11
|
wrap?: boolean;
|
|
20
12
|
schema?: string;
|
|
@@ -34,6 +26,10 @@ export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSql
|
|
|
34
26
|
dropDb?: boolean;
|
|
35
27
|
schema?: string;
|
|
36
28
|
}): Promise<void>;
|
|
29
|
+
clearDatabase(options?: {
|
|
30
|
+
schema?: string;
|
|
31
|
+
truncate?: boolean;
|
|
32
|
+
}): Promise<void>;
|
|
37
33
|
getDropSchemaSQL(options?: {
|
|
38
34
|
wrap?: boolean;
|
|
39
35
|
dropMigrationsTable?: boolean;
|
|
@@ -83,7 +79,7 @@ export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSql
|
|
|
83
79
|
* creates new database and connects to it
|
|
84
80
|
*/
|
|
85
81
|
createDatabase(name: string): Promise<void>;
|
|
86
|
-
dropDatabase(name
|
|
82
|
+
dropDatabase(name?: string): Promise<void>;
|
|
87
83
|
execute(sql: string, options?: {
|
|
88
84
|
wrap?: boolean;
|
|
89
85
|
}): Promise<void>;
|
|
@@ -95,7 +91,8 @@ export declare class SchemaGenerator extends AbstractSchemaGenerator<AbstractSql
|
|
|
95
91
|
private createCheck;
|
|
96
92
|
private dropCheck;
|
|
97
93
|
private dropTable;
|
|
98
|
-
private configureColumn;
|
|
99
94
|
private createForeignKeys;
|
|
100
95
|
private dump;
|
|
96
|
+
private get knex();
|
|
101
97
|
}
|
|
98
|
+
export { SqlSchemaGenerator as SchemaGenerator };
|