@mikro-orm/knex 6.4.6-dev.9 → 7.0.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AbstractSqlConnection.d.ts +15 -25
- package/AbstractSqlConnection.js +101 -126
- package/AbstractSqlDriver.d.ts +20 -13
- package/AbstractSqlDriver.js +73 -54
- package/AbstractSqlPlatform.d.ts +15 -3
- package/AbstractSqlPlatform.js +32 -11
- package/README.md +0 -2
- package/SqlEntityManager.d.ts +5 -6
- package/SqlEntityManager.js +5 -5
- package/SqlEntityRepository.d.ts +1 -6
- package/SqlEntityRepository.js +0 -6
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -0
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +161 -0
- package/dialects/mssql/index.d.ts +1 -1
- package/dialects/mssql/index.js +1 -1
- package/dialects/mysql/MySqlExceptionConverter.js +1 -0
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +7 -0
- package/dialects/mysql/MySqlNativeQueryBuilder.js +81 -0
- package/dialects/mysql/MySqlPlatform.d.ts +10 -2
- package/dialects/mysql/MySqlPlatform.js +23 -1
- package/dialects/mysql/MySqlSchemaHelper.d.ts +6 -12
- package/dialects/mysql/MySqlSchemaHelper.js +42 -75
- package/dialects/mysql/index.d.ts +1 -3
- package/dialects/mysql/index.js +1 -3
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +5 -0
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +12 -0
- package/dialects/postgresql/index.d.ts +1 -1
- package/dialects/postgresql/index.js +1 -1
- package/dialects/sqlite/BaseSqliteConnection.d.ts +0 -5
- package/dialects/sqlite/BaseSqliteConnection.js +4 -42
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +15 -3
- package/dialects/sqlite/BaseSqlitePlatform.js +20 -4
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +9 -0
- package/dialects/sqlite/SqliteExceptionConverter.js +55 -0
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +6 -0
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +15 -0
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +38 -0
- package/dialects/sqlite/SqliteSchemaHelper.js +384 -0
- package/dialects/sqlite/index.d.ts +3 -5
- package/dialects/sqlite/index.js +3 -5
- package/index.d.ts +1 -2
- package/index.js +3 -5
- package/index.mjs +10 -13
- package/package.json +4 -18
- package/query/CriteriaNodeFactory.js +5 -5
- package/query/NativeQueryBuilder.d.ts +108 -0
- package/query/NativeQueryBuilder.js +429 -0
- package/query/ObjectCriteriaNode.js +3 -3
- package/query/QueryBuilder.d.ts +30 -34
- package/query/QueryBuilder.js +112 -123
- package/query/QueryBuilderHelper.d.ts +27 -23
- package/query/QueryBuilderHelper.js +174 -168
- package/query/ScalarCriteriaNode.js +4 -0
- package/query/index.d.ts +1 -0
- package/query/index.js +1 -0
- package/schema/DatabaseSchema.js +9 -6
- package/schema/DatabaseTable.d.ts +2 -1
- package/schema/DatabaseTable.js +9 -5
- package/schema/SchemaComparator.d.ts +1 -2
- package/schema/SchemaComparator.js +31 -18
- package/schema/SchemaHelper.d.ts +27 -33
- package/schema/SchemaHelper.js +294 -184
- package/schema/SqlSchemaGenerator.d.ts +3 -9
- package/schema/SqlSchemaGenerator.js +105 -229
- package/typings.d.ts +7 -17
- package/MonkeyPatchable.d.ts +0 -18
- package/MonkeyPatchable.js +0 -60
- package/dialects/mssql/MsSqlColumnCompiler.d.ts +0 -4
- package/dialects/mssql/MsSqlColumnCompiler.js +0 -10
- package/dialects/mssql/MsSqlKnexDialect.d.ts +0 -6
- package/dialects/mssql/MsSqlKnexDialect.js +0 -22
- package/dialects/mssql/MsSqlQueryCompiler.d.ts +0 -12
- package/dialects/mssql/MsSqlQueryCompiler.js +0 -94
- package/dialects/mssql/MsSqlTableCompiler.d.ts +0 -9
- package/dialects/mssql/MsSqlTableCompiler.js +0 -40
- package/dialects/mysql/MariaDbKnexDialect.d.ts +0 -6
- package/dialects/mysql/MariaDbKnexDialect.js +0 -16
- package/dialects/mysql/MySqlColumnCompiler.d.ts +0 -7
- package/dialects/mysql/MySqlColumnCompiler.js +0 -26
- package/dialects/mysql/MySqlConnection.d.ts +0 -8
- package/dialects/mysql/MySqlConnection.js +0 -43
- package/dialects/mysql/MySqlKnexDialect.d.ts +0 -5
- package/dialects/mysql/MySqlKnexDialect.js +0 -17
- package/dialects/mysql/MySqlQueryCompiler.d.ts +0 -5
- package/dialects/mysql/MySqlQueryCompiler.js +0 -23
- package/dialects/postgresql/PostgreSqlKnexDialect.d.ts +0 -7
- package/dialects/postgresql/PostgreSqlKnexDialect.js +0 -20
- package/dialects/postgresql/PostgreSqlQueryCompiler.d.ts +0 -4
- package/dialects/postgresql/PostgreSqlQueryCompiler.js +0 -13
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +0 -11
- package/dialects/postgresql/PostgreSqlTableCompiler.js +0 -89
- package/dialects/sqlite/BaseSqliteSchemaHelper.d.ts +0 -28
- package/dialects/sqlite/BaseSqliteSchemaHelper.js +0 -200
- package/dialects/sqlite/BetterSqliteKnexDialect.d.ts +0 -5
- package/dialects/sqlite/BetterSqliteKnexDialect.js +0 -15
- package/dialects/sqlite/LibSqlKnexDialect.d.ts +0 -11
- package/dialects/sqlite/LibSqlKnexDialect.js +0 -85
- package/dialects/sqlite/SqliteKnexDialect.d.ts +0 -8
- package/dialects/sqlite/SqliteKnexDialect.js +0 -67
- package/dialects/sqlite/SqliteTableCompiler.d.ts +0 -6
- package/dialects/sqlite/SqliteTableCompiler.js +0 -71
|
@@ -52,13 +52,13 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
52
52
|
}
|
|
53
53
|
async getCreateSchemaSQL(options = {}) {
|
|
54
54
|
const toSchema = this.getTargetSchema(options.schema);
|
|
55
|
-
|
|
55
|
+
const ret = [];
|
|
56
56
|
for (const namespace of toSchema.getNamespaces()) {
|
|
57
57
|
if (namespace === this.platform.getDefaultSchemaName()) {
|
|
58
58
|
continue;
|
|
59
59
|
}
|
|
60
60
|
const sql = this.helper.getCreateNamespaceSQL(namespace);
|
|
61
|
-
|
|
61
|
+
this.append(ret, sql);
|
|
62
62
|
}
|
|
63
63
|
if (this.platform.supportsNativeEnums()) {
|
|
64
64
|
const created = [];
|
|
@@ -69,14 +69,17 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
69
69
|
}
|
|
70
70
|
created.push(enumName);
|
|
71
71
|
const sql = this.helper.getCreateNativeEnumSQL(enumOptions.name, enumOptions.items, this.getSchemaName(enumOptions, options));
|
|
72
|
-
|
|
72
|
+
this.append(ret, sql);
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
for (const
|
|
76
|
-
|
|
75
|
+
for (const table of toSchema.getTables()) {
|
|
76
|
+
this.append(ret, this.helper.createTable(table), true);
|
|
77
77
|
}
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
if (this.helper.supportsSchemaConstraints()) {
|
|
79
|
+
for (const table of toSchema.getTables()) {
|
|
80
|
+
const fks = Object.values(table.getForeignKeys()).map(fk => this.helper.createForeignKey(table, fk));
|
|
81
|
+
this.append(ret, fks, true);
|
|
82
|
+
}
|
|
80
83
|
}
|
|
81
84
|
return this.wrapSchema(ret, options);
|
|
82
85
|
}
|
|
@@ -109,6 +112,18 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
109
112
|
.truncate();
|
|
110
113
|
}
|
|
111
114
|
await this.execute(this.helper.enableForeignKeysSQL());
|
|
115
|
+
// const parts: string[] = [this.helper.disableForeignKeysSQL()];
|
|
116
|
+
//
|
|
117
|
+
// for (const meta of this.getOrderedMetadata(options?.schema).reverse()) {
|
|
118
|
+
// const query = this.driver.createQueryBuilder(meta.className, this.em?.getTransactionContext(), 'write', false)
|
|
119
|
+
// .withSchema(options?.schema)
|
|
120
|
+
// .truncate()
|
|
121
|
+
// .getFormattedQuery();
|
|
122
|
+
// parts.push(query);
|
|
123
|
+
// }
|
|
124
|
+
//
|
|
125
|
+
// parts.push(this.helper.enableForeignKeysSQL());
|
|
126
|
+
// await this.execute(parts.join(';\n'));
|
|
112
127
|
this.clearIdentityMap();
|
|
113
128
|
}
|
|
114
129
|
async getDropSchemaSQL(options = {}) {
|
|
@@ -116,37 +131,43 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
116
131
|
const metadata = this.getOrderedMetadata(options.schema).reverse();
|
|
117
132
|
const schemas = this.getTargetSchema(options.schema).getNamespaces();
|
|
118
133
|
const schema = await DatabaseSchema_1.DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas);
|
|
119
|
-
|
|
120
|
-
// remove FKs explicitly if we can't use cascading statement and we don't disable FK checks (we need this for circular relations)
|
|
134
|
+
const ret = [];
|
|
135
|
+
// remove FKs explicitly if we can't use a cascading statement and we don't disable FK checks (we need this for circular relations)
|
|
121
136
|
for (const meta of metadata) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
137
|
+
if (!this.platform.usesCascadeStatement() && (!this.options.disableForeignKeys || options.dropForeignKeys)) {
|
|
138
|
+
const table = schema.getTable(meta.tableName);
|
|
139
|
+
if (!table) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const foreignKeys = Object.values(table.getForeignKeys());
|
|
143
|
+
for (const fk of foreignKeys) {
|
|
144
|
+
this.append(ret, this.helper.dropForeignKey(table.getShortestName(), fk.constraintName));
|
|
129
145
|
}
|
|
130
146
|
}
|
|
131
147
|
}
|
|
132
148
|
for (const meta of metadata) {
|
|
133
|
-
|
|
149
|
+
this.append(ret, this.helper.dropTableIfExists(meta.tableName, this.getSchemaName(meta, options)));
|
|
134
150
|
}
|
|
135
151
|
if (this.platform.supportsNativeEnums()) {
|
|
136
152
|
for (const columnName of Object.keys(schema.getNativeEnums())) {
|
|
137
153
|
const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
|
|
138
|
-
|
|
154
|
+
this.append(ret, sql);
|
|
139
155
|
}
|
|
140
156
|
}
|
|
141
157
|
if (options.dropMigrationsTable) {
|
|
142
|
-
|
|
158
|
+
this.append(ret, this.helper.dropTableIfExists(this.config.get('migrations').tableName, this.config.get('schema')));
|
|
143
159
|
}
|
|
144
|
-
return this.wrapSchema(ret
|
|
160
|
+
return this.wrapSchema(ret, options);
|
|
145
161
|
}
|
|
146
162
|
getSchemaName(meta, options) {
|
|
147
163
|
const schemaName = options.schema ?? this.config.get('schema');
|
|
148
164
|
/* istanbul ignore next */
|
|
149
|
-
|
|
165
|
+
const resolvedName = meta.schema && meta.schema === '*' ? schemaName : (meta.schema ?? schemaName);
|
|
166
|
+
// skip default schema name
|
|
167
|
+
if (resolvedName === this.platform.getDefaultSchemaName()) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
return resolvedName;
|
|
150
171
|
}
|
|
151
172
|
async updateSchema(options = {}) {
|
|
152
173
|
const sql = await this.getUpdateSchemaSQL(options);
|
|
@@ -168,8 +189,8 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
168
189
|
const diffUp = comparator.compare(fromSchema, toSchema);
|
|
169
190
|
const diffDown = comparator.compare(toSchema, fromSchema, diffUp);
|
|
170
191
|
return {
|
|
171
|
-
up:
|
|
172
|
-
down: this.platform.supportsDownMigrations() ?
|
|
192
|
+
up: this.diffToSQL(diffUp, options),
|
|
193
|
+
down: this.platform.supportsDownMigrations() ? this.diffToSQL(diffDown, options) : '',
|
|
173
194
|
};
|
|
174
195
|
}
|
|
175
196
|
async prepareSchemaForComparison(options) {
|
|
@@ -183,70 +204,74 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
183
204
|
toSchema.prune(options.schema, wildcardSchemaTables);
|
|
184
205
|
return { fromSchema, toSchema };
|
|
185
206
|
}
|
|
186
|
-
|
|
187
|
-
|
|
207
|
+
diffToSQL(schemaDiff, options) {
|
|
208
|
+
const ret = [];
|
|
209
|
+
globalThis.idx = 0;
|
|
188
210
|
if (this.platform.supportsSchemas()) {
|
|
189
211
|
for (const newNamespace of schemaDiff.newNamespaces) {
|
|
190
212
|
const sql = this.helper.getCreateNamespaceSQL(newNamespace);
|
|
191
|
-
|
|
213
|
+
this.append(ret, sql);
|
|
192
214
|
}
|
|
193
215
|
}
|
|
194
216
|
if (this.platform.supportsNativeEnums()) {
|
|
195
217
|
for (const newNativeEnum of schemaDiff.newNativeEnums) {
|
|
196
218
|
const sql = this.helper.getCreateNativeEnumSQL(newNativeEnum.name, newNativeEnum.items, this.getSchemaName(newNativeEnum, options));
|
|
197
|
-
|
|
219
|
+
this.append(ret, sql);
|
|
198
220
|
}
|
|
199
221
|
}
|
|
200
222
|
if (!options.safe && this.options.createForeignKeyConstraints) {
|
|
201
223
|
for (const orphanedForeignKey of schemaDiff.orphanedForeignKeys) {
|
|
202
|
-
const [schemaName, tableName] = this.helper.splitTableName(orphanedForeignKey.localTableName);
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
224
|
+
const [schemaName, tableName] = this.helper.splitTableName(orphanedForeignKey.localTableName, true);
|
|
225
|
+
/* istanbul ignore if */
|
|
226
|
+
const name = (schemaName ? schemaName + '.' : '') + tableName;
|
|
227
|
+
this.append(ret, this.helper.dropForeignKey(name, orphanedForeignKey.constraintName));
|
|
228
|
+
}
|
|
229
|
+
if (schemaDiff.orphanedForeignKeys.length > 0) {
|
|
230
|
+
ret.push('');
|
|
206
231
|
}
|
|
207
232
|
}
|
|
208
233
|
for (const newTable of Object.values(schemaDiff.newTables)) {
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
234
|
+
this.append(ret, this.helper.createTable(newTable, true), true);
|
|
235
|
+
}
|
|
236
|
+
if (this.helper.supportsSchemaConstraints()) {
|
|
237
|
+
for (const newTable of Object.values(schemaDiff.newTables)) {
|
|
238
|
+
const sql = [];
|
|
239
|
+
if (this.options.createForeignKeyConstraints) {
|
|
240
|
+
const fks = Object.values(newTable.getForeignKeys()).map(fk => this.helper.createForeignKey(newTable, fk));
|
|
241
|
+
this.append(sql, fks);
|
|
242
|
+
}
|
|
243
|
+
for (const check of newTable.getChecks()) {
|
|
244
|
+
this.append(sql, this.helper.createCheck(newTable, check));
|
|
245
|
+
}
|
|
246
|
+
this.append(ret, sql, true);
|
|
247
|
+
}
|
|
215
248
|
}
|
|
216
249
|
if (options.dropTables && !options.safe) {
|
|
217
250
|
for (const table of Object.values(schemaDiff.removedTables)) {
|
|
218
|
-
|
|
251
|
+
this.append(ret, this.helper.dropTableIfExists(table.name, table.schema));
|
|
252
|
+
}
|
|
253
|
+
if (core_1.Utils.hasObjectKeys(schemaDiff.removedTables)) {
|
|
254
|
+
ret.push('');
|
|
219
255
|
}
|
|
220
256
|
}
|
|
221
257
|
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
222
|
-
|
|
223
|
-
ret += await this.dump(builder);
|
|
224
|
-
}
|
|
258
|
+
this.append(ret, this.preAlterTable(changedTable, options.safe), true);
|
|
225
259
|
}
|
|
226
260
|
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
227
|
-
|
|
228
|
-
let diff = await this.dump(builder);
|
|
229
|
-
if (diff.includes('CREATE TABLE `_knex_temp_alter') && this.helper.getAlterTable) {
|
|
230
|
-
diff = await this.helper.getAlterTable(changedTable, options.wrap);
|
|
231
|
-
}
|
|
232
|
-
ret += diff;
|
|
233
|
-
}
|
|
261
|
+
this.append(ret, this.helper.alterTable(changedTable, options.safe), true);
|
|
234
262
|
}
|
|
235
263
|
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
236
|
-
|
|
237
|
-
ret += await this.dump(builder);
|
|
238
|
-
}
|
|
264
|
+
this.append(ret, this.helper.getPostAlterTable(changedTable, options.safe), true);
|
|
239
265
|
}
|
|
240
266
|
if (!options.safe && this.platform.supportsNativeEnums()) {
|
|
241
267
|
for (const removedNativeEnum of schemaDiff.removedNativeEnums) {
|
|
242
|
-
|
|
243
|
-
ret += await this.dump(sql, '\n');
|
|
268
|
+
this.append(ret, this.helper.getDropNativeEnumSQL(removedNativeEnum.name, removedNativeEnum.schema));
|
|
244
269
|
}
|
|
245
270
|
}
|
|
246
271
|
if (options.dropTables && !options.safe) {
|
|
247
272
|
for (const removedNamespace of schemaDiff.removedNamespaces) {
|
|
248
273
|
const sql = this.helper.getDropNamespaceSQL(removedNamespace);
|
|
249
|
-
|
|
274
|
+
this.append(ret, sql);
|
|
250
275
|
}
|
|
251
276
|
}
|
|
252
277
|
return this.wrapSchema(ret, options);
|
|
@@ -256,144 +281,13 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
256
281
|
*/
|
|
257
282
|
preAlterTable(diff, safe) {
|
|
258
283
|
const ret = [];
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
for (const foreignKey of Object.values(diff.changedForeignKeys)) {
|
|
267
|
-
table.dropForeign(foreignKey.columnNames, foreignKey.constraintName);
|
|
268
|
-
}
|
|
269
|
-
}));
|
|
270
|
-
return ret;
|
|
271
|
-
}
|
|
272
|
-
postAlterTable(diff, safe) {
|
|
273
|
-
const ret = [];
|
|
274
|
-
const push = (sql) => sql ? ret.push(this.knex.schema.raw(sql)) : undefined;
|
|
275
|
-
push(this.helper.getPostAlterTable(diff, safe));
|
|
276
|
-
return ret;
|
|
277
|
-
}
|
|
278
|
-
alterTable(diff, safe) {
|
|
279
|
-
const ret = [];
|
|
280
|
-
const [schemaName, tableName] = this.helper.splitTableName(diff.name);
|
|
281
|
-
if (this.platform.supportsNativeEnums()) {
|
|
282
|
-
const changedNativeEnums = [];
|
|
283
|
-
for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
|
|
284
|
-
if (!column.nativeEnumName) {
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
const key = schemaName && schemaName !== this.platform.getDefaultSchemaName() && !column.nativeEnumName.includes('.')
|
|
288
|
-
? schemaName + '.' + column.nativeEnumName
|
|
289
|
-
: column.nativeEnumName;
|
|
290
|
-
if (changedProperties.has('enumItems') && key in diff.fromTable.nativeEnums) {
|
|
291
|
-
changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.nativeEnums[key].items]);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
core_1.Utils.removeDuplicates(changedNativeEnums).forEach(([enumName, itemsNew, itemsOld]) => {
|
|
295
|
-
// postgres allows only adding new items, the values are case insensitive
|
|
296
|
-
itemsOld = itemsOld.map(v => v.toLowerCase());
|
|
297
|
-
const newItems = itemsNew.filter(val => !itemsOld.includes(val.toLowerCase()));
|
|
298
|
-
if (enumName.includes('.')) {
|
|
299
|
-
const [enumSchemaName, rawEnumName] = enumName.split('.');
|
|
300
|
-
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(rawEnumName, enumSchemaName, val, itemsNew, itemsOld))));
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
ret.push(...newItems.map(val => this.knex.schema.raw(this.helper.getAlterNativeEnumSQL(enumName, schemaName, val, itemsNew, itemsOld))));
|
|
304
|
-
});
|
|
284
|
+
this.append(ret, this.helper.getPreAlterTable(diff, safe));
|
|
285
|
+
for (const foreignKey of Object.values(diff.removedForeignKeys)) {
|
|
286
|
+
ret.push(this.helper.dropForeignKey(diff.toTable.getShortestName(), foreignKey.constraintName));
|
|
287
|
+
}
|
|
288
|
+
for (const foreignKey of Object.values(diff.changedForeignKeys)) {
|
|
289
|
+
ret.push(this.helper.dropForeignKey(diff.toTable.getShortestName(), foreignKey.constraintName));
|
|
305
290
|
}
|
|
306
|
-
ret.push(this.helper.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
307
|
-
for (const index of Object.values(diff.removedIndexes)) {
|
|
308
|
-
this.dropIndex(table, index);
|
|
309
|
-
}
|
|
310
|
-
for (const index of Object.values(diff.changedIndexes)) {
|
|
311
|
-
this.dropIndex(table, index);
|
|
312
|
-
}
|
|
313
|
-
for (const check of Object.values(diff.removedChecks)) {
|
|
314
|
-
this.dropCheck(table, check);
|
|
315
|
-
}
|
|
316
|
-
for (const check of Object.values(diff.changedChecks)) {
|
|
317
|
-
this.dropCheck(table, check);
|
|
318
|
-
}
|
|
319
|
-
/* istanbul ignore else */
|
|
320
|
-
if (!safe && Object.values(diff.removedColumns).length > 0) {
|
|
321
|
-
this.helper.pushTableQuery(table, this.helper.getDropColumnsSQL(tableName, Object.values(diff.removedColumns), schemaName));
|
|
322
|
-
}
|
|
323
|
-
}));
|
|
324
|
-
ret.push(this.helper.createSchemaBuilder(schemaName).alterTable(tableName, table => {
|
|
325
|
-
for (const column of Object.values(diff.addedColumns)) {
|
|
326
|
-
const col = this.helper.createTableColumn(table, column, diff.fromTable, undefined, true);
|
|
327
|
-
this.helper.configureColumn(column, col, this.knex);
|
|
328
|
-
const foreignKey = Object.values(diff.addedForeignKeys).find(fk => fk.columnNames.length === 1 && fk.columnNames[0] === column.name);
|
|
329
|
-
if (foreignKey && this.options.createForeignKeyConstraints) {
|
|
330
|
-
delete diff.addedForeignKeys[foreignKey.constraintName];
|
|
331
|
-
const builder = col.references(foreignKey.referencedColumnNames[0])
|
|
332
|
-
.inTable(this.helper.getReferencedTableName(foreignKey.referencedTableName))
|
|
333
|
-
.withKeyName(foreignKey.constraintName)
|
|
334
|
-
.onUpdate(foreignKey.updateRule)
|
|
335
|
-
.onDelete(foreignKey.deleteRule);
|
|
336
|
-
if (foreignKey.deferMode) {
|
|
337
|
-
builder.deferrable(foreignKey.deferMode);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
|
|
342
|
-
if (changedProperties.size === 1 && changedProperties.has('comment')) {
|
|
343
|
-
continue;
|
|
344
|
-
}
|
|
345
|
-
if (changedProperties.size === 1 && changedProperties.has('enumItems') && column.nativeEnumName) {
|
|
346
|
-
continue;
|
|
347
|
-
}
|
|
348
|
-
const col = this.helper.createTableColumn(table, column, diff.fromTable, changedProperties, true)?.alter();
|
|
349
|
-
if (col) {
|
|
350
|
-
this.helper.configureColumn(column, col, this.knex, changedProperties);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
for (const { column } of Object.values(diff.changedColumns).filter(diff => diff.changedProperties.has('autoincrement'))) {
|
|
354
|
-
this.helper.pushTableQuery(table, this.helper.getAlterColumnAutoincrement(tableName, column, schemaName));
|
|
355
|
-
}
|
|
356
|
-
for (const { column, changedProperties } of Object.values(diff.changedColumns).filter(diff => diff.changedProperties.has('comment'))) {
|
|
357
|
-
if (['type', 'nullable', 'autoincrement', 'unsigned', 'default', 'enumItems'].some(t => changedProperties.has(t))) {
|
|
358
|
-
continue; // will be handled via knex
|
|
359
|
-
}
|
|
360
|
-
this.helper.pushTableQuery(table, this.helper.getChangeColumnCommentSQL(tableName, column, schemaName));
|
|
361
|
-
}
|
|
362
|
-
for (const [oldColumnName, column] of Object.entries(diff.renamedColumns)) {
|
|
363
|
-
this.helper.pushTableQuery(table, this.helper.getRenameColumnSQL(tableName, oldColumnName, column, schemaName));
|
|
364
|
-
}
|
|
365
|
-
for (const foreignKey of Object.values(diff.addedForeignKeys)) {
|
|
366
|
-
this.helper.createForeignKey(table, foreignKey, undefined);
|
|
367
|
-
}
|
|
368
|
-
for (const foreignKey of Object.values(diff.changedForeignKeys)) {
|
|
369
|
-
this.helper.createForeignKey(table, foreignKey, undefined);
|
|
370
|
-
}
|
|
371
|
-
for (const index of Object.values(diff.addedIndexes)) {
|
|
372
|
-
this.helper.createIndex(table, index, diff.toTable);
|
|
373
|
-
}
|
|
374
|
-
for (const index of Object.values(diff.changedIndexes)) {
|
|
375
|
-
this.helper.createIndex(table, index, diff.toTable, true);
|
|
376
|
-
}
|
|
377
|
-
for (const [oldIndexName, index] of Object.entries(diff.renamedIndexes)) {
|
|
378
|
-
if (index.unique) {
|
|
379
|
-
this.dropIndex(table, index, oldIndexName);
|
|
380
|
-
this.helper.createIndex(table, index, diff.toTable);
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
383
|
-
this.helper.pushTableQuery(table, this.helper.getRenameIndexSQL(diff.name, index, oldIndexName));
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
for (const check of Object.values(diff.addedChecks)) {
|
|
387
|
-
this.helper.createCheck(table, check);
|
|
388
|
-
}
|
|
389
|
-
for (const check of Object.values(diff.changedChecks)) {
|
|
390
|
-
this.helper.createCheck(table, check);
|
|
391
|
-
}
|
|
392
|
-
if ('changedComment' in diff) {
|
|
393
|
-
const comment = diff.changedComment ? this.platform.quoteValue(diff.changedComment).replace(/^'|'$/g, '') : '';
|
|
394
|
-
table.comment(comment);
|
|
395
|
-
}
|
|
396
|
-
}));
|
|
397
291
|
return ret;
|
|
398
292
|
}
|
|
399
293
|
/**
|
|
@@ -401,8 +295,9 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
401
295
|
*/
|
|
402
296
|
async createDatabase(name) {
|
|
403
297
|
name ??= this.config.get('dbName');
|
|
404
|
-
const sql = this.helper.getCreateDatabaseSQL('' + this.
|
|
298
|
+
const sql = this.helper.getCreateDatabaseSQL('' + this.platform.quoteIdentifier(name));
|
|
405
299
|
if (sql) {
|
|
300
|
+
// console.log(sql);
|
|
406
301
|
await this.execute(sql);
|
|
407
302
|
}
|
|
408
303
|
this.config.set('dbName', name);
|
|
@@ -436,55 +331,36 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
436
331
|
if (this.platform.supportsMultipleStatements()) {
|
|
437
332
|
for (const group of groups) {
|
|
438
333
|
const query = group.join('\n');
|
|
334
|
+
// console.log(query);
|
|
439
335
|
await this.driver.execute(query);
|
|
440
336
|
}
|
|
441
337
|
return;
|
|
442
338
|
}
|
|
339
|
+
// console.log(groups);
|
|
443
340
|
await core_1.Utils.runSerial(groups.flat(), line => this.driver.execute(line));
|
|
444
341
|
}
|
|
342
|
+
async dropTableIfExists(name, schema) {
|
|
343
|
+
const sql = this.helper.dropTableIfExists(name, schema);
|
|
344
|
+
return this.execute(sql);
|
|
345
|
+
}
|
|
445
346
|
wrapSchema(sql, options) {
|
|
446
|
-
|
|
447
|
-
|
|
347
|
+
const array = core_1.Utils.asArray(sql);
|
|
348
|
+
if (array.length === 0) {
|
|
349
|
+
return '';
|
|
350
|
+
}
|
|
351
|
+
if (array[array.length - 1] === '') {
|
|
352
|
+
array.pop();
|
|
353
|
+
}
|
|
354
|
+
if (options.wrap === false) {
|
|
355
|
+
return array.join('\n') + '\n';
|
|
448
356
|
}
|
|
449
357
|
let ret = this.helper.getSchemaBeginning(this.config.get('charset'), this.options.disableForeignKeys);
|
|
450
|
-
ret +=
|
|
358
|
+
ret += array.join('\n') + '\n';
|
|
451
359
|
ret += this.helper.getSchemaEnd(this.options.disableForeignKeys);
|
|
452
360
|
return ret;
|
|
453
361
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
table.dropPrimary(oldIndexName);
|
|
457
|
-
}
|
|
458
|
-
else if (index.unique && index.constraint) {
|
|
459
|
-
table.dropUnique(index.columnNames, oldIndexName);
|
|
460
|
-
}
|
|
461
|
-
else {
|
|
462
|
-
table.dropIndex(index.columnNames, oldIndexName);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
dropCheck(table, check) {
|
|
466
|
-
table.dropChecks(check.name);
|
|
467
|
-
}
|
|
468
|
-
dropTable(name, schema) {
|
|
469
|
-
let builder = this.helper.createSchemaBuilder(schema).dropTableIfExists(name);
|
|
470
|
-
if (this.platform.usesCascadeStatement()) {
|
|
471
|
-
builder = this.knex.schema.raw(builder.toQuery() + ' cascade');
|
|
472
|
-
}
|
|
473
|
-
return builder;
|
|
474
|
-
}
|
|
475
|
-
createForeignKeys(table, tableDef, schema) {
|
|
476
|
-
if (!this.helper.supportsSchemaConstraints()) {
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
for (const fk of Object.values(tableDef.getForeignKeys())) {
|
|
480
|
-
this.helper.createForeignKey(table, fk, schema);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
async dump(builder, append = '\n\n') {
|
|
484
|
-
return this.helper.dump(builder, append);
|
|
485
|
-
}
|
|
486
|
-
get knex() {
|
|
487
|
-
return this.connection.getKnex();
|
|
362
|
+
append(array, sql, pad) {
|
|
363
|
+
return this.helper.append(array, sql, pad);
|
|
488
364
|
}
|
|
489
365
|
}
|
|
490
366
|
exports.SqlSchemaGenerator = SqlSchemaGenerator;
|
package/typings.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import type { DeferMode, CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type, QueryFlag, AnyEntity, EntityName } from '@mikro-orm/core';
|
|
2
|
-
import type { Knex } from 'knex';
|
|
3
2
|
import type { JoinType, QueryType } from './query/enums';
|
|
4
3
|
import type { DatabaseSchema, DatabaseTable } from './schema';
|
|
4
|
+
import type { QueryBuilder } from './query/QueryBuilder';
|
|
5
|
+
import type { NativeQueryBuilder } from './query/NativeQueryBuilder';
|
|
5
6
|
export interface Table {
|
|
6
7
|
table_name: string;
|
|
7
8
|
schema_name?: string;
|
|
8
9
|
table_comment?: string;
|
|
9
10
|
}
|
|
10
|
-
export type KnexStringRef = Knex.Ref<string, {
|
|
11
|
-
[alias: string]: string;
|
|
12
|
-
}>;
|
|
13
11
|
type AnyString = string & {};
|
|
14
|
-
export type Field<T> = AnyString | keyof T | RawQueryFragment |
|
|
12
|
+
export type Field<T> = AnyString | keyof T | RawQueryFragment | QueryBuilder | NativeQueryBuilder;
|
|
15
13
|
export interface JoinOptions {
|
|
16
14
|
table: string;
|
|
17
15
|
schema?: string;
|
|
@@ -40,6 +38,7 @@ export interface Column {
|
|
|
40
38
|
precision?: number;
|
|
41
39
|
scale?: number;
|
|
42
40
|
default?: string | null;
|
|
41
|
+
defaultConstraint?: string;
|
|
43
42
|
comment?: string;
|
|
44
43
|
generated?: string;
|
|
45
44
|
nativeEnumName?: string;
|
|
@@ -72,7 +71,7 @@ export interface IndexDef {
|
|
|
72
71
|
type?: string | Readonly<{
|
|
73
72
|
indexType?: string;
|
|
74
73
|
storageEngineIndexType?: 'hash' | 'btree';
|
|
75
|
-
predicate?:
|
|
74
|
+
predicate?: string;
|
|
76
75
|
}>;
|
|
77
76
|
deferMode?: DeferMode;
|
|
78
77
|
}
|
|
@@ -128,7 +127,7 @@ export interface SchemaDifference {
|
|
|
128
127
|
}
|
|
129
128
|
export interface IQueryBuilder<T> {
|
|
130
129
|
readonly alias: string;
|
|
131
|
-
readonly type
|
|
130
|
+
readonly type: QueryType;
|
|
132
131
|
_fields?: Field<T>[];
|
|
133
132
|
/** @internal */
|
|
134
133
|
helper: any;
|
|
@@ -146,7 +145,7 @@ export interface IQueryBuilder<T> {
|
|
|
146
145
|
joinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
|
|
147
146
|
leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): this;
|
|
148
147
|
innerJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): this;
|
|
149
|
-
withSubQuery(subQuery:
|
|
148
|
+
withSubQuery(subQuery: RawQueryFragment | NativeQueryBuilder, alias: string): this;
|
|
150
149
|
where(cond: QBFilterQuery<T>, operator?: keyof typeof GroupOperator): this;
|
|
151
150
|
where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this;
|
|
152
151
|
andWhere(cond: QBFilterQuery<T>): this;
|
|
@@ -185,13 +184,4 @@ export interface ICriteriaNode<T extends object> {
|
|
|
185
184
|
getPath(addIndex?: boolean): string;
|
|
186
185
|
getPivotPath(path: string): string;
|
|
187
186
|
}
|
|
188
|
-
export type MySqlIncrementOptions = {
|
|
189
|
-
primaryKey?: boolean;
|
|
190
|
-
unsigned?: boolean;
|
|
191
|
-
type?: Column['type'];
|
|
192
|
-
};
|
|
193
|
-
export interface MySqlTableBuilder extends Knex.TableBuilder {
|
|
194
|
-
increments(columnName?: string, options?: MySqlIncrementOptions): Knex.ColumnBuilder;
|
|
195
|
-
bigIncrements(columnName?: string, options?: MySqlIncrementOptions): Knex.ColumnBuilder;
|
|
196
|
-
}
|
|
197
187
|
export {};
|
package/MonkeyPatchable.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export declare const MonkeyPatchable: {
|
|
2
|
-
Client: any;
|
|
3
|
-
QueryExecutioner: any;
|
|
4
|
-
MySqlDialect: any;
|
|
5
|
-
MySqlColumnCompiler: any;
|
|
6
|
-
MySqlQueryCompiler: any;
|
|
7
|
-
MsSqlColumnCompiler: any;
|
|
8
|
-
MsSqlTableCompiler: any;
|
|
9
|
-
MsSqlQueryCompiler: any;
|
|
10
|
-
MsSqlDialect: any;
|
|
11
|
-
PostgresDialect: any;
|
|
12
|
-
PostgresDialectTableCompiler: any;
|
|
13
|
-
PostgresQueryCompiler: any;
|
|
14
|
-
Sqlite3Dialect: any;
|
|
15
|
-
Sqlite3DialectTableCompiler: any;
|
|
16
|
-
BetterSqlite3Dialect: any;
|
|
17
|
-
TableCompiler: any;
|
|
18
|
-
};
|
package/MonkeyPatchable.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.MonkeyPatchable = void 0;
|
|
7
|
-
// @ts-ignore
|
|
8
|
-
const client_1 = __importDefault(require("knex/lib/client"));
|
|
9
|
-
// @ts-ignore
|
|
10
|
-
const query_executioner_1 = __importDefault(require("knex/lib/execution/internal/query-executioner"));
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
const mysql2_1 = __importDefault(require("knex/lib/dialects/mysql2"));
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
const mysql_columncompiler_1 = __importDefault(require("knex/lib/dialects/mysql/schema/mysql-columncompiler"));
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
const mysql_querycompiler_1 = __importDefault(require("knex/lib/dialects/mysql/query/mysql-querycompiler"));
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
const mssql_columncompiler_1 = __importDefault(require("knex/lib/dialects/mssql/schema/mssql-columncompiler"));
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
const mssql_tablecompiler_1 = __importDefault(require("knex/lib/dialects/mssql/schema/mssql-tablecompiler"));
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
const mssql_querycompiler_1 = __importDefault(require("knex/lib/dialects/mssql/query/mssql-querycompiler"));
|
|
23
|
-
// @ts-ignore
|
|
24
|
-
const mssql_1 = __importDefault(require("knex/lib/dialects/mssql"));
|
|
25
|
-
// @ts-ignore
|
|
26
|
-
const postgres_1 = __importDefault(require("knex/lib/dialects/postgres"));
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
const pg_tablecompiler_1 = __importDefault(require("knex/lib/dialects/postgres/schema/pg-tablecompiler"));
|
|
29
|
-
// @ts-ignore
|
|
30
|
-
const pg_querycompiler_1 = __importDefault(require("knex/lib/dialects/postgres/query/pg-querycompiler"));
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
const sqlite3_1 = __importDefault(require("knex/lib/dialects/sqlite3"));
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
const better_sqlite3_1 = __importDefault(require("knex/lib/dialects/better-sqlite3"));
|
|
35
|
-
// @ts-ignore
|
|
36
|
-
const sqlite_tablecompiler_1 = __importDefault(require("knex/lib/dialects/sqlite3/schema/sqlite-tablecompiler"));
|
|
37
|
-
// @ts-ignore
|
|
38
|
-
const tablecompiler_1 = __importDefault(require("knex/lib/schema/tablecompiler"));
|
|
39
|
-
// These specific portions of knex are overridden by the different
|
|
40
|
-
// database packages. We need to be sure the knex files they get to
|
|
41
|
-
// monkey patch are the same version as our overall knex instance
|
|
42
|
-
// which is why we need to import them in this package.
|
|
43
|
-
exports.MonkeyPatchable = {
|
|
44
|
-
Client: client_1.default,
|
|
45
|
-
QueryExecutioner: query_executioner_1.default,
|
|
46
|
-
MySqlDialect: mysql2_1.default,
|
|
47
|
-
MySqlColumnCompiler: mysql_columncompiler_1.default,
|
|
48
|
-
MySqlQueryCompiler: mysql_querycompiler_1.default,
|
|
49
|
-
MsSqlColumnCompiler: mssql_columncompiler_1.default,
|
|
50
|
-
MsSqlTableCompiler: mssql_tablecompiler_1.default,
|
|
51
|
-
MsSqlQueryCompiler: mssql_querycompiler_1.default,
|
|
52
|
-
MsSqlDialect: mssql_1.default,
|
|
53
|
-
PostgresDialect: postgres_1.default,
|
|
54
|
-
PostgresDialectTableCompiler: pg_tablecompiler_1.default,
|
|
55
|
-
PostgresQueryCompiler: pg_querycompiler_1.default,
|
|
56
|
-
Sqlite3Dialect: sqlite3_1.default,
|
|
57
|
-
Sqlite3DialectTableCompiler: sqlite_tablecompiler_1.default,
|
|
58
|
-
BetterSqlite3Dialect: better_sqlite3_1.default,
|
|
59
|
-
TableCompiler: tablecompiler_1.default,
|
|
60
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MsSqlColumnCompiler = void 0;
|
|
4
|
-
const MonkeyPatchable_1 = require("../../MonkeyPatchable");
|
|
5
|
-
class MsSqlColumnCompiler extends MonkeyPatchable_1.MonkeyPatchable.MsSqlColumnCompiler {
|
|
6
|
-
enu(allowed) {
|
|
7
|
-
return `nvarchar(100) check (${this.formatter.wrap(this.args[0])} in ('${(allowed.join("', '"))}'))`;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.MsSqlColumnCompiler = MsSqlColumnCompiler;
|