1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var CockroachDriver_1 = require("../driver/cockroachdb/CockroachDriver");
|
5 | var Table_1 = require("./table/Table");
|
6 | var TableColumn_1 = require("./table/TableColumn");
|
7 | var TableForeignKey_1 = require("./table/TableForeignKey");
|
8 | var TableIndex_1 = require("./table/TableIndex");
|
9 | var PromiseUtils_1 = require("../util/PromiseUtils");
|
10 | var TableUtils_1 = require("./util/TableUtils");
|
11 | var PostgresDriver_1 = require("../driver/postgres/PostgresDriver");
|
12 | var MysqlDriver_1 = require("../driver/mysql/MysqlDriver");
|
13 | var TableUnique_1 = require("./table/TableUnique");
|
14 | var TableCheck_1 = require("./table/TableCheck");
|
15 | var TableExclusion_1 = require("./table/TableExclusion");
|
16 | var View_1 = require("./view/View");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var RdbmsSchemaBuilder = (function () {
|
32 |
|
33 |
|
34 |
|
35 | function RdbmsSchemaBuilder(connection) {
|
36 | this.connection = connection;
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 | |
42 |
|
43 |
|
44 | RdbmsSchemaBuilder.prototype.build = function () {
|
45 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
46 | var tablePaths, error_1, rollbackError_1;
|
47 | return tslib_1.__generator(this, function (_a) {
|
48 | switch (_a.label) {
|
49 | case 0:
|
50 | this.queryRunner = this.connection.createQueryRunner("master");
|
51 | if (!!(this.connection.driver instanceof CockroachDriver_1.CockroachDriver)) return [3 , 2];
|
52 | return [4 , this.queryRunner.startTransaction()];
|
53 | case 1:
|
54 | _a.sent();
|
55 | _a.label = 2;
|
56 | case 2:
|
57 | _a.trys.push([2, 12, 18, 20]);
|
58 | tablePaths = this.entityToSyncMetadatas.map(function (metadata) { return metadata.tablePath; });
|
59 | if (!(this.viewEntityToSyncMetadatas.length > 0)) return [3 , 4];
|
60 | return [4 , this.createTypeormMetadataTable()];
|
61 | case 3:
|
62 | _a.sent();
|
63 | _a.label = 4;
|
64 | case 4: return [4 , this.queryRunner.getTables(tablePaths)];
|
65 | case 5:
|
66 | _a.sent();
|
67 | return [4 , this.queryRunner.getViews([])];
|
68 | case 6:
|
69 | _a.sent();
|
70 | return [4 , this.executeSchemaSyncOperationsInProperOrder()];
|
71 | case 7:
|
72 | _a.sent();
|
73 | if (!this.connection.queryResultCache) return [3 , 9];
|
74 | return [4 , this.connection.queryResultCache.synchronize(this.queryRunner)];
|
75 | case 8:
|
76 | _a.sent();
|
77 | _a.label = 9;
|
78 | case 9:
|
79 | if (!!(this.connection.driver instanceof CockroachDriver_1.CockroachDriver)) return [3 , 11];
|
80 | return [4 , this.queryRunner.commitTransaction()];
|
81 | case 10:
|
82 | _a.sent();
|
83 | _a.label = 11;
|
84 | case 11: return [3 , 20];
|
85 | case 12:
|
86 | error_1 = _a.sent();
|
87 | _a.label = 13;
|
88 | case 13:
|
89 | _a.trys.push([13, 16, , 17]);
|
90 | if (!!(this.connection.driver instanceof CockroachDriver_1.CockroachDriver)) return [3 , 15];
|
91 | return [4 , this.queryRunner.rollbackTransaction()];
|
92 | case 14:
|
93 | _a.sent();
|
94 | _a.label = 15;
|
95 | case 15: return [3 , 17];
|
96 | case 16:
|
97 | rollbackError_1 = _a.sent();
|
98 | return [3 , 17];
|
99 | case 17: throw error_1;
|
100 | case 18: return [4 , this.queryRunner.release()];
|
101 | case 19:
|
102 | _a.sent();
|
103 | return [7 ];
|
104 | case 20: return [2 ];
|
105 | }
|
106 | });
|
107 | });
|
108 | };
|
109 | |
110 |
|
111 |
|
112 | RdbmsSchemaBuilder.prototype.log = function () {
|
113 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
114 | var tablePaths;
|
115 | return tslib_1.__generator(this, function (_a) {
|
116 | switch (_a.label) {
|
117 | case 0:
|
118 | this.queryRunner = this.connection.createQueryRunner("master");
|
119 | _a.label = 1;
|
120 | case 1:
|
121 | _a.trys.push([1, , 7, 9]);
|
122 | tablePaths = this.entityToSyncMetadatas.map(function (metadata) { return metadata.tablePath; });
|
123 | return [4 , this.queryRunner.getTables(tablePaths)];
|
124 | case 2:
|
125 | _a.sent();
|
126 | return [4 , this.queryRunner.getViews([])];
|
127 | case 3:
|
128 | _a.sent();
|
129 | this.queryRunner.enableSqlMemory();
|
130 | return [4 , this.executeSchemaSyncOperationsInProperOrder()];
|
131 | case 4:
|
132 | _a.sent();
|
133 | if (!this.connection.queryResultCache) return [3 , 6];
|
134 | return [4 , this.connection.queryResultCache.synchronize(this.queryRunner)];
|
135 | case 5:
|
136 | _a.sent();
|
137 | _a.label = 6;
|
138 | case 6: return [2 , this.queryRunner.getMemorySql()];
|
139 | case 7:
|
140 |
|
141 |
|
142 |
|
143 | this.queryRunner.disableSqlMemory();
|
144 | return [4 , this.queryRunner.release()];
|
145 | case 8:
|
146 | _a.sent();
|
147 | return [7 ];
|
148 | case 9: return [2 ];
|
149 | }
|
150 | });
|
151 | });
|
152 | };
|
153 | Object.defineProperty(RdbmsSchemaBuilder.prototype, "entityToSyncMetadatas", {
|
154 |
|
155 |
|
156 |
|
157 | |
158 |
|
159 |
|
160 | get: function () {
|
161 | return this.connection.entityMetadatas.filter(function (metadata) { return metadata.synchronize && metadata.tableType !== "entity-child" && metadata.tableType !== "view"; });
|
162 | },
|
163 | enumerable: true,
|
164 | configurable: true
|
165 | });
|
166 | Object.defineProperty(RdbmsSchemaBuilder.prototype, "viewEntityToSyncMetadatas", {
|
167 | |
168 |
|
169 |
|
170 | get: function () {
|
171 | return this.connection.entityMetadatas.filter(function (metadata) { return metadata.tableType === "view"; });
|
172 | },
|
173 | enumerable: true,
|
174 | configurable: true
|
175 | });
|
176 | |
177 |
|
178 |
|
179 |
|
180 | RdbmsSchemaBuilder.prototype.executeSchemaSyncOperationsInProperOrder = function () {
|
181 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
182 | return tslib_1.__generator(this, function (_a) {
|
183 | switch (_a.label) {
|
184 | case 0: return [4 , this.dropOldViews()];
|
185 | case 1:
|
186 | _a.sent();
|
187 | return [4 , this.dropOldForeignKeys()];
|
188 | case 2:
|
189 | _a.sent();
|
190 | return [4 , this.dropOldIndices()];
|
191 | case 3:
|
192 | _a.sent();
|
193 | return [4 , this.dropOldChecks()];
|
194 | case 4:
|
195 | _a.sent();
|
196 | return [4 , this.dropOldExclusions()];
|
197 | case 5:
|
198 | _a.sent();
|
199 | return [4 , this.dropCompositeUniqueConstraints()];
|
200 | case 6:
|
201 | _a.sent();
|
202 |
|
203 | return [4 , this.renameColumns()];
|
204 | case 7:
|
205 |
|
206 | _a.sent();
|
207 | return [4 , this.createNewTables()];
|
208 | case 8:
|
209 | _a.sent();
|
210 | return [4 , this.dropRemovedColumns()];
|
211 | case 9:
|
212 | _a.sent();
|
213 | return [4 , this.addNewColumns()];
|
214 | case 10:
|
215 | _a.sent();
|
216 | return [4 , this.updatePrimaryKeys()];
|
217 | case 11:
|
218 | _a.sent();
|
219 | return [4 , this.updateExistColumns()];
|
220 | case 12:
|
221 | _a.sent();
|
222 | return [4 , this.createNewIndices()];
|
223 | case 13:
|
224 | _a.sent();
|
225 | return [4 , this.createNewChecks()];
|
226 | case 14:
|
227 | _a.sent();
|
228 | return [4 , this.createNewExclusions()];
|
229 | case 15:
|
230 | _a.sent();
|
231 | return [4 , this.createCompositeUniqueConstraints()];
|
232 | case 16:
|
233 | _a.sent();
|
234 | return [4 , this.createForeignKeys()];
|
235 | case 17:
|
236 | _a.sent();
|
237 | return [4 , this.createViews()];
|
238 | case 18:
|
239 | _a.sent();
|
240 | return [2 ];
|
241 | }
|
242 | });
|
243 | });
|
244 | };
|
245 | |
246 |
|
247 |
|
248 | RdbmsSchemaBuilder.prototype.dropOldForeignKeys = function () {
|
249 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
250 | var _this = this;
|
251 | return tslib_1.__generator(this, function (_a) {
|
252 | switch (_a.label) {
|
253 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
254 | var table, tableForeignKeysToDrop;
|
255 | return tslib_1.__generator(this, function (_a) {
|
256 | switch (_a.label) {
|
257 | case 0:
|
258 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
259 | if (!table)
|
260 | return [2 ];
|
261 | tableForeignKeysToDrop = table.foreignKeys.filter(function (tableForeignKey) {
|
262 | var metadataFK = metadata.foreignKeys.find(function (metadataForeignKey) { return metadataForeignKey.name === tableForeignKey.name; });
|
263 | return !metadataFK
|
264 | || (metadataFK.onDelete && metadataFK.onDelete !== tableForeignKey.onDelete)
|
265 | || (metadataFK.onUpdate && metadataFK.onUpdate !== tableForeignKey.onUpdate);
|
266 | });
|
267 | if (tableForeignKeysToDrop.length === 0)
|
268 | return [2 ];
|
269 | this.connection.logger.logSchemaBuild("dropping old foreign keys of " + table.name + ": " + tableForeignKeysToDrop.map(function (dbForeignKey) { return dbForeignKey.name; }).join(", "));
|
270 |
|
271 | return [4 , this.queryRunner.dropForeignKeys(table, tableForeignKeysToDrop)];
|
272 | case 1:
|
273 |
|
274 | _a.sent();
|
275 | return [2 ];
|
276 | }
|
277 | });
|
278 | }); })];
|
279 | case 1:
|
280 | _a.sent();
|
281 | return [2 ];
|
282 | }
|
283 | });
|
284 | });
|
285 | };
|
286 | |
287 |
|
288 |
|
289 | RdbmsSchemaBuilder.prototype.renameTables = function () {
|
290 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
291 | var _this = this;
|
292 | return tslib_1.__generator(this, function (_a) {
|
293 | switch (_a.label) {
|
294 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
295 | return tslib_1.__generator(this, function (_a) {
|
296 | return [2 ];
|
297 | });
|
298 | }); })];
|
299 | case 1:
|
300 | _a.sent();
|
301 | return [2 ];
|
302 | }
|
303 | });
|
304 | });
|
305 | };
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 | RdbmsSchemaBuilder.prototype.renameColumns = function () {
|
312 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
313 | var _this = this;
|
314 | return tslib_1.__generator(this, function (_a) {
|
315 | switch (_a.label) {
|
316 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
317 | var table, renamedMetadataColumns, renamedTableColumns, renamedColumn;
|
318 | var _this = this;
|
319 | return tslib_1.__generator(this, function (_a) {
|
320 | switch (_a.label) {
|
321 | case 0:
|
322 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
323 | if (!table)
|
324 | return [2 ];
|
325 | if (metadata.columns.length !== table.columns.length)
|
326 | return [2 ];
|
327 | renamedMetadataColumns = metadata.columns.filter(function (column) {
|
328 | return !table.columns.find(function (tableColumn) {
|
329 | return tableColumn.name === column.databaseName
|
330 | && tableColumn.type === _this.connection.driver.normalizeType(column)
|
331 | && tableColumn.isNullable === column.isNullable
|
332 | && tableColumn.isUnique === _this.connection.driver.normalizeIsUnique(column);
|
333 | });
|
334 | });
|
335 | if (renamedMetadataColumns.length === 0 || renamedMetadataColumns.length > 1)
|
336 | return [2 ];
|
337 | renamedTableColumns = table.columns.filter(function (tableColumn) {
|
338 | return !metadata.columns.find(function (column) {
|
339 | return column.databaseName === tableColumn.name
|
340 | && _this.connection.driver.normalizeType(column) === tableColumn.type
|
341 | && column.isNullable === tableColumn.isNullable
|
342 | && _this.connection.driver.normalizeIsUnique(column) === tableColumn.isUnique;
|
343 | });
|
344 | });
|
345 | if (renamedTableColumns.length === 0 || renamedTableColumns.length > 1)
|
346 | return [2 ];
|
347 | renamedColumn = renamedTableColumns[0].clone();
|
348 | renamedColumn.name = renamedMetadataColumns[0].databaseName;
|
349 | this.connection.logger.logSchemaBuild("renaming column \"" + renamedTableColumns[0].name + "\" in to \"" + renamedColumn.name + "\"");
|
350 | return [4 , this.queryRunner.renameColumn(table, renamedTableColumns[0], renamedColumn)];
|
351 | case 1:
|
352 | _a.sent();
|
353 | return [2 ];
|
354 | }
|
355 | });
|
356 | }); })];
|
357 | case 1:
|
358 | _a.sent();
|
359 | return [2 ];
|
360 | }
|
361 | });
|
362 | });
|
363 | };
|
364 | RdbmsSchemaBuilder.prototype.dropOldIndices = function () {
|
365 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
366 | var _this = this;
|
367 | return tslib_1.__generator(this, function (_a) {
|
368 | switch (_a.label) {
|
369 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
370 | var table, dropQueries;
|
371 | var _this = this;
|
372 | return tslib_1.__generator(this, function (_a) {
|
373 | switch (_a.label) {
|
374 | case 0:
|
375 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
376 | if (!table)
|
377 | return [2 ];
|
378 | dropQueries = table.indices
|
379 | .filter(function (tableIndex) {
|
380 | var indexMetadata = metadata.indices.find(function (index) { return index.name === tableIndex.name; });
|
381 | if (indexMetadata) {
|
382 | if (indexMetadata.synchronize === false)
|
383 | return false;
|
384 | if (indexMetadata.isUnique !== tableIndex.isUnique)
|
385 | return true;
|
386 | if (indexMetadata.isSpatial !== tableIndex.isSpatial)
|
387 | return true;
|
388 | if (indexMetadata.isFulltext !== tableIndex.isFulltext)
|
389 | return true;
|
390 | if (indexMetadata.columns.length !== tableIndex.columnNames.length)
|
391 | return true;
|
392 | return !indexMetadata.columns.every(function (column) { return tableIndex.columnNames.indexOf(column.databaseName) !== -1; });
|
393 | }
|
394 | return true;
|
395 | })
|
396 | .map(function (tableIndex) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
397 | return tslib_1.__generator(this, function (_a) {
|
398 | switch (_a.label) {
|
399 | case 0:
|
400 | this.connection.logger.logSchemaBuild("dropping an index: \"" + tableIndex.name + "\" from table " + table.name);
|
401 | return [4 , this.queryRunner.dropIndex(table, tableIndex)];
|
402 | case 1:
|
403 | _a.sent();
|
404 | return [2 ];
|
405 | }
|
406 | });
|
407 | }); });
|
408 | return [4 , Promise.all(dropQueries)];
|
409 | case 1:
|
410 | _a.sent();
|
411 | return [2 ];
|
412 | }
|
413 | });
|
414 | }); })];
|
415 | case 1:
|
416 | _a.sent();
|
417 | return [2 ];
|
418 | }
|
419 | });
|
420 | });
|
421 | };
|
422 | RdbmsSchemaBuilder.prototype.dropOldChecks = function () {
|
423 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
424 | var _this = this;
|
425 | return tslib_1.__generator(this, function (_a) {
|
426 | switch (_a.label) {
|
427 | case 0:
|
428 |
|
429 | if (this.connection.driver instanceof MysqlDriver_1.MysqlDriver)
|
430 | return [2 ];
|
431 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
432 | var table, oldChecks;
|
433 | return tslib_1.__generator(this, function (_a) {
|
434 | switch (_a.label) {
|
435 | case 0:
|
436 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
437 | if (!table)
|
438 | return [2 ];
|
439 | oldChecks = table.checks.filter(function (tableCheck) {
|
440 | return !metadata.checks.find(function (checkMetadata) { return checkMetadata.name === tableCheck.name; });
|
441 | });
|
442 | if (oldChecks.length === 0)
|
443 | return [2 ];
|
444 | this.connection.logger.logSchemaBuild("dropping old check constraint: " + oldChecks.map(function (check) { return "\"" + check.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
|
445 | return [4 , this.queryRunner.dropCheckConstraints(table, oldChecks)];
|
446 | case 1:
|
447 | _a.sent();
|
448 | return [2 ];
|
449 | }
|
450 | });
|
451 | }); })];
|
452 | case 1:
|
453 | _a.sent();
|
454 | return [2 ];
|
455 | }
|
456 | });
|
457 | });
|
458 | };
|
459 | RdbmsSchemaBuilder.prototype.dropCompositeUniqueConstraints = function () {
|
460 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
461 | var _this = this;
|
462 | return tslib_1.__generator(this, function (_a) {
|
463 | switch (_a.label) {
|
464 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
465 | var table, compositeUniques;
|
466 | return tslib_1.__generator(this, function (_a) {
|
467 | switch (_a.label) {
|
468 | case 0:
|
469 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
470 | if (!table)
|
471 | return [2 ];
|
472 | compositeUniques = table.uniques.filter(function (tableUnique) {
|
473 | return tableUnique.columnNames.length > 1 && !metadata.uniques.find(function (uniqueMetadata) { return uniqueMetadata.name === tableUnique.name; });
|
474 | });
|
475 | if (compositeUniques.length === 0)
|
476 | return [2 ];
|
477 | this.connection.logger.logSchemaBuild("dropping old unique constraint: " + compositeUniques.map(function (unique) { return "\"" + unique.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
|
478 | return [4 , this.queryRunner.dropUniqueConstraints(table, compositeUniques)];
|
479 | case 1:
|
480 | _a.sent();
|
481 | return [2 ];
|
482 | }
|
483 | });
|
484 | }); })];
|
485 | case 1:
|
486 | _a.sent();
|
487 | return [2 ];
|
488 | }
|
489 | });
|
490 | });
|
491 | };
|
492 | RdbmsSchemaBuilder.prototype.dropOldExclusions = function () {
|
493 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
494 | var _this = this;
|
495 | return tslib_1.__generator(this, function (_a) {
|
496 | switch (_a.label) {
|
497 | case 0:
|
498 |
|
499 | if (!(this.connection.driver instanceof PostgresDriver_1.PostgresDriver))
|
500 | return [2 ];
|
501 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
502 | var table, oldExclusions;
|
503 | return tslib_1.__generator(this, function (_a) {
|
504 | switch (_a.label) {
|
505 | case 0:
|
506 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
507 | if (!table)
|
508 | return [2 ];
|
509 | oldExclusions = table.exclusions.filter(function (tableExclusion) {
|
510 | return !metadata.exclusions.find(function (exclusionMetadata) { return exclusionMetadata.name === tableExclusion.name; });
|
511 | });
|
512 | if (oldExclusions.length === 0)
|
513 | return [2 ];
|
514 | this.connection.logger.logSchemaBuild("dropping old exclusion constraint: " + oldExclusions.map(function (exclusion) { return "\"" + exclusion.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
|
515 | return [4 , this.queryRunner.dropExclusionConstraints(table, oldExclusions)];
|
516 | case 1:
|
517 | _a.sent();
|
518 | return [2 ];
|
519 | }
|
520 | });
|
521 | }); })];
|
522 | case 1:
|
523 | _a.sent();
|
524 | return [2 ];
|
525 | }
|
526 | });
|
527 | });
|
528 | };
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 | RdbmsSchemaBuilder.prototype.createNewTables = function () {
|
535 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
536 | var _this = this;
|
537 | return tslib_1.__generator(this, function (_a) {
|
538 | switch (_a.label) {
|
539 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
540 | var existTable, table;
|
541 | var _this = this;
|
542 | return tslib_1.__generator(this, function (_a) {
|
543 | switch (_a.label) {
|
544 | case 0:
|
545 | existTable = this.queryRunner.loadedTables.find(function (table) {
|
546 | var database = metadata.database && metadata.database !== _this.connection.driver.database ? metadata.database : undefined;
|
547 | var schema = metadata.schema || _this.connection.driver.options.schema;
|
548 | var fullTableName = _this.connection.driver.buildTableName(metadata.tableName, schema, database);
|
549 | return table.name === fullTableName;
|
550 | });
|
551 | if (existTable)
|
552 | return [2 ];
|
553 | this.connection.logger.logSchemaBuild("creating a new table: " + metadata.tablePath);
|
554 | table = Table_1.Table.create(metadata, this.connection.driver);
|
555 | return [4 , this.queryRunner.createTable(table, false, false)];
|
556 | case 1:
|
557 | _a.sent();
|
558 | this.queryRunner.loadedTables.push(table);
|
559 | return [2 ];
|
560 | }
|
561 | });
|
562 | }); })];
|
563 | case 1:
|
564 | _a.sent();
|
565 | return [2 ];
|
566 | }
|
567 | });
|
568 | });
|
569 | };
|
570 | RdbmsSchemaBuilder.prototype.createViews = function () {
|
571 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
572 | var _this = this;
|
573 | return tslib_1.__generator(this, function (_a) {
|
574 | switch (_a.label) {
|
575 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.viewEntityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
576 | var existView, view;
|
577 | var _this = this;
|
578 | return tslib_1.__generator(this, function (_a) {
|
579 | switch (_a.label) {
|
580 | case 0:
|
581 | existView = this.queryRunner.loadedViews.find(function (view) {
|
582 | var database = metadata.database && metadata.database !== _this.connection.driver.database ? metadata.database : undefined;
|
583 | var schema = metadata.schema || _this.connection.driver.options.schema;
|
584 | var fullViewName = _this.connection.driver.buildTableName(metadata.tableName, schema, database);
|
585 | var viewExpression = typeof view.expression === "string" ? view.expression.trim() : view.expression(_this.connection).getQuery();
|
586 | var metadataExpression = typeof metadata.expression === "string" ? metadata.expression.trim() : metadata.expression(_this.connection).getQuery();
|
587 | return view.name === fullViewName && viewExpression === metadataExpression;
|
588 | });
|
589 | if (existView)
|
590 | return [2 ];
|
591 | this.connection.logger.logSchemaBuild("creating a new view: " + metadata.tablePath);
|
592 | view = View_1.View.create(metadata, this.connection.driver);
|
593 | return [4 , this.queryRunner.createView(view)];
|
594 | case 1:
|
595 | _a.sent();
|
596 | this.queryRunner.loadedViews.push(view);
|
597 | return [2 ];
|
598 | }
|
599 | });
|
600 | }); })];
|
601 | case 1:
|
602 | _a.sent();
|
603 | return [2 ];
|
604 | }
|
605 | });
|
606 | });
|
607 | };
|
608 | RdbmsSchemaBuilder.prototype.dropOldViews = function () {
|
609 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
610 | var _this = this;
|
611 | return tslib_1.__generator(this, function (_a) {
|
612 | switch (_a.label) {
|
613 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.queryRunner.loadedViews, function (view) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
614 | var existViewMetadata;
|
615 | var _this = this;
|
616 | return tslib_1.__generator(this, function (_a) {
|
617 | switch (_a.label) {
|
618 | case 0:
|
619 | existViewMetadata = this.viewEntityToSyncMetadatas.find(function (metadata) {
|
620 | var database = metadata.database && metadata.database !== _this.connection.driver.database ? metadata.database : undefined;
|
621 | var schema = metadata.schema || _this.connection.driver.options.schema;
|
622 | var fullViewName = _this.connection.driver.buildTableName(metadata.tableName, schema, database);
|
623 | var viewExpression = typeof view.expression === "string" ? view.expression.trim() : view.expression(_this.connection).getQuery();
|
624 | var metadataExpression = typeof metadata.expression === "string" ? metadata.expression.trim() : metadata.expression(_this.connection).getQuery();
|
625 | return view.name === fullViewName && viewExpression === metadataExpression;
|
626 | });
|
627 | if (existViewMetadata)
|
628 | return [2 ];
|
629 | this.connection.logger.logSchemaBuild("dropping an old view: " + view.name);
|
630 |
|
631 | return [4 , this.queryRunner.dropView(view)];
|
632 | case 1:
|
633 |
|
634 | _a.sent();
|
635 | this.queryRunner.loadedViews.splice(this.queryRunner.loadedViews.indexOf(view), 1);
|
636 | return [2 ];
|
637 | }
|
638 | });
|
639 | }); })];
|
640 | case 1:
|
641 | _a.sent();
|
642 | return [2 ];
|
643 | }
|
644 | });
|
645 | });
|
646 | };
|
647 | |
648 |
|
649 |
|
650 |
|
651 | RdbmsSchemaBuilder.prototype.dropRemovedColumns = function () {
|
652 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
653 | var _this = this;
|
654 | return tslib_1.__generator(this, function (_a) {
|
655 | switch (_a.label) {
|
656 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
657 | var table, droppedTableColumns;
|
658 | return tslib_1.__generator(this, function (_a) {
|
659 | switch (_a.label) {
|
660 | case 0:
|
661 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
662 | if (!table)
|
663 | return [2 ];
|
664 | droppedTableColumns = table.columns.filter(function (tableColumn) {
|
665 | return !metadata.columns.find(function (columnMetadata) { return columnMetadata.databaseName === tableColumn.name; });
|
666 | });
|
667 | if (droppedTableColumns.length === 0)
|
668 | return [2 ];
|
669 | this.connection.logger.logSchemaBuild("columns dropped in " + table.name + ": " + droppedTableColumns.map(function (column) { return column.name; }).join(", "));
|
670 |
|
671 | return [4 , this.queryRunner.dropColumns(table, droppedTableColumns)];
|
672 | case 1:
|
673 |
|
674 | _a.sent();
|
675 | return [2 ];
|
676 | }
|
677 | });
|
678 | }); })];
|
679 | case 1:
|
680 | _a.sent();
|
681 | return [2 ];
|
682 | }
|
683 | });
|
684 | });
|
685 | };
|
686 | |
687 |
|
688 |
|
689 |
|
690 | RdbmsSchemaBuilder.prototype.addNewColumns = function () {
|
691 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
692 | var _this = this;
|
693 | return tslib_1.__generator(this, function (_a) {
|
694 | switch (_a.label) {
|
695 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
696 | var table, newColumnMetadatas, newTableColumnOptions, newTableColumns;
|
697 | return tslib_1.__generator(this, function (_a) {
|
698 | switch (_a.label) {
|
699 | case 0:
|
700 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
701 | if (!table)
|
702 | return [2 ];
|
703 | newColumnMetadatas = metadata.columns.filter(function (columnMetadata) {
|
704 | return !table.columns.find(function (tableColumn) { return tableColumn.name === columnMetadata.databaseName; });
|
705 | });
|
706 | if (newColumnMetadatas.length === 0)
|
707 | return [2 ];
|
708 | newTableColumnOptions = this.metadataColumnsToTableColumnOptions(newColumnMetadatas);
|
709 | newTableColumns = newTableColumnOptions.map(function (option) { return new TableColumn_1.TableColumn(option); });
|
710 | if (newTableColumns.length === 0)
|
711 | return [2 ];
|
712 | this.connection.logger.logSchemaBuild("new columns added: " + newColumnMetadatas.map(function (column) { return column.databaseName; }).join(", "));
|
713 | return [4 , this.queryRunner.addColumns(table, newTableColumns)];
|
714 | case 1:
|
715 | _a.sent();
|
716 | return [2 ];
|
717 | }
|
718 | });
|
719 | }); })];
|
720 | case 1:
|
721 | _a.sent();
|
722 | return [2 ];
|
723 | }
|
724 | });
|
725 | });
|
726 | };
|
727 | |
728 |
|
729 |
|
730 | RdbmsSchemaBuilder.prototype.updatePrimaryKeys = function () {
|
731 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
732 | var _this = this;
|
733 | return tslib_1.__generator(this, function (_a) {
|
734 | switch (_a.label) {
|
735 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
736 | var table, primaryMetadataColumns, primaryTableColumns, changedPrimaryColumns;
|
737 | var _this = this;
|
738 | return tslib_1.__generator(this, function (_a) {
|
739 | switch (_a.label) {
|
740 | case 0:
|
741 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
742 | if (!table)
|
743 | return [2 ];
|
744 | primaryMetadataColumns = metadata.columns.filter(function (column) { return column.isPrimary; });
|
745 | primaryTableColumns = table.columns.filter(function (column) { return column.isPrimary; });
|
746 | if (!(primaryTableColumns.length !== primaryMetadataColumns.length && primaryMetadataColumns.length > 1)) return [3 , 2];
|
747 | changedPrimaryColumns = primaryMetadataColumns.map(function (primaryMetadataColumn) {
|
748 | return new TableColumn_1.TableColumn(TableUtils_1.TableUtils.createTableColumnOptions(primaryMetadataColumn, _this.connection.driver));
|
749 | });
|
750 | return [4 , this.queryRunner.updatePrimaryKeys(table, changedPrimaryColumns)];
|
751 | case 1:
|
752 | _a.sent();
|
753 | _a.label = 2;
|
754 | case 2: return [2 ];
|
755 | }
|
756 | });
|
757 | }); })];
|
758 | case 1:
|
759 | _a.sent();
|
760 | return [2 ];
|
761 | }
|
762 | });
|
763 | });
|
764 | };
|
765 | |
766 |
|
767 |
|
768 |
|
769 | RdbmsSchemaBuilder.prototype.updateExistColumns = function () {
|
770 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
771 | var _this = this;
|
772 | return tslib_1.__generator(this, function (_a) {
|
773 | switch (_a.label) {
|
774 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
775 | var table, changedColumns, newAndOldTableColumns;
|
776 | var _this = this;
|
777 | return tslib_1.__generator(this, function (_a) {
|
778 | switch (_a.label) {
|
779 | case 0:
|
780 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
781 | if (!table)
|
782 | return [2 ];
|
783 | changedColumns = this.connection.driver.findChangedColumns(table.columns, metadata.columns);
|
784 | if (changedColumns.length === 0)
|
785 | return [2 ];
|
786 |
|
787 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(changedColumns, function (changedColumn) { return _this.dropColumnReferencedForeignKeys(metadata.tablePath, changedColumn.databaseName); })];
|
788 | case 1:
|
789 |
|
790 | _a.sent();
|
791 |
|
792 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(changedColumns, function (changedColumn) { return _this.dropColumnCompositeIndices(metadata.tablePath, changedColumn.databaseName); })];
|
793 | case 2:
|
794 |
|
795 | _a.sent();
|
796 | if (!!(this.connection.driver instanceof MysqlDriver_1.MysqlDriver)) return [3 , 4];
|
797 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(changedColumns, function (changedColumn) { return _this.dropColumnCompositeUniques(metadata.tablePath, changedColumn.databaseName); })];
|
798 | case 3:
|
799 | _a.sent();
|
800 | _a.label = 4;
|
801 | case 4:
|
802 | newAndOldTableColumns = changedColumns.map(function (changedColumn) {
|
803 | var oldTableColumn = table.columns.find(function (column) { return column.name === changedColumn.databaseName; });
|
804 | var newTableColumnOptions = TableUtils_1.TableUtils.createTableColumnOptions(changedColumn, _this.connection.driver);
|
805 | var newTableColumn = new TableColumn_1.TableColumn(newTableColumnOptions);
|
806 | return {
|
807 | oldColumn: oldTableColumn,
|
808 | newColumn: newTableColumn
|
809 | };
|
810 | });
|
811 | if (newAndOldTableColumns.length === 0)
|
812 | return [2 ];
|
813 | this.connection.logger.logSchemaBuild("columns changed in \"" + table.name + "\". updating: " + changedColumns.map(function (column) { return column.databaseName; }).join(", "));
|
814 | return [4 , this.queryRunner.changeColumns(table, newAndOldTableColumns)];
|
815 | case 5:
|
816 | _a.sent();
|
817 | return [2 ];
|
818 | }
|
819 | });
|
820 | }); })];
|
821 | case 1:
|
822 | _a.sent();
|
823 | return [2 ];
|
824 | }
|
825 | });
|
826 | });
|
827 | };
|
828 | |
829 |
|
830 |
|
831 | RdbmsSchemaBuilder.prototype.createNewIndices = function () {
|
832 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
833 | var _this = this;
|
834 | return tslib_1.__generator(this, function (_a) {
|
835 | switch (_a.label) {
|
836 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
837 | var table, newIndices;
|
838 | return tslib_1.__generator(this, function (_a) {
|
839 | switch (_a.label) {
|
840 | case 0:
|
841 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
842 | if (!table)
|
843 | return [2 ];
|
844 | newIndices = metadata.indices
|
845 | .filter(function (indexMetadata) { return !table.indices.find(function (tableIndex) { return tableIndex.name === indexMetadata.name; }) && indexMetadata.synchronize === true; })
|
846 | .map(function (indexMetadata) { return TableIndex_1.TableIndex.create(indexMetadata); });
|
847 | if (newIndices.length === 0)
|
848 | return [2 ];
|
849 | this.connection.logger.logSchemaBuild("adding new indices " + newIndices.map(function (index) { return "\"" + index.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
|
850 | return [4 , this.queryRunner.createIndices(table, newIndices)];
|
851 | case 1:
|
852 | _a.sent();
|
853 | return [2 ];
|
854 | }
|
855 | });
|
856 | }); })];
|
857 | case 1:
|
858 | _a.sent();
|
859 | return [2 ];
|
860 | }
|
861 | });
|
862 | });
|
863 | };
|
864 | RdbmsSchemaBuilder.prototype.createNewChecks = function () {
|
865 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
866 | var _this = this;
|
867 | return tslib_1.__generator(this, function (_a) {
|
868 | switch (_a.label) {
|
869 | case 0:
|
870 |
|
871 | if (this.connection.driver instanceof MysqlDriver_1.MysqlDriver)
|
872 | return [2 ];
|
873 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
874 | var table, newChecks;
|
875 | return tslib_1.__generator(this, function (_a) {
|
876 | switch (_a.label) {
|
877 | case 0:
|
878 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
879 | if (!table)
|
880 | return [2 ];
|
881 | newChecks = metadata.checks
|
882 | .filter(function (checkMetadata) { return !table.checks.find(function (tableCheck) { return tableCheck.name === checkMetadata.name; }); })
|
883 | .map(function (checkMetadata) { return TableCheck_1.TableCheck.create(checkMetadata); });
|
884 | if (newChecks.length === 0)
|
885 | return [2 ];
|
886 | this.connection.logger.logSchemaBuild("adding new check constraints: " + newChecks.map(function (index) { return "\"" + index.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
|
887 | return [4 , this.queryRunner.createCheckConstraints(table, newChecks)];
|
888 | case 1:
|
889 | _a.sent();
|
890 | return [2 ];
|
891 | }
|
892 | });
|
893 | }); })];
|
894 | case 1:
|
895 | _a.sent();
|
896 | return [2 ];
|
897 | }
|
898 | });
|
899 | });
|
900 | };
|
901 | |
902 |
|
903 |
|
904 | RdbmsSchemaBuilder.prototype.createCompositeUniqueConstraints = function () {
|
905 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
906 | var _this = this;
|
907 | return tslib_1.__generator(this, function (_a) {
|
908 | switch (_a.label) {
|
909 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
910 | var table, compositeUniques;
|
911 | return tslib_1.__generator(this, function (_a) {
|
912 | switch (_a.label) {
|
913 | case 0:
|
914 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
915 | if (!table)
|
916 | return [2 ];
|
917 | compositeUniques = metadata.uniques
|
918 | .filter(function (uniqueMetadata) { return uniqueMetadata.columns.length > 1 && !table.uniques.find(function (tableUnique) { return tableUnique.name === uniqueMetadata.name; }); })
|
919 | .map(function (uniqueMetadata) { return TableUnique_1.TableUnique.create(uniqueMetadata); });
|
920 | if (compositeUniques.length === 0)
|
921 | return [2 ];
|
922 | this.connection.logger.logSchemaBuild("adding new unique constraints: " + compositeUniques.map(function (unique) { return "\"" + unique.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
|
923 | return [4 , this.queryRunner.createUniqueConstraints(table, compositeUniques)];
|
924 | case 1:
|
925 | _a.sent();
|
926 | return [2 ];
|
927 | }
|
928 | });
|
929 | }); })];
|
930 | case 1:
|
931 | _a.sent();
|
932 | return [2 ];
|
933 | }
|
934 | });
|
935 | });
|
936 | };
|
937 | |
938 |
|
939 |
|
940 | RdbmsSchemaBuilder.prototype.createNewExclusions = function () {
|
941 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
942 | var _this = this;
|
943 | return tslib_1.__generator(this, function (_a) {
|
944 | switch (_a.label) {
|
945 | case 0:
|
946 |
|
947 | if (!(this.connection.driver instanceof PostgresDriver_1.PostgresDriver))
|
948 | return [2 ];
|
949 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
950 | var table, newExclusions;
|
951 | return tslib_1.__generator(this, function (_a) {
|
952 | switch (_a.label) {
|
953 | case 0:
|
954 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
955 | if (!table)
|
956 | return [2 ];
|
957 | newExclusions = metadata.exclusions
|
958 | .filter(function (exclusionMetadata) { return !table.exclusions.find(function (tableExclusion) { return tableExclusion.name === exclusionMetadata.name; }); })
|
959 | .map(function (exclusionMetadata) { return TableExclusion_1.TableExclusion.create(exclusionMetadata); });
|
960 | if (newExclusions.length === 0)
|
961 | return [2 ];
|
962 | this.connection.logger.logSchemaBuild("adding new exclusion constraints: " + newExclusions.map(function (exclusion) { return "\"" + exclusion.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
|
963 | return [4 , this.queryRunner.createExclusionConstraints(table, newExclusions)];
|
964 | case 1:
|
965 | _a.sent();
|
966 | return [2 ];
|
967 | }
|
968 | });
|
969 | }); })];
|
970 | case 1:
|
971 | _a.sent();
|
972 | return [2 ];
|
973 | }
|
974 | });
|
975 | });
|
976 | };
|
977 | |
978 |
|
979 |
|
980 | RdbmsSchemaBuilder.prototype.createForeignKeys = function () {
|
981 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
982 | var _this = this;
|
983 | return tslib_1.__generator(this, function (_a) {
|
984 | switch (_a.label) {
|
985 | case 0: return [4 , PromiseUtils_1.PromiseUtils.runInSequence(this.entityToSyncMetadatas, function (metadata) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
986 | var table, newKeys, dbForeignKeys;
|
987 | return tslib_1.__generator(this, function (_a) {
|
988 | switch (_a.label) {
|
989 | case 0:
|
990 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === metadata.tablePath; });
|
991 | if (!table)
|
992 | return [2 ];
|
993 | newKeys = metadata.foreignKeys.filter(function (foreignKey) {
|
994 | return !table.foreignKeys.find(function (dbForeignKey) { return dbForeignKey.name === foreignKey.name; });
|
995 | });
|
996 | if (newKeys.length === 0)
|
997 | return [2 ];
|
998 | dbForeignKeys = newKeys.map(function (foreignKeyMetadata) { return TableForeignKey_1.TableForeignKey.create(foreignKeyMetadata); });
|
999 | this.connection.logger.logSchemaBuild("creating a foreign keys: " + newKeys.map(function (key) { return key.name; }).join(", ") + " on table \"" + table.name + "\"");
|
1000 | return [4 , this.queryRunner.createForeignKeys(table, dbForeignKeys)];
|
1001 | case 1:
|
1002 | _a.sent();
|
1003 | return [2 ];
|
1004 | }
|
1005 | });
|
1006 | }); })];
|
1007 | case 1:
|
1008 | _a.sent();
|
1009 | return [2 ];
|
1010 | }
|
1011 | });
|
1012 | });
|
1013 | };
|
1014 | |
1015 |
|
1016 |
|
1017 | RdbmsSchemaBuilder.prototype.dropColumnReferencedForeignKeys = function (tablePath, columnName) {
|
1018 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
1019 | var table, tablesWithFK, columnForeignKey, clonedTable;
|
1020 | var _this = this;
|
1021 | return tslib_1.__generator(this, function (_a) {
|
1022 | switch (_a.label) {
|
1023 | case 0:
|
1024 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === tablePath; });
|
1025 | if (!table)
|
1026 | return [2 ];
|
1027 | tablesWithFK = [];
|
1028 | columnForeignKey = table.foreignKeys.find(function (foreignKey) { return foreignKey.columnNames.indexOf(columnName) !== -1; });
|
1029 | if (columnForeignKey) {
|
1030 | clonedTable = table.clone();
|
1031 | clonedTable.foreignKeys = [columnForeignKey];
|
1032 | tablesWithFK.push(clonedTable);
|
1033 | table.removeForeignKey(columnForeignKey);
|
1034 | }
|
1035 | this.queryRunner.loadedTables.forEach(function (loadedTable) {
|
1036 | var dependForeignKeys = loadedTable.foreignKeys.filter(function (foreignKey) {
|
1037 | return foreignKey.referencedTableName === tablePath && foreignKey.referencedColumnNames.indexOf(columnName) !== -1;
|
1038 | });
|
1039 | if (dependForeignKeys.length > 0) {
|
1040 | var clonedTable = loadedTable.clone();
|
1041 | clonedTable.foreignKeys = dependForeignKeys;
|
1042 | tablesWithFK.push(clonedTable);
|
1043 | dependForeignKeys.forEach(function (dependForeignKey) { return loadedTable.removeForeignKey(dependForeignKey); });
|
1044 | }
|
1045 | });
|
1046 | if (!(tablesWithFK.length > 0)) return [3 , 2];
|
1047 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(tablesWithFK, function (tableWithFK) {
|
1048 | _this.connection.logger.logSchemaBuild("dropping related foreign keys of " + tableWithFK.name + ": " + tableWithFK.foreignKeys.map(function (foreignKey) { return foreignKey.name; }).join(", "));
|
1049 | return _this.queryRunner.dropForeignKeys(tableWithFK, tableWithFK.foreignKeys);
|
1050 | })];
|
1051 | case 1:
|
1052 | _a.sent();
|
1053 | _a.label = 2;
|
1054 | case 2: return [2 ];
|
1055 | }
|
1056 | });
|
1057 | });
|
1058 | };
|
1059 | |
1060 |
|
1061 |
|
1062 | RdbmsSchemaBuilder.prototype.dropColumnCompositeIndices = function (tablePath, columnName) {
|
1063 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
1064 | var table, relatedIndices;
|
1065 | return tslib_1.__generator(this, function (_a) {
|
1066 | switch (_a.label) {
|
1067 | case 0:
|
1068 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === tablePath; });
|
1069 | if (!table)
|
1070 | return [2 ];
|
1071 | relatedIndices = table.indices.filter(function (index) { return index.columnNames.length > 1 && index.columnNames.indexOf(columnName) !== -1; });
|
1072 | if (relatedIndices.length === 0)
|
1073 | return [2 ];
|
1074 | this.connection.logger.logSchemaBuild("dropping related indices of \"" + tablePath + "\".\"" + columnName + "\": " + relatedIndices.map(function (index) { return index.name; }).join(", "));
|
1075 | return [4 , this.queryRunner.dropIndices(table, relatedIndices)];
|
1076 | case 1:
|
1077 | _a.sent();
|
1078 | return [2 ];
|
1079 | }
|
1080 | });
|
1081 | });
|
1082 | };
|
1083 | |
1084 |
|
1085 |
|
1086 | RdbmsSchemaBuilder.prototype.dropColumnCompositeUniques = function (tablePath, columnName) {
|
1087 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
1088 | var table, relatedUniques;
|
1089 | return tslib_1.__generator(this, function (_a) {
|
1090 | switch (_a.label) {
|
1091 | case 0:
|
1092 | table = this.queryRunner.loadedTables.find(function (table) { return table.name === tablePath; });
|
1093 | if (!table)
|
1094 | return [2 ];
|
1095 | relatedUniques = table.uniques.filter(function (unique) { return unique.columnNames.length > 1 && unique.columnNames.indexOf(columnName) !== -1; });
|
1096 | if (relatedUniques.length === 0)
|
1097 | return [2 ];
|
1098 | this.connection.logger.logSchemaBuild("dropping related unique constraints of \"" + tablePath + "\".\"" + columnName + "\": " + relatedUniques.map(function (unique) { return unique.name; }).join(", "));
|
1099 | return [4 , this.queryRunner.dropUniqueConstraints(table, relatedUniques)];
|
1100 | case 1:
|
1101 | _a.sent();
|
1102 | return [2 ];
|
1103 | }
|
1104 | });
|
1105 | });
|
1106 | };
|
1107 | |
1108 |
|
1109 |
|
1110 | RdbmsSchemaBuilder.prototype.metadataColumnsToTableColumnOptions = function (columns) {
|
1111 | var _this = this;
|
1112 | return columns.map(function (columnMetadata) { return TableUtils_1.TableUtils.createTableColumnOptions(columnMetadata, _this.connection.driver); });
|
1113 | };
|
1114 | |
1115 |
|
1116 |
|
1117 | RdbmsSchemaBuilder.prototype.createTypeormMetadataTable = function () {
|
1118 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
1119 | var options, typeormMetadataTable;
|
1120 | return tslib_1.__generator(this, function (_a) {
|
1121 | switch (_a.label) {
|
1122 | case 0:
|
1123 | options = this.connection.driver.options;
|
1124 | typeormMetadataTable = this.connection.driver.buildTableName("typeorm_metadata", options.schema, options.database);
|
1125 | return [4 , this.queryRunner.createTable(new Table_1.Table({
|
1126 | name: typeormMetadataTable,
|
1127 | columns: [
|
1128 | {
|
1129 | name: "type",
|
1130 | type: this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.metadataType }),
|
1131 | isNullable: false
|
1132 | },
|
1133 | {
|
1134 | name: "database",
|
1135 | type: this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.metadataDatabase }),
|
1136 | isNullable: true
|
1137 | },
|
1138 | {
|
1139 | name: "schema",
|
1140 | type: this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.metadataSchema }),
|
1141 | isNullable: true
|
1142 | },
|
1143 | {
|
1144 | name: "table",
|
1145 | type: this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.metadataTable }),
|
1146 | isNullable: true
|
1147 | },
|
1148 | {
|
1149 | name: "name",
|
1150 | type: this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.metadataName }),
|
1151 | isNullable: true
|
1152 | },
|
1153 | {
|
1154 | name: "value",
|
1155 | type: this.connection.driver.normalizeType({ type: this.connection.driver.mappedDataTypes.metadataValue }),
|
1156 | isNullable: true
|
1157 | },
|
1158 | ]
|
1159 | }), true)];
|
1160 | case 1:
|
1161 | _a.sent();
|
1162 | return [2 ];
|
1163 | }
|
1164 | });
|
1165 | });
|
1166 | };
|
1167 | return RdbmsSchemaBuilder;
|
1168 | }());
|
1169 | exports.RdbmsSchemaBuilder = RdbmsSchemaBuilder;
|
1170 |
|
1171 |
|