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