UNPKG

73.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var CockroachDriver_1 = require("../driver/cockroachdb/CockroachDriver");
5var Table_1 = require("./table/Table");
6var TableColumn_1 = require("./table/TableColumn");
7var TableForeignKey_1 = require("./table/TableForeignKey");
8var TableIndex_1 = require("./table/TableIndex");
9var PromiseUtils_1 = require("../util/PromiseUtils");
10var TableUtils_1 = require("./util/TableUtils");
11var PostgresDriver_1 = require("../driver/postgres/PostgresDriver");
12var MysqlDriver_1 = require("../driver/mysql/MysqlDriver");
13var TableUnique_1 = require("./table/TableUnique");
14var TableCheck_1 = require("./table/TableCheck");
15var TableExclusion_1 = require("./table/TableExclusion");
16var View_1 = require("./view/View");
17/**
18 * Creates complete tables schemas in the database based on the entity metadatas.
19 *
20 * Steps how schema is being built:
21 * 1. load list of all tables with complete column and keys information from the db
22 * 2. drop all (old) foreign keys that exist in the table, but does not exist in the metadata
23 * 3. create new tables that does not exist in the db, but exist in the metadata
24 * 4. drop all columns exist (left old) in the db table, but does not exist in the metadata
25 * 5. add columns from metadata which does not exist in the table
26 * 6. update all exist columns which metadata has changed
27 * 7. update primary keys - update old and create new primary key from changed columns
28 * 8. create foreign keys which does not exist in the table yet
29 * 9. create indices which are missing in db yet, and drops indices which exist in the db, but does not exist in the metadata anymore
30 */
31var RdbmsSchemaBuilder = /** @class */ (function () {
32 // -------------------------------------------------------------------------
33 // Constructor
34 // -------------------------------------------------------------------------
35 function RdbmsSchemaBuilder(connection) {
36 this.connection = connection;
37 }
38 // -------------------------------------------------------------------------
39 // Public Methods
40 // -------------------------------------------------------------------------
41 /**
42 * Creates complete schemas for the given entity metadatas.
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 /*break*/, 2];
52 return [4 /*yield*/, 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 /*break*/, 4];
60 return [4 /*yield*/, this.createTypeormMetadataTable()];
61 case 3:
62 _a.sent();
63 _a.label = 4;
64 case 4: return [4 /*yield*/, this.queryRunner.getTables(tablePaths)];
65 case 5:
66 _a.sent();
67 return [4 /*yield*/, this.queryRunner.getViews([])];
68 case 6:
69 _a.sent();
70 return [4 /*yield*/, this.executeSchemaSyncOperationsInProperOrder()];
71 case 7:
72 _a.sent();
73 if (!this.connection.queryResultCache) return [3 /*break*/, 9];
74 return [4 /*yield*/, 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 /*break*/, 11];
80 return [4 /*yield*/, this.queryRunner.commitTransaction()];
81 case 10:
82 _a.sent();
83 _a.label = 11;
84 case 11: return [3 /*break*/, 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 /*break*/, 15];
91 return [4 /*yield*/, this.queryRunner.rollbackTransaction()];
92 case 14:
93 _a.sent();
94 _a.label = 15;
95 case 15: return [3 /*break*/, 17];
96 case 16:
97 rollbackError_1 = _a.sent();
98 return [3 /*break*/, 17];
99 case 17: throw error_1;
100 case 18: return [4 /*yield*/, this.queryRunner.release()];
101 case 19:
102 _a.sent();
103 return [7 /*endfinally*/];
104 case 20: return [2 /*return*/];
105 }
106 });
107 });
108 };
109 /**
110 * Returns sql queries to be executed by schema builder.
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 /*yield*/, this.queryRunner.getTables(tablePaths)];
124 case 2:
125 _a.sent();
126 return [4 /*yield*/, this.queryRunner.getViews([])];
127 case 3:
128 _a.sent();
129 this.queryRunner.enableSqlMemory();
130 return [4 /*yield*/, this.executeSchemaSyncOperationsInProperOrder()];
131 case 4:
132 _a.sent();
133 if (!this.connection.queryResultCache) return [3 /*break*/, 6];
134 return [4 /*yield*/, this.connection.queryResultCache.synchronize(this.queryRunner)];
135 case 5:
136 _a.sent();
137 _a.label = 6;
138 case 6: return [2 /*return*/, this.queryRunner.getMemorySql()];
139 case 7:
140 // its important to disable this mode despite the fact we are release query builder
141 // because there exist drivers which reuse same query runner. Also its important to disable
142 // sql memory after call of getMemorySql() method because last one flushes sql memory.
143 this.queryRunner.disableSqlMemory();
144 return [4 /*yield*/, this.queryRunner.release()];
145 case 8:
146 _a.sent();
147 return [7 /*endfinally*/];
148 case 9: return [2 /*return*/];
149 }
150 });
151 });
152 };
153 Object.defineProperty(RdbmsSchemaBuilder.prototype, "entityToSyncMetadatas", {
154 // -------------------------------------------------------------------------
155 // Protected Methods
156 // -------------------------------------------------------------------------
157 /**
158 * Returns only entities that should be synced in the database.
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 * Returns only entities that should be synced in the database.
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 * Executes schema sync operations in a proper order.
178 * Order of operations matter here.
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 /*yield*/, this.dropOldViews()];
185 case 1:
186 _a.sent();
187 return [4 /*yield*/, this.dropOldForeignKeys()];
188 case 2:
189 _a.sent();
190 return [4 /*yield*/, this.dropOldIndices()];
191 case 3:
192 _a.sent();
193 return [4 /*yield*/, this.dropOldChecks()];
194 case 4:
195 _a.sent();
196 return [4 /*yield*/, this.dropOldExclusions()];
197 case 5:
198 _a.sent();
199 return [4 /*yield*/, this.dropCompositeUniqueConstraints()];
200 case 6:
201 _a.sent();
202 // await this.renameTables();
203 return [4 /*yield*/, this.renameColumns()];
204 case 7:
205 // await this.renameTables();
206 _a.sent();
207 return [4 /*yield*/, this.createNewTables()];
208 case 8:
209 _a.sent();
210 return [4 /*yield*/, this.dropRemovedColumns()];
211 case 9:
212 _a.sent();
213 return [4 /*yield*/, this.addNewColumns()];
214 case 10:
215 _a.sent();
216 return [4 /*yield*/, this.updatePrimaryKeys()];
217 case 11:
218 _a.sent();
219 return [4 /*yield*/, this.updateExistColumns()];
220 case 12:
221 _a.sent();
222 return [4 /*yield*/, this.createNewIndices()];
223 case 13:
224 _a.sent();
225 return [4 /*yield*/, this.createNewChecks()];
226 case 14:
227 _a.sent();
228 return [4 /*yield*/, this.createNewExclusions()];
229 case 15:
230 _a.sent();
231 return [4 /*yield*/, this.createCompositeUniqueConstraints()];
232 case 16:
233 _a.sent();
234 return [4 /*yield*/, this.createForeignKeys()];
235 case 17:
236 _a.sent();
237 return [4 /*yield*/, this.createViews()];
238 case 18:
239 _a.sent();
240 return [2 /*return*/];
241 }
242 });
243 });
244 };
245 /**
246 * Drops all (old) foreign keys that exist in the tables, but do not exist in the entity metadata.
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 /*yield*/, 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 /*return*/];
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 /*return*/];
269 this.connection.logger.logSchemaBuild("dropping old foreign keys of " + table.name + ": " + tableForeignKeysToDrop.map(function (dbForeignKey) { return dbForeignKey.name; }).join(", "));
270 // drop foreign keys from the database
271 return [4 /*yield*/, this.queryRunner.dropForeignKeys(table, tableForeignKeysToDrop)];
272 case 1:
273 // drop foreign keys from the database
274 _a.sent();
275 return [2 /*return*/];
276 }
277 });
278 }); })];
279 case 1:
280 _a.sent();
281 return [2 /*return*/];
282 }
283 });
284 });
285 };
286 /**
287 * Rename tables
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 /*yield*/, 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 /*return*/];
297 });
298 }); })];
299 case 1:
300 _a.sent();
301 return [2 /*return*/];
302 }
303 });
304 });
305 };
306 /**
307 * Renames columns.
308 * Works if only one column per table was changed.
309 * Changes only column name. If something besides name was changed, these changes will be ignored.
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 /*yield*/, 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 /*return*/];
325 if (metadata.columns.length !== table.columns.length)
326 return [2 /*return*/];
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 /*return*/];
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 /*return*/];
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 /*yield*/, this.queryRunner.renameColumn(table, renamedTableColumns[0], renamedColumn)];
351 case 1:
352 _a.sent();
353 return [2 /*return*/];
354 }
355 });
356 }); })];
357 case 1:
358 _a.sent();
359 return [2 /*return*/];
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 /*yield*/, 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 /*return*/];
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 /*yield*/, this.queryRunner.dropIndex(table, tableIndex)];
402 case 1:
403 _a.sent();
404 return [2 /*return*/];
405 }
406 });
407 }); });
408 return [4 /*yield*/, Promise.all(dropQueries)];
409 case 1:
410 _a.sent();
411 return [2 /*return*/];
412 }
413 });
414 }); })];
415 case 1:
416 _a.sent();
417 return [2 /*return*/];
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 // Mysql does not support check constraints
429 if (this.connection.driver instanceof MysqlDriver_1.MysqlDriver)
430 return [2 /*return*/];
431 return [4 /*yield*/, 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 /*return*/];
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 /*return*/];
444 this.connection.logger.logSchemaBuild("dropping old check constraint: " + oldChecks.map(function (check) { return "\"" + check.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
445 return [4 /*yield*/, this.queryRunner.dropCheckConstraints(table, oldChecks)];
446 case 1:
447 _a.sent();
448 return [2 /*return*/];
449 }
450 });
451 }); })];
452 case 1:
453 _a.sent();
454 return [2 /*return*/];
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 /*yield*/, 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 /*return*/];
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 /*return*/];
477 this.connection.logger.logSchemaBuild("dropping old unique constraint: " + compositeUniques.map(function (unique) { return "\"" + unique.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
478 return [4 /*yield*/, this.queryRunner.dropUniqueConstraints(table, compositeUniques)];
479 case 1:
480 _a.sent();
481 return [2 /*return*/];
482 }
483 });
484 }); })];
485 case 1:
486 _a.sent();
487 return [2 /*return*/];
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 // Only PostgreSQL supports exclusion constraints
499 if (!(this.connection.driver instanceof PostgresDriver_1.PostgresDriver))
500 return [2 /*return*/];
501 return [4 /*yield*/, 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 /*return*/];
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 /*return*/];
514 this.connection.logger.logSchemaBuild("dropping old exclusion constraint: " + oldExclusions.map(function (exclusion) { return "\"" + exclusion.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
515 return [4 /*yield*/, this.queryRunner.dropExclusionConstraints(table, oldExclusions)];
516 case 1:
517 _a.sent();
518 return [2 /*return*/];
519 }
520 });
521 }); })];
522 case 1:
523 _a.sent();
524 return [2 /*return*/];
525 }
526 });
527 });
528 };
529 /**
530 * Creates tables that do not exist in the database yet.
531 * New tables are created without foreign and primary keys.
532 * Primary key only can be created in conclusion with auto generated column.
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 /*yield*/, 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 /*return*/];
553 this.connection.logger.logSchemaBuild("creating a new table: " + metadata.tablePath);
554 table = Table_1.Table.create(metadata, this.connection.driver);
555 return [4 /*yield*/, this.queryRunner.createTable(table, false, false)];
556 case 1:
557 _a.sent();
558 this.queryRunner.loadedTables.push(table);
559 return [2 /*return*/];
560 }
561 });
562 }); })];
563 case 1:
564 _a.sent();
565 return [2 /*return*/];
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 /*yield*/, 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 /*return*/];
591 this.connection.logger.logSchemaBuild("creating a new view: " + metadata.tablePath);
592 view = View_1.View.create(metadata, this.connection.driver);
593 return [4 /*yield*/, this.queryRunner.createView(view)];
594 case 1:
595 _a.sent();
596 this.queryRunner.loadedViews.push(view);
597 return [2 /*return*/];
598 }
599 });
600 }); })];
601 case 1:
602 _a.sent();
603 return [2 /*return*/];
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 /*yield*/, 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 /*return*/];
629 this.connection.logger.logSchemaBuild("dropping an old view: " + view.name);
630 // drop an old view
631 return [4 /*yield*/, this.queryRunner.dropView(view)];
632 case 1:
633 // drop an old view
634 _a.sent();
635 this.queryRunner.loadedViews.splice(this.queryRunner.loadedViews.indexOf(view), 1);
636 return [2 /*return*/];
637 }
638 });
639 }); })];
640 case 1:
641 _a.sent();
642 return [2 /*return*/];
643 }
644 });
645 });
646 };
647 /**
648 * Drops all columns that exist in the table, but does not exist in the metadata (left old).
649 * We drop their keys too, since it should be safe.
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 /*yield*/, 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 /*return*/];
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 /*return*/];
669 this.connection.logger.logSchemaBuild("columns dropped in " + table.name + ": " + droppedTableColumns.map(function (column) { return column.name; }).join(", "));
670 // drop columns from the database
671 return [4 /*yield*/, this.queryRunner.dropColumns(table, droppedTableColumns)];
672 case 1:
673 // drop columns from the database
674 _a.sent();
675 return [2 /*return*/];
676 }
677 });
678 }); })];
679 case 1:
680 _a.sent();
681 return [2 /*return*/];
682 }
683 });
684 });
685 };
686 /**
687 * Adds columns from metadata which does not exist in the table.
688 * Columns are created without keys.
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 /*yield*/, 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 /*return*/];
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 /*return*/];
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 /*return*/];
712 this.connection.logger.logSchemaBuild("new columns added: " + newColumnMetadatas.map(function (column) { return column.databaseName; }).join(", "));
713 return [4 /*yield*/, this.queryRunner.addColumns(table, newTableColumns)];
714 case 1:
715 _a.sent();
716 return [2 /*return*/];
717 }
718 });
719 }); })];
720 case 1:
721 _a.sent();
722 return [2 /*return*/];
723 }
724 });
725 });
726 };
727 /**
728 * Updates composite primary keys.
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 /*yield*/, 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 /*return*/];
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 /*break*/, 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 /*yield*/, this.queryRunner.updatePrimaryKeys(table, changedPrimaryColumns)];
751 case 1:
752 _a.sent();
753 _a.label = 2;
754 case 2: return [2 /*return*/];
755 }
756 });
757 }); })];
758 case 1:
759 _a.sent();
760 return [2 /*return*/];
761 }
762 });
763 });
764 };
765 /**
766 * Update all exist columns which metadata has changed.
767 * Still don't create keys. Also we don't touch foreign keys of the changed columns.
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 /*yield*/, 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 /*return*/];
783 changedColumns = this.connection.driver.findChangedColumns(table.columns, metadata.columns);
784 if (changedColumns.length === 0)
785 return [2 /*return*/];
786 // drop all foreign keys that point to this column
787 return [4 /*yield*/, PromiseUtils_1.PromiseUtils.runInSequence(changedColumns, function (changedColumn) { return _this.dropColumnReferencedForeignKeys(metadata.tablePath, changedColumn.databaseName); })];
788 case 1:
789 // drop all foreign keys that point to this column
790 _a.sent();
791 // drop all composite indices related to this column
792 return [4 /*yield*/, PromiseUtils_1.PromiseUtils.runInSequence(changedColumns, function (changedColumn) { return _this.dropColumnCompositeIndices(metadata.tablePath, changedColumn.databaseName); })];
793 case 2:
794 // drop all composite indices related to this column
795 _a.sent();
796 if (!!(this.connection.driver instanceof MysqlDriver_1.MysqlDriver)) return [3 /*break*/, 4];
797 return [4 /*yield*/, 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 /*return*/];
813 this.connection.logger.logSchemaBuild("columns changed in \"" + table.name + "\". updating: " + changedColumns.map(function (column) { return column.databaseName; }).join(", "));
814 return [4 /*yield*/, this.queryRunner.changeColumns(table, newAndOldTableColumns)];
815 case 5:
816 _a.sent();
817 return [2 /*return*/];
818 }
819 });
820 }); })];
821 case 1:
822 _a.sent();
823 return [2 /*return*/];
824 }
825 });
826 });
827 };
828 /**
829 * Creates composite indices which are missing in db yet.
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 /*yield*/, 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 /*return*/];
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 /*return*/];
849 this.connection.logger.logSchemaBuild("adding new indices " + newIndices.map(function (index) { return "\"" + index.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
850 return [4 /*yield*/, this.queryRunner.createIndices(table, newIndices)];
851 case 1:
852 _a.sent();
853 return [2 /*return*/];
854 }
855 });
856 }); })];
857 case 1:
858 _a.sent();
859 return [2 /*return*/];
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 // Mysql does not support check constraints
871 if (this.connection.driver instanceof MysqlDriver_1.MysqlDriver)
872 return [2 /*return*/];
873 return [4 /*yield*/, 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 /*return*/];
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 /*return*/];
886 this.connection.logger.logSchemaBuild("adding new check constraints: " + newChecks.map(function (index) { return "\"" + index.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
887 return [4 /*yield*/, this.queryRunner.createCheckConstraints(table, newChecks)];
888 case 1:
889 _a.sent();
890 return [2 /*return*/];
891 }
892 });
893 }); })];
894 case 1:
895 _a.sent();
896 return [2 /*return*/];
897 }
898 });
899 });
900 };
901 /**
902 * Creates composite uniques which are missing in db yet.
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 /*yield*/, 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 /*return*/];
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 /*return*/];
922 this.connection.logger.logSchemaBuild("adding new unique constraints: " + compositeUniques.map(function (unique) { return "\"" + unique.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
923 return [4 /*yield*/, this.queryRunner.createUniqueConstraints(table, compositeUniques)];
924 case 1:
925 _a.sent();
926 return [2 /*return*/];
927 }
928 });
929 }); })];
930 case 1:
931 _a.sent();
932 return [2 /*return*/];
933 }
934 });
935 });
936 };
937 /**
938 * Creates exclusions which are missing in db yet.
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 // Only PostgreSQL supports exclusion constraints
947 if (!(this.connection.driver instanceof PostgresDriver_1.PostgresDriver))
948 return [2 /*return*/];
949 return [4 /*yield*/, 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 /*return*/];
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 /*return*/];
962 this.connection.logger.logSchemaBuild("adding new exclusion constraints: " + newExclusions.map(function (exclusion) { return "\"" + exclusion.name + "\""; }).join(", ") + " in table \"" + table.name + "\"");
963 return [4 /*yield*/, this.queryRunner.createExclusionConstraints(table, newExclusions)];
964 case 1:
965 _a.sent();
966 return [2 /*return*/];
967 }
968 });
969 }); })];
970 case 1:
971 _a.sent();
972 return [2 /*return*/];
973 }
974 });
975 });
976 };
977 /**
978 * Creates foreign keys which does not exist in the table yet.
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 /*yield*/, 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 /*return*/];
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 /*return*/];
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 /*yield*/, this.queryRunner.createForeignKeys(table, dbForeignKeys)];
1001 case 1:
1002 _a.sent();
1003 return [2 /*return*/];
1004 }
1005 });
1006 }); })];
1007 case 1:
1008 _a.sent();
1009 return [2 /*return*/];
1010 }
1011 });
1012 });
1013 };
1014 /**
1015 * Drops all foreign keys where given column of the given table is being used.
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 /*return*/];
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 /*break*/, 2];
1047 return [4 /*yield*/, 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 /*return*/];
1055 }
1056 });
1057 });
1058 };
1059 /**
1060 * Drops all composite indices, related to given column.
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 /*return*/];
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 /*return*/];
1074 this.connection.logger.logSchemaBuild("dropping related indices of \"" + tablePath + "\".\"" + columnName + "\": " + relatedIndices.map(function (index) { return index.name; }).join(", "));
1075 return [4 /*yield*/, this.queryRunner.dropIndices(table, relatedIndices)];
1076 case 1:
1077 _a.sent();
1078 return [2 /*return*/];
1079 }
1080 });
1081 });
1082 };
1083 /**
1084 * Drops all composite uniques, related to given column.
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 /*return*/];
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 /*return*/];
1098 this.connection.logger.logSchemaBuild("dropping related unique constraints of \"" + tablePath + "\".\"" + columnName + "\": " + relatedUniques.map(function (unique) { return unique.name; }).join(", "));
1099 return [4 /*yield*/, this.queryRunner.dropUniqueConstraints(table, relatedUniques)];
1100 case 1:
1101 _a.sent();
1102 return [2 /*return*/];
1103 }
1104 });
1105 });
1106 };
1107 /**
1108 * Creates new columns from the given column metadatas.
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 * Creates typeorm service table for storing user defined Views.
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 /*yield*/, 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 /*return*/];
1163 }
1164 });
1165 });
1166 };
1167 return RdbmsSchemaBuilder;
1168}());
1169exports.RdbmsSchemaBuilder = RdbmsSchemaBuilder;
1170
1171//# sourceMappingURL=RdbmsSchemaBuilder.js.map