@mikro-orm/knex 6.2.10-dev.1 → 6.2.10-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/AbstractSqlDriver.d.ts +9 -9
- package/AbstractSqlDriver.js +5 -2
- package/PivotCollectionPersister.d.ts +2 -2
- package/SqlEntityManager.d.ts +2 -2
- package/SqlEntityRepository.d.ts +4 -4
- package/dialects/mysql/MySqlSchemaHelper.js +2 -2
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +3 -0
- package/dialects/sqlite/BaseSqlitePlatform.js +3 -0
- package/dialects/sqlite/BaseSqliteSchemaHelper.d.ts +2 -1
- package/dialects/sqlite/BaseSqliteSchemaHelper.js +17 -0
- package/dialects/sqlite/LibSqlKnexDialect.d.ts +5 -0
- package/dialects/sqlite/LibSqlKnexDialect.js +67 -0
- package/index.mjs +1 -0
- package/package.json +2 -2
- package/query/CriteriaNode.d.ts +0 -1
- package/query/ObjectCriteriaNode.js +2 -2
- package/query/QueryBuilder.d.ts +98 -78
- package/query/QueryBuilder.js +11 -7
- package/query/QueryBuilderHelper.d.ts +2 -2
- package/query/QueryBuilderHelper.js +17 -3
- package/schema/DatabaseTable.d.ts +3 -2
- package/schema/DatabaseTable.js +12 -13
- package/schema/SchemaComparator.js +6 -2
- package/schema/SchemaHelper.d.ts +18 -2
- package/schema/SchemaHelper.js +139 -4
- package/schema/SqlSchemaGenerator.d.ts +4 -11
- package/schema/SqlSchemaGenerator.js +37 -152
- package/typings.d.ts +3 -1
package/schema/SchemaHelper.js
CHANGED
|
@@ -30,7 +30,11 @@ class SchemaHelper {
|
|
|
30
30
|
return core_1.Utils.flatten(pks);
|
|
31
31
|
}
|
|
32
32
|
inferLengthFromColumnType(type) {
|
|
33
|
-
|
|
33
|
+
const match = type.match(/^\w+\s*(?:\(\s*(\d+)\s*\)|$)/);
|
|
34
|
+
if (!match) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
return +match[1];
|
|
34
38
|
}
|
|
35
39
|
async getForeignKeys(connection, tableName, schemaName) {
|
|
36
40
|
const fks = await connection.execute(this.getForeignKeysSQL(tableName, schemaName));
|
|
@@ -56,13 +60,12 @@ class SchemaHelper {
|
|
|
56
60
|
getDropNativeEnumSQL(name, schema) {
|
|
57
61
|
throw new Error('Not supported by given driver');
|
|
58
62
|
}
|
|
59
|
-
getAlterNativeEnumSQL(name, schema, value) {
|
|
63
|
+
getAlterNativeEnumSQL(name, schema, value, items, oldItems) {
|
|
60
64
|
throw new Error('Not supported by given driver');
|
|
61
65
|
}
|
|
62
66
|
async loadInformationSchema(schema, connection, tables, schemas) {
|
|
63
67
|
for (const t of tables) {
|
|
64
|
-
const table = schema.addTable(t.table_name, t.schema_name);
|
|
65
|
-
table.comment = t.table_comment;
|
|
68
|
+
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
66
69
|
const cols = await this.getColumns(connection, table.name, table.schema);
|
|
67
70
|
const indexes = await this.getIndexes(connection, table.name, table.schema);
|
|
68
71
|
const checks = await this.getChecks(connection, table.name, table.schema, cols);
|
|
@@ -273,5 +276,137 @@ class SchemaHelper {
|
|
|
273
276
|
pushTableQuery(table, expression, grouping = 'alterTable') {
|
|
274
277
|
table._statements.push({ grouping, method: 'raw', args: [expression] });
|
|
275
278
|
}
|
|
279
|
+
async dump(builder, append) {
|
|
280
|
+
if (typeof builder === 'string') {
|
|
281
|
+
return builder ? builder + (builder.endsWith(';') ? '' : ';') + append : '';
|
|
282
|
+
}
|
|
283
|
+
const sql = await builder.generateDdlCommands();
|
|
284
|
+
const queries = [...sql.pre, ...sql.sql, ...sql.post];
|
|
285
|
+
if (queries.length === 0) {
|
|
286
|
+
return '';
|
|
287
|
+
}
|
|
288
|
+
const dump = `${queries.map(q => typeof q === 'object' ? q.sql : q).join(';\n')};${append}`;
|
|
289
|
+
const tmp = dump.replace(/pragma table_.+/ig, '').replace(/\n\n+/g, '\n').trim();
|
|
290
|
+
return tmp ? tmp + append : '';
|
|
291
|
+
}
|
|
292
|
+
createTable(tableDef, alter) {
|
|
293
|
+
return this.createSchemaBuilder(tableDef.schema).createTable(tableDef.name, table => {
|
|
294
|
+
tableDef.getColumns().forEach(column => {
|
|
295
|
+
const col = this.createTableColumn(table, column, tableDef, undefined, alter);
|
|
296
|
+
this.configureColumn(column, col, this.knex);
|
|
297
|
+
});
|
|
298
|
+
for (const index of tableDef.getIndexes()) {
|
|
299
|
+
const createPrimary = !tableDef.getColumns().some(c => c.autoincrement && c.primary) || this.hasNonDefaultPrimaryKeyName(tableDef);
|
|
300
|
+
this.createIndex(table, index, tableDef, createPrimary);
|
|
301
|
+
}
|
|
302
|
+
for (const check of tableDef.getChecks()) {
|
|
303
|
+
this.createCheck(table, check);
|
|
304
|
+
}
|
|
305
|
+
if (tableDef.comment) {
|
|
306
|
+
const comment = this.platform.quoteValue(tableDef.comment).replace(/^'|'$/g, '');
|
|
307
|
+
table.comment(comment);
|
|
308
|
+
}
|
|
309
|
+
if (!this.supportsSchemaConstraints()) {
|
|
310
|
+
for (const fk of Object.values(tableDef.getForeignKeys())) {
|
|
311
|
+
this.createForeignKey(table, fk);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
this.finalizeTable(table, this.platform.getConfig().get('charset'), this.platform.getConfig().get('collate'));
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
createForeignKey(table, foreignKey, schema) {
|
|
318
|
+
if (!this.options.createForeignKeyConstraints) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const builder = table
|
|
322
|
+
.foreign(foreignKey.columnNames, foreignKey.constraintName)
|
|
323
|
+
.references(foreignKey.referencedColumnNames)
|
|
324
|
+
.inTable(this.getReferencedTableName(foreignKey.referencedTableName, schema))
|
|
325
|
+
.withKeyName(foreignKey.constraintName);
|
|
326
|
+
if (foreignKey.localTableName !== foreignKey.referencedTableName || this.platform.supportsMultipleCascadePaths()) {
|
|
327
|
+
if (foreignKey.updateRule) {
|
|
328
|
+
builder.onUpdate(foreignKey.updateRule);
|
|
329
|
+
}
|
|
330
|
+
if (foreignKey.deleteRule) {
|
|
331
|
+
builder.onDelete(foreignKey.deleteRule);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (foreignKey.deferMode) {
|
|
335
|
+
builder.deferrable(foreignKey.deferMode);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
splitTableName(name) {
|
|
339
|
+
const parts = name.split('.');
|
|
340
|
+
const tableName = parts.pop();
|
|
341
|
+
const schemaName = parts.pop();
|
|
342
|
+
return [schemaName, tableName];
|
|
343
|
+
}
|
|
344
|
+
getReferencedTableName(referencedTableName, schema) {
|
|
345
|
+
const [schemaName, tableName] = this.splitTableName(referencedTableName);
|
|
346
|
+
schema = schemaName ?? schema ?? this.platform.getConfig().get('schema');
|
|
347
|
+
/* istanbul ignore next */
|
|
348
|
+
if (schema && schemaName === '*') {
|
|
349
|
+
return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
|
|
350
|
+
}
|
|
351
|
+
if (!schemaName || schemaName === this.platform.getDefaultSchemaName()) {
|
|
352
|
+
return tableName;
|
|
353
|
+
}
|
|
354
|
+
return `${schemaName}.${tableName}`;
|
|
355
|
+
}
|
|
356
|
+
createIndex(table, index, tableDef, createPrimary = false) {
|
|
357
|
+
if (index.primary && !createPrimary) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (index.expression) {
|
|
361
|
+
this.pushTableQuery(table, index.expression);
|
|
362
|
+
}
|
|
363
|
+
else if (index.primary) {
|
|
364
|
+
const keyName = this.hasNonDefaultPrimaryKeyName(tableDef) ? index.keyName : undefined;
|
|
365
|
+
table.primary(index.columnNames, keyName);
|
|
366
|
+
}
|
|
367
|
+
else if (index.unique) {
|
|
368
|
+
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
369
|
+
if (index.columnNames.some(column => column.includes('.'))) {
|
|
370
|
+
const columns = this.platform.getJsonIndexDefinition(index);
|
|
371
|
+
table.index(columns.map(column => this.knex.raw(column)), index.keyName, { indexType: 'unique' });
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
table.unique(index.columnNames, { indexName: index.keyName, deferrable: index.deferMode });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
else if (index.type === 'fulltext') {
|
|
378
|
+
const columns = index.columnNames.map(name => ({ name, type: tableDef.getColumn(name).type }));
|
|
379
|
+
if (this.platform.supportsCreatingFullTextIndex()) {
|
|
380
|
+
this.pushTableQuery(table, this.platform.getFullTextIndexExpression(index.keyName, tableDef.schema, tableDef.name, columns));
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
385
|
+
if (index.columnNames.some(column => column.includes('.'))) {
|
|
386
|
+
const columns = this.platform.getJsonIndexDefinition(index);
|
|
387
|
+
table.index(columns.map(column => this.knex.raw(column)), index.keyName, index.type);
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
table.index(index.columnNames, index.keyName, index.type);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
createCheck(table, check) {
|
|
395
|
+
table.check(check.expression, {}, check.name);
|
|
396
|
+
}
|
|
397
|
+
createSchemaBuilder(schema) {
|
|
398
|
+
const builder = this.knex.schema;
|
|
399
|
+
if (schema && schema !== this.platform.getDefaultSchemaName()) {
|
|
400
|
+
builder.withSchema(schema);
|
|
401
|
+
}
|
|
402
|
+
return builder;
|
|
403
|
+
}
|
|
404
|
+
get knex() {
|
|
405
|
+
const connection = this.platform.getConfig().getDriver().getConnection();
|
|
406
|
+
return connection.getKnex();
|
|
407
|
+
}
|
|
408
|
+
get options() {
|
|
409
|
+
return this.platform.getConfig().get('schemaGenerator');
|
|
410
|
+
}
|
|
276
411
|
}
|
|
277
412
|
exports.SchemaHelper = SchemaHelper;
|
|
@@ -5,10 +5,10 @@ import type { AbstractSqlDriver } from '../AbstractSqlDriver';
|
|
|
5
5
|
export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver> implements ISchemaGenerator {
|
|
6
6
|
protected readonly helper: import("./SchemaHelper").SchemaHelper;
|
|
7
7
|
protected readonly options: {
|
|
8
|
-
disableForeignKeys?: boolean
|
|
9
|
-
createForeignKeyConstraints?: boolean
|
|
10
|
-
ignoreSchema?: string[]
|
|
11
|
-
managementDbName?: string
|
|
8
|
+
disableForeignKeys?: boolean;
|
|
9
|
+
createForeignKeyConstraints?: boolean;
|
|
10
|
+
ignoreSchema?: string[];
|
|
11
|
+
managementDbName?: string;
|
|
12
12
|
};
|
|
13
13
|
protected lastEnsuredDatabase?: string;
|
|
14
14
|
static register(orm: MikroORM): void;
|
|
@@ -38,14 +38,11 @@ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<Abstract
|
|
|
38
38
|
dropTables?: boolean;
|
|
39
39
|
schema?: string;
|
|
40
40
|
}): Promise<string>;
|
|
41
|
-
private getReferencedTableName;
|
|
42
|
-
private createForeignKey;
|
|
43
41
|
/**
|
|
44
42
|
* We need to drop foreign keys first for all tables to allow dropping PK constraints.
|
|
45
43
|
*/
|
|
46
44
|
private preAlterTable;
|
|
47
45
|
private postAlterTable;
|
|
48
|
-
private splitTableName;
|
|
49
46
|
private alterTable;
|
|
50
47
|
/**
|
|
51
48
|
* creates new database and connects to it
|
|
@@ -57,11 +54,7 @@ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<Abstract
|
|
|
57
54
|
ctx?: Transaction;
|
|
58
55
|
}): Promise<void>;
|
|
59
56
|
private wrapSchema;
|
|
60
|
-
private createSchemaBuilder;
|
|
61
|
-
private createTable;
|
|
62
|
-
private createIndex;
|
|
63
57
|
private dropIndex;
|
|
64
|
-
private createCheck;
|
|
65
58
|
private dropCheck;
|
|
66
59
|
private dropTable;
|
|
67
60
|
private createForeignKeys;
|
|
@@ -59,7 +59,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
59
59
|
continue;
|
|
60
60
|
}
|
|
61
61
|
const sql = this.helper.getCreateNamespaceSQL(namespace);
|
|
62
|
-
ret += await this.dump(
|
|
62
|
+
ret += await this.dump(sql, '\n');
|
|
63
63
|
}
|
|
64
64
|
if (this.platform.supportsNativeEnums()) {
|
|
65
65
|
const created = [];
|
|
@@ -70,14 +70,14 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
70
70
|
}
|
|
71
71
|
created.push(enumName);
|
|
72
72
|
const sql = this.helper.getCreateNativeEnumSQL(enumOptions.name, enumOptions.items, this.getSchemaName(enumOptions, options));
|
|
73
|
-
ret += await this.dump(
|
|
73
|
+
ret += await this.dump(sql, '\n');
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
for (const tableDef of toSchema.getTables()) {
|
|
77
|
-
ret += await this.dump(this.createTable(tableDef));
|
|
77
|
+
ret += await this.dump(this.helper.createTable(tableDef));
|
|
78
78
|
}
|
|
79
79
|
for (const tableDef of toSchema.getTables()) {
|
|
80
|
-
ret += await this.dump(this.createSchemaBuilder(tableDef.schema).alterTable(tableDef.name, table => this.createForeignKeys(table, tableDef, options.schema)));
|
|
80
|
+
ret += await this.dump(this.helper.createSchemaBuilder(tableDef.schema).alterTable(tableDef.name, table => this.createForeignKeys(table, tableDef, options.schema)));
|
|
81
81
|
}
|
|
82
82
|
return this.wrapSchema(ret, { wrap });
|
|
83
83
|
}
|
|
@@ -124,7 +124,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
124
124
|
const table = schema.getTable(meta.tableName);
|
|
125
125
|
if (!this.platform.usesCascadeStatement() && table && (!wrap || options.dropForeignKeys)) {
|
|
126
126
|
for (const fk of Object.values(table.getForeignKeys())) {
|
|
127
|
-
const builder = this.createSchemaBuilder(table.schema).alterTable(table.name, tbl => {
|
|
127
|
+
const builder = this.helper.createSchemaBuilder(table.schema).alterTable(table.name, tbl => {
|
|
128
128
|
tbl.dropForeign(fk.columnNames, fk.constraintName);
|
|
129
129
|
});
|
|
130
130
|
ret += await this.dump(builder, '\n');
|
|
@@ -137,7 +137,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
137
137
|
if (this.platform.supportsNativeEnums()) {
|
|
138
138
|
for (const columnName of Object.keys(schema.getNativeEnums())) {
|
|
139
139
|
const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
|
|
140
|
-
ret += await this.dump(
|
|
140
|
+
ret += await this.dump(sql, '\n');
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
if (options.dropMigrationsTable) {
|
|
@@ -191,28 +191,28 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
191
191
|
if (this.platform.supportsSchemas()) {
|
|
192
192
|
for (const newNamespace of schemaDiff.newNamespaces) {
|
|
193
193
|
const sql = this.helper.getCreateNamespaceSQL(newNamespace);
|
|
194
|
-
ret += await this.dump(
|
|
194
|
+
ret += await this.dump(sql, '\n');
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
if (this.platform.supportsNativeEnums()) {
|
|
198
198
|
for (const newNativeEnum of schemaDiff.newNativeEnums) {
|
|
199
199
|
const sql = this.helper.getCreateNativeEnumSQL(newNativeEnum.name, newNativeEnum.items, this.getSchemaName(newNativeEnum, options));
|
|
200
|
-
ret += await this.dump(
|
|
200
|
+
ret += await this.dump(sql, '\n');
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
|
-
if (!options.safe) {
|
|
203
|
+
if (!options.safe && this.options.createForeignKeyConstraints) {
|
|
204
204
|
for (const orphanedForeignKey of schemaDiff.orphanedForeignKeys) {
|
|
205
|
-
const [schemaName, tableName] = this.splitTableName(orphanedForeignKey.localTableName);
|
|
206
|
-
ret += await this.dump(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
205
|
+
const [schemaName, tableName] = this.helper.splitTableName(orphanedForeignKey.localTableName);
|
|
206
|
+
ret += await this.dump(this.helper.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
207
207
|
return table.dropForeign(orphanedForeignKey.columnNames, orphanedForeignKey.constraintName);
|
|
208
208
|
}));
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
for (const newTable of Object.values(schemaDiff.newTables)) {
|
|
212
|
-
ret += await this.dump(this.createTable(newTable, true));
|
|
212
|
+
ret += await this.dump(this.helper.createTable(newTable, true));
|
|
213
213
|
}
|
|
214
214
|
for (const newTable of Object.values(schemaDiff.newTables)) {
|
|
215
|
-
ret += await this.dump(this.createSchemaBuilder(newTable.schema).alterTable(newTable.name, table => {
|
|
215
|
+
ret += await this.dump(this.helper.createSchemaBuilder(newTable.schema).alterTable(newTable.name, table => {
|
|
216
216
|
this.createForeignKeys(table, newTable, options.schema);
|
|
217
217
|
}));
|
|
218
218
|
}
|
|
@@ -228,7 +228,11 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
228
228
|
}
|
|
229
229
|
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
230
230
|
for (const builder of this.alterTable(changedTable, options.safe)) {
|
|
231
|
-
|
|
231
|
+
let diff = await this.dump(builder);
|
|
232
|
+
if (diff.includes('CREATE TABLE `_knex_temp_alter') && this.helper.getAlterTable) {
|
|
233
|
+
diff = await this.helper.getAlterTable(changedTable, options.wrap);
|
|
234
|
+
}
|
|
235
|
+
ret += diff;
|
|
232
236
|
}
|
|
233
237
|
}
|
|
234
238
|
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
@@ -239,50 +243,17 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
239
243
|
if (!options.safe && this.platform.supportsNativeEnums()) {
|
|
240
244
|
for (const removedNativeEnum of schemaDiff.removedNativeEnums) {
|
|
241
245
|
const sql = this.helper.getDropNativeEnumSQL(removedNativeEnum.name, removedNativeEnum.schema);
|
|
242
|
-
ret += await this.dump(
|
|
246
|
+
ret += await this.dump(sql, '\n');
|
|
243
247
|
}
|
|
244
248
|
}
|
|
245
249
|
if (options.dropTables && !options.safe) {
|
|
246
250
|
for (const removedNamespace of schemaDiff.removedNamespaces) {
|
|
247
251
|
const sql = this.helper.getDropNamespaceSQL(removedNamespace);
|
|
248
|
-
ret += await this.dump(
|
|
252
|
+
ret += await this.dump(sql, '\n');
|
|
249
253
|
}
|
|
250
254
|
}
|
|
251
255
|
return this.wrapSchema(ret, options);
|
|
252
256
|
}
|
|
253
|
-
getReferencedTableName(referencedTableName, schema) {
|
|
254
|
-
const [schemaName, tableName] = this.splitTableName(referencedTableName);
|
|
255
|
-
schema = schemaName ?? schema ?? this.config.get('schema');
|
|
256
|
-
/* istanbul ignore next */
|
|
257
|
-
if (schema && schemaName === '*') {
|
|
258
|
-
return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
|
|
259
|
-
}
|
|
260
|
-
if (!schemaName || schemaName === this.platform.getDefaultSchemaName()) {
|
|
261
|
-
return tableName;
|
|
262
|
-
}
|
|
263
|
-
return `${schemaName}.${tableName}`;
|
|
264
|
-
}
|
|
265
|
-
createForeignKey(table, foreignKey, schema) {
|
|
266
|
-
if (!this.options.createForeignKeyConstraints) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const builder = table
|
|
270
|
-
.foreign(foreignKey.columnNames, foreignKey.constraintName)
|
|
271
|
-
.references(foreignKey.referencedColumnNames)
|
|
272
|
-
.inTable(this.getReferencedTableName(foreignKey.referencedTableName, schema))
|
|
273
|
-
.withKeyName(foreignKey.constraintName);
|
|
274
|
-
if (foreignKey.localTableName !== foreignKey.referencedTableName || this.platform.supportsMultipleCascadePaths()) {
|
|
275
|
-
if (foreignKey.updateRule) {
|
|
276
|
-
builder.onUpdate(foreignKey.updateRule);
|
|
277
|
-
}
|
|
278
|
-
if (foreignKey.deleteRule) {
|
|
279
|
-
builder.onDelete(foreignKey.deleteRule);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
if (foreignKey.deferMode) {
|
|
283
|
-
builder.deferrable(foreignKey.deferMode);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
257
|
/**
|
|
287
258
|
* We need to drop foreign keys first for all tables to allow dropping PK constraints.
|
|
288
259
|
*/
|
|
@@ -290,8 +261,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
290
261
|
const ret = [];
|
|
291
262
|
const push = (sql) => sql ? ret.push(this.knex.schema.raw(sql)) : undefined;
|
|
292
263
|
push(this.helper.getPreAlterTable(diff, safe));
|
|
293
|
-
const [schemaName, tableName] = this.splitTableName(diff.name);
|
|
294
|
-
ret.push(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
264
|
+
const [schemaName, tableName] = this.helper.splitTableName(diff.name);
|
|
265
|
+
ret.push(this.helper.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
295
266
|
for (const foreignKey of Object.values(diff.removedForeignKeys)) {
|
|
296
267
|
table.dropForeign(foreignKey.columnNames, foreignKey.constraintName);
|
|
297
268
|
}
|
|
@@ -307,15 +278,9 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
307
278
|
push(this.helper.getPostAlterTable(diff, safe));
|
|
308
279
|
return ret;
|
|
309
280
|
}
|
|
310
|
-
splitTableName(name) {
|
|
311
|
-
const parts = name.split('.');
|
|
312
|
-
const tableName = parts.pop();
|
|
313
|
-
const schemaName = parts.pop();
|
|
314
|
-
return [schemaName, tableName];
|
|
315
|
-
}
|
|
316
281
|
alterTable(diff, safe) {
|
|
317
282
|
const ret = [];
|
|
318
|
-
const [schemaName, tableName] = this.splitTableName(diff.name);
|
|
283
|
+
const [schemaName, tableName] = this.helper.splitTableName(diff.name);
|
|
319
284
|
if (this.platform.supportsNativeEnums()) {
|
|
320
285
|
const changedNativeEnums = [];
|
|
321
286
|
for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
|
|
@@ -331,10 +296,10 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
331
296
|
// postgres allows only adding new items, the values are case insensitive
|
|
332
297
|
itemsOld = itemsOld.map(v => v.toLowerCase());
|
|
333
298
|
const newItems = itemsNew.filter(val => !itemsOld.includes(val.toLowerCase()));
|
|
334
|
-
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(enumName, schemaName, val))));
|
|
299
|
+
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(enumName, schemaName, val, itemsNew, itemsOld))));
|
|
335
300
|
});
|
|
336
301
|
}
|
|
337
|
-
ret.push(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
302
|
+
ret.push(this.helper.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
338
303
|
for (const index of Object.values(diff.removedIndexes)) {
|
|
339
304
|
this.dropIndex(table, index);
|
|
340
305
|
}
|
|
@@ -352,7 +317,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
352
317
|
this.helper.pushTableQuery(table, this.helper.getDropColumnsSQL(tableName, Object.values(diff.removedColumns), schemaName));
|
|
353
318
|
}
|
|
354
319
|
}));
|
|
355
|
-
ret.push(this.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
320
|
+
ret.push(this.helper.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
356
321
|
for (const column of Object.values(diff.addedColumns)) {
|
|
357
322
|
const col = this.helper.createTableColumn(table, column, diff.fromTable, undefined, true);
|
|
358
323
|
this.helper.configureColumn(column, col, this.knex);
|
|
@@ -360,7 +325,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
360
325
|
if (foreignKey && this.options.createForeignKeyConstraints) {
|
|
361
326
|
delete diff.addedForeignKeys[foreignKey.constraintName];
|
|
362
327
|
const builder = col.references(foreignKey.referencedColumnNames[0])
|
|
363
|
-
.inTable(this.getReferencedTableName(foreignKey.referencedTableName))
|
|
328
|
+
.inTable(this.helper.getReferencedTableName(foreignKey.referencedTableName))
|
|
364
329
|
.withKeyName(foreignKey.constraintName)
|
|
365
330
|
.onUpdate(foreignKey.updateRule)
|
|
366
331
|
.onDelete(foreignKey.deleteRule);
|
|
@@ -394,31 +359,31 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
394
359
|
this.helper.pushTableQuery(table, this.helper.getRenameColumnSQL(tableName, oldColumnName, column, schemaName));
|
|
395
360
|
}
|
|
396
361
|
for (const foreignKey of Object.values(diff.addedForeignKeys)) {
|
|
397
|
-
this.createForeignKey(table, foreignKey);
|
|
362
|
+
this.helper.createForeignKey(table, foreignKey, undefined);
|
|
398
363
|
}
|
|
399
364
|
for (const foreignKey of Object.values(diff.changedForeignKeys)) {
|
|
400
|
-
this.createForeignKey(table, foreignKey);
|
|
365
|
+
this.helper.createForeignKey(table, foreignKey, undefined);
|
|
401
366
|
}
|
|
402
367
|
for (const index of Object.values(diff.addedIndexes)) {
|
|
403
|
-
this.createIndex(table, index, diff.toTable);
|
|
368
|
+
this.helper.createIndex(table, index, diff.toTable);
|
|
404
369
|
}
|
|
405
370
|
for (const index of Object.values(diff.changedIndexes)) {
|
|
406
|
-
this.createIndex(table, index, diff.toTable, true);
|
|
371
|
+
this.helper.createIndex(table, index, diff.toTable, true);
|
|
407
372
|
}
|
|
408
373
|
for (const [oldIndexName, index] of Object.entries(diff.renamedIndexes)) {
|
|
409
374
|
if (index.unique) {
|
|
410
375
|
this.dropIndex(table, index, oldIndexName);
|
|
411
|
-
this.createIndex(table, index, diff.toTable);
|
|
376
|
+
this.helper.createIndex(table, index, diff.toTable);
|
|
412
377
|
}
|
|
413
378
|
else {
|
|
414
379
|
this.helper.pushTableQuery(table, this.helper.getRenameIndexSQL(diff.name, index, oldIndexName));
|
|
415
380
|
}
|
|
416
381
|
}
|
|
417
382
|
for (const check of Object.values(diff.addedChecks)) {
|
|
418
|
-
this.createCheck(table, check);
|
|
383
|
+
this.helper.createCheck(table, check);
|
|
419
384
|
}
|
|
420
385
|
for (const check of Object.values(diff.changedChecks)) {
|
|
421
|
-
this.createCheck(table, check);
|
|
386
|
+
this.helper.createCheck(table, check);
|
|
422
387
|
}
|
|
423
388
|
if ('changedComment' in diff) {
|
|
424
389
|
const comment = diff.changedComment ? this.platform.quoteValue(diff.changedComment).replace(/^'|'$/g, '') : '';
|
|
@@ -483,76 +448,6 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
483
448
|
ret += this.helper.getSchemaEnd();
|
|
484
449
|
return ret;
|
|
485
450
|
}
|
|
486
|
-
createSchemaBuilder(schema) {
|
|
487
|
-
const builder = this.knex.schema;
|
|
488
|
-
if (schema && schema !== this.platform.getDefaultSchemaName()) {
|
|
489
|
-
builder.withSchema(schema);
|
|
490
|
-
}
|
|
491
|
-
return builder;
|
|
492
|
-
}
|
|
493
|
-
createTable(tableDef, alter) {
|
|
494
|
-
return this.createSchemaBuilder(tableDef.schema).createTable(tableDef.name, table => {
|
|
495
|
-
tableDef.getColumns().forEach(column => {
|
|
496
|
-
const col = this.helper.createTableColumn(table, column, tableDef, undefined, alter);
|
|
497
|
-
this.helper.configureColumn(column, col, this.knex);
|
|
498
|
-
});
|
|
499
|
-
for (const index of tableDef.getIndexes()) {
|
|
500
|
-
const createPrimary = !tableDef.getColumns().some(c => c.autoincrement && c.primary) || this.helper.hasNonDefaultPrimaryKeyName(tableDef);
|
|
501
|
-
this.createIndex(table, index, tableDef, createPrimary);
|
|
502
|
-
}
|
|
503
|
-
for (const check of tableDef.getChecks()) {
|
|
504
|
-
this.createCheck(table, check);
|
|
505
|
-
}
|
|
506
|
-
if (tableDef.comment) {
|
|
507
|
-
const comment = this.platform.quoteValue(tableDef.comment).replace(/^'|'$/g, '');
|
|
508
|
-
table.comment(comment);
|
|
509
|
-
}
|
|
510
|
-
if (!this.helper.supportsSchemaConstraints()) {
|
|
511
|
-
for (const fk of Object.values(tableDef.getForeignKeys())) {
|
|
512
|
-
this.createForeignKey(table, fk);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
this.helper.finalizeTable(table, this.config.get('charset'), this.config.get('collate'));
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
createIndex(table, index, tableDef, createPrimary = false) {
|
|
519
|
-
if (index.primary && !createPrimary) {
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
if (index.primary) {
|
|
523
|
-
const keyName = this.helper.hasNonDefaultPrimaryKeyName(tableDef) ? index.keyName : undefined;
|
|
524
|
-
table.primary(index.columnNames, keyName);
|
|
525
|
-
}
|
|
526
|
-
else if (index.unique) {
|
|
527
|
-
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
528
|
-
if (index.columnNames.some(column => column.includes('.'))) {
|
|
529
|
-
const columns = this.platform.getJsonIndexDefinition(index);
|
|
530
|
-
table.index(columns.map(column => this.knex.raw(column)), index.keyName, { indexType: 'unique' });
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
table.unique(index.columnNames, { indexName: index.keyName, deferrable: index.deferMode });
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
else if (index.expression) {
|
|
537
|
-
this.helper.pushTableQuery(table, index.expression);
|
|
538
|
-
}
|
|
539
|
-
else if (index.type === 'fulltext') {
|
|
540
|
-
const columns = index.columnNames.map(name => ({ name, type: tableDef.getColumn(name).type }));
|
|
541
|
-
if (this.platform.supportsCreatingFullTextIndex()) {
|
|
542
|
-
this.helper.pushTableQuery(table, this.platform.getFullTextIndexExpression(index.keyName, tableDef.schema, tableDef.name, columns));
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
else {
|
|
546
|
-
// JSON columns can have unique index but not unique constraint, and we need to distinguish those, so we can properly drop them
|
|
547
|
-
if (index.columnNames.some(column => column.includes('.'))) {
|
|
548
|
-
const columns = this.platform.getJsonIndexDefinition(index);
|
|
549
|
-
table.index(columns.map(column => this.knex.raw(column)), index.keyName, index.type);
|
|
550
|
-
}
|
|
551
|
-
else {
|
|
552
|
-
table.index(index.columnNames, index.keyName, index.type);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
451
|
dropIndex(table, index, oldIndexName = index.keyName) {
|
|
557
452
|
if (index.primary) {
|
|
558
453
|
table.dropPrimary(oldIndexName);
|
|
@@ -564,14 +459,11 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
564
459
|
table.dropIndex(index.columnNames, oldIndexName);
|
|
565
460
|
}
|
|
566
461
|
}
|
|
567
|
-
createCheck(table, check) {
|
|
568
|
-
table.check(check.expression, {}, check.name);
|
|
569
|
-
}
|
|
570
462
|
dropCheck(table, check) {
|
|
571
463
|
table.dropChecks(check.name);
|
|
572
464
|
}
|
|
573
465
|
dropTable(name, schema) {
|
|
574
|
-
let builder = this.createSchemaBuilder(schema).dropTableIfExists(name);
|
|
466
|
+
let builder = this.helper.createSchemaBuilder(schema).dropTableIfExists(name);
|
|
575
467
|
if (this.platform.usesCascadeStatement()) {
|
|
576
468
|
builder = this.knex.schema.raw(builder.toQuery() + ' cascade');
|
|
577
469
|
}
|
|
@@ -582,18 +474,11 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
582
474
|
return;
|
|
583
475
|
}
|
|
584
476
|
for (const fk of Object.values(tableDef.getForeignKeys())) {
|
|
585
|
-
this.createForeignKey(table, fk, schema);
|
|
477
|
+
this.helper.createForeignKey(table, fk, schema);
|
|
586
478
|
}
|
|
587
479
|
}
|
|
588
480
|
async dump(builder, append = '\n\n') {
|
|
589
|
-
|
|
590
|
-
const queries = [...sql.pre, ...sql.sql, ...sql.post];
|
|
591
|
-
if (queries.length === 0) {
|
|
592
|
-
return '';
|
|
593
|
-
}
|
|
594
|
-
const dump = `${queries.map(q => typeof q === 'object' ? q.sql : q).join(';\n')};${append}`;
|
|
595
|
-
const tmp = dump.replace(/pragma table_.+/ig, '').replace(/\n\n+/g, '\n').trim();
|
|
596
|
-
return tmp ? tmp + append : '';
|
|
481
|
+
return this.helper.dump(builder, append);
|
|
597
482
|
}
|
|
598
483
|
get knex() {
|
|
599
484
|
return this.connection.getKnex();
|
package/typings.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export interface Column {
|
|
|
47
47
|
unique?: boolean;
|
|
48
48
|
/** mysql only */
|
|
49
49
|
extra?: string;
|
|
50
|
-
ignoreSchemaChanges?: ('type' | 'extra')[];
|
|
50
|
+
ignoreSchemaChanges?: ('type' | 'extra' | 'default')[];
|
|
51
51
|
}
|
|
52
52
|
export interface ForeignKey {
|
|
53
53
|
columnNames: string[];
|
|
@@ -129,6 +129,8 @@ export interface IQueryBuilder<T> {
|
|
|
129
129
|
readonly alias: string;
|
|
130
130
|
readonly type?: QueryType;
|
|
131
131
|
_fields?: Field<T>[];
|
|
132
|
+
/** @internal */
|
|
133
|
+
helper: any;
|
|
132
134
|
select(fields: Field<T> | Field<T>[], distinct?: boolean): this;
|
|
133
135
|
addSelect(fields: string | string[]): this;
|
|
134
136
|
from<T extends AnyEntity<T> = AnyEntity>(target: EntityName<T> | IQueryBuilder<T>, aliasName?: string): IQueryBuilder<T>;
|