typeorm
Version:
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.
915 lines • 99 kB
JavaScript
import { __awaiter, __generator, __values } from "tslib";
import { CockroachDriver } from "../driver/cockroachdb/CockroachDriver";
import { Table } from "./table/Table";
import { TableColumn } from "./table/TableColumn";
import { TableForeignKey } from "./table/TableForeignKey";
import { TableIndex } from "./table/TableIndex";
import { TableUtils } from "./util/TableUtils";
import { PostgresDriver } from "../driver/postgres/PostgresDriver";
import { MysqlDriver } from "../driver/mysql/MysqlDriver";
import { TableUnique } from "./table/TableUnique";
import { TableCheck } from "./table/TableCheck";
import { TableExclusion } from "./table/TableExclusion";
import { View } from "./view/View";
import { AuroraDataApiDriver } from "../driver/aurora-data-api/AuroraDataApiDriver";
/**
* Creates complete tables schemas in the database based on the entity metadatas.
*
* Steps how schema is being built:
* 1. load list of all tables with complete column and keys information from the db
* 2. drop all (old) foreign keys that exist in the table, but does not exist in the metadata
* 3. create new tables that does not exist in the db, but exist in the metadata
* 4. drop all columns exist (left old) in the db table, but does not exist in the metadata
* 5. add columns from metadata which does not exist in the table
* 6. update all exist columns which metadata has changed
* 7. update primary keys - update old and create new primary key from changed columns
* 8. create foreign keys which does not exist in the table yet
* 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
*/
var RdbmsSchemaBuilder = /** @class */ (function () {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
function RdbmsSchemaBuilder(connection) {
this.connection = connection;
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Creates complete schemas for the given entity metadatas.
*/
RdbmsSchemaBuilder.prototype.build = function () {
return __awaiter(this, void 0, void 0, function () {
var isUsingTransactions, tablePaths, error_1, rollbackError_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.queryRunner = this.connection.createQueryRunner();
// this.connection.driver.database || this.currentDatabase;
this.currentDatabase = this.connection.driver.database;
this.currentSchema = this.connection.driver.schema;
isUsingTransactions = (!(this.connection.driver instanceof CockroachDriver) &&
this.connection.options.migrationsTransactionMode !== "none");
if (!isUsingTransactions) return [3 /*break*/, 2];
return [4 /*yield*/, this.queryRunner.startTransaction()];
case 1:
_a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 12, 18, 20]);
if (!(this.viewEntityToSyncMetadatas.length > 0)) return [3 /*break*/, 4];
return [4 /*yield*/, this.createTypeormMetadataTable()];
case 3:
_a.sent();
_a.label = 4;
case 4:
tablePaths = this.entityToSyncMetadatas.map(function (metadata) { return _this.getTablePath(metadata); });
return [4 /*yield*/, this.queryRunner.getTables(tablePaths)];
case 5:
_a.sent();
return [4 /*yield*/, this.queryRunner.getViews([])];
case 6:
_a.sent();
return [4 /*yield*/, this.executeSchemaSyncOperationsInProperOrder()];
case 7:
_a.sent();
if (!this.connection.queryResultCache) return [3 /*break*/, 9];
return [4 /*yield*/, this.connection.queryResultCache.synchronize(this.queryRunner)];
case 8:
_a.sent();
_a.label = 9;
case 9:
if (!isUsingTransactions) return [3 /*break*/, 11];
return [4 /*yield*/, this.queryRunner.commitTransaction()];
case 10:
_a.sent();
_a.label = 11;
case 11: return [3 /*break*/, 20];
case 12:
error_1 = _a.sent();
_a.label = 13;
case 13:
_a.trys.push([13, 16, , 17]);
if (!isUsingTransactions) return [3 /*break*/, 15];
return [4 /*yield*/, this.queryRunner.rollbackTransaction()];
case 14:
_a.sent();
_a.label = 15;
case 15: return [3 /*break*/, 17];
case 16:
rollbackError_1 = _a.sent();
return [3 /*break*/, 17];
case 17: throw error_1;
case 18: return [4 /*yield*/, this.queryRunner.release()];
case 19:
_a.sent();
return [7 /*endfinally*/];
case 20: return [2 /*return*/];
}
});
});
};
/**
* Returns sql queries to be executed by schema builder.
*/
RdbmsSchemaBuilder.prototype.log = function () {
return __awaiter(this, void 0, void 0, function () {
var tablePaths;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.queryRunner = this.connection.createQueryRunner();
_a.label = 1;
case 1:
_a.trys.push([1, , 9, 11]);
if (!(this.viewEntityToSyncMetadatas.length > 0)) return [3 /*break*/, 3];
return [4 /*yield*/, this.createTypeormMetadataTable()];
case 2:
_a.sent();
_a.label = 3;
case 3:
tablePaths = this.entityToSyncMetadatas.map(function (metadata) { return _this.getTablePath(metadata); });
return [4 /*yield*/, this.queryRunner.getTables(tablePaths)];
case 4:
_a.sent();
return [4 /*yield*/, this.queryRunner.getViews([])];
case 5:
_a.sent();
this.queryRunner.enableSqlMemory();
return [4 /*yield*/, this.executeSchemaSyncOperationsInProperOrder()];
case 6:
_a.sent();
if (!this.connection.queryResultCache) return [3 /*break*/, 8];
return [4 /*yield*/, this.connection.queryResultCache.synchronize(this.queryRunner)];
case 7:
_a.sent();
_a.label = 8;
case 8: return [2 /*return*/, this.queryRunner.getMemorySql()];
case 9:
// its important to disable this mode despite the fact we are release query builder
// because there exist drivers which reuse same query runner. Also its important to disable
// sql memory after call of getMemorySql() method because last one flushes sql memory.
this.queryRunner.disableSqlMemory();
return [4 /*yield*/, this.queryRunner.release()];
case 10:
_a.sent();
return [7 /*endfinally*/];
case 11: return [2 /*return*/];
}
});
});
};
Object.defineProperty(RdbmsSchemaBuilder.prototype, "entityToSyncMetadatas", {
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Returns only entities that should be synced in the database.
*/
get: function () {
return this.connection.entityMetadatas.filter(function (metadata) { return metadata.synchronize && metadata.tableType !== "entity-child" && metadata.tableType !== "view"; });
},
enumerable: false,
configurable: true
});
Object.defineProperty(RdbmsSchemaBuilder.prototype, "viewEntityToSyncMetadatas", {
/**
* Returns only entities that should be synced in the database.
*/
get: function () {
return this.connection.entityMetadatas.filter(function (metadata) { return metadata.tableType === "view" && metadata.synchronize; });
},
enumerable: false,
configurable: true
});
/**
* Executes schema sync operations in a proper order.
* Order of operations matter here.
*/
RdbmsSchemaBuilder.prototype.executeSchemaSyncOperationsInProperOrder = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.dropOldViews()];
case 1:
_a.sent();
return [4 /*yield*/, this.dropOldForeignKeys()];
case 2:
_a.sent();
return [4 /*yield*/, this.dropOldIndices()];
case 3:
_a.sent();
return [4 /*yield*/, this.dropOldChecks()];
case 4:
_a.sent();
return [4 /*yield*/, this.dropOldExclusions()];
case 5:
_a.sent();
return [4 /*yield*/, this.dropCompositeUniqueConstraints()];
case 6:
_a.sent();
// await this.renameTables();
return [4 /*yield*/, this.renameColumns()];
case 7:
// await this.renameTables();
_a.sent();
return [4 /*yield*/, this.createNewTables()];
case 8:
_a.sent();
return [4 /*yield*/, this.dropRemovedColumns()];
case 9:
_a.sent();
return [4 /*yield*/, this.addNewColumns()];
case 10:
_a.sent();
return [4 /*yield*/, this.updatePrimaryKeys()];
case 11:
_a.sent();
return [4 /*yield*/, this.updateExistColumns()];
case 12:
_a.sent();
return [4 /*yield*/, this.createNewIndices()];
case 13:
_a.sent();
return [4 /*yield*/, this.createNewChecks()];
case 14:
_a.sent();
return [4 /*yield*/, this.createNewExclusions()];
case 15:
_a.sent();
return [4 /*yield*/, this.createCompositeUniqueConstraints()];
case 16:
_a.sent();
return [4 /*yield*/, this.createForeignKeys()];
case 17:
_a.sent();
return [4 /*yield*/, this.createViews()];
case 18:
_a.sent();
return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.getTablePath = function (target) {
var parsed = this.connection.driver.parseTableName(target);
return this.connection.driver.buildTableName(parsed.tableName, parsed.schema || this.currentSchema, parsed.database || this.currentDatabase);
};
/**
* Drops all (old) foreign keys that exist in the tables, but do not exist in the entity metadata.
*/
RdbmsSchemaBuilder.prototype.dropOldForeignKeys = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_1, this_1, _a, _b, metadata, e_1_1;
var e_1, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_1 = function (metadata) {
var table, tableForeignKeysToDrop;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_1.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
tableForeignKeysToDrop = table.foreignKeys.filter(function (tableForeignKey) {
var metadataFK = metadata.foreignKeys.find(function (metadataForeignKey) { return ((tableForeignKey.name === metadataForeignKey.name) &&
(_this.getTablePath(tableForeignKey) === _this.getTablePath(metadataForeignKey.referencedEntityMetadata))); });
return !metadataFK
|| (metadataFK.onDelete && metadataFK.onDelete !== tableForeignKey.onDelete)
|| (metadataFK.onUpdate && metadataFK.onUpdate !== tableForeignKey.onUpdate);
});
if (tableForeignKeysToDrop.length === 0)
return [2 /*return*/, "continue"];
this_1.connection.logger.logSchemaBuild("dropping old foreign keys of " + table.name + ": " + tableForeignKeysToDrop.map(function (dbForeignKey) { return dbForeignKey.name; }).join(", "));
// drop foreign keys from the database
return [4 /*yield*/, this_1.queryRunner.dropForeignKeys(table, tableForeignKeysToDrop)];
case 1:
// drop foreign keys from the database
_e.sent();
return [2 /*return*/];
}
});
};
this_1 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_1(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _d.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
/**
* Rename tables
*/
RdbmsSchemaBuilder.prototype.renameTables = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
/**
* Renames columns.
* Works if only one column per table was changed.
* Changes only column name. If something besides name was changed, these changes will be ignored.
*/
RdbmsSchemaBuilder.prototype.renameColumns = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_2, this_2, _a, _b, metadata, e_2_1;
var e_2, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_2 = function (metadata) {
var table, renamedMetadataColumns, renamedTableColumns, renamedColumn;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_2.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
if (metadata.columns.length !== table.columns.length)
return [2 /*return*/, "continue"];
renamedMetadataColumns = metadata.columns.filter(function (column) {
return !table.columns.find(function (tableColumn) {
return tableColumn.name === column.databaseName
&& tableColumn.type === _this.connection.driver.normalizeType(column)
&& tableColumn.isNullable === column.isNullable
&& tableColumn.isUnique === _this.connection.driver.normalizeIsUnique(column);
});
});
if (renamedMetadataColumns.length === 0 || renamedMetadataColumns.length > 1)
return [2 /*return*/, "continue"];
renamedTableColumns = table.columns.filter(function (tableColumn) {
return !metadata.columns.find(function (column) {
return column.databaseName === tableColumn.name
&& _this.connection.driver.normalizeType(column) === tableColumn.type
&& column.isNullable === tableColumn.isNullable
&& _this.connection.driver.normalizeIsUnique(column) === tableColumn.isUnique;
});
});
if (renamedTableColumns.length === 0 || renamedTableColumns.length > 1)
return [2 /*return*/, "continue"];
renamedColumn = renamedTableColumns[0].clone();
renamedColumn.name = renamedMetadataColumns[0].databaseName;
this_2.connection.logger.logSchemaBuild("renaming column \"" + renamedTableColumns[0].name + "\" in to \"" + renamedColumn.name + "\"");
return [4 /*yield*/, this_2.queryRunner.renameColumn(table, renamedTableColumns[0], renamedColumn)];
case 1:
_e.sent();
return [2 /*return*/];
}
});
};
this_2 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_2(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_2_1 = _d.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.dropOldIndices = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_3, this_3, _a, _b, metadata, e_3_1;
var e_3, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_3 = function (metadata) {
var table, dropQueries;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_3.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
dropQueries = table.indices
.filter(function (tableIndex) {
var indexMetadata = metadata.indices.find(function (index) { return index.name === tableIndex.name; });
if (indexMetadata) {
if (indexMetadata.synchronize === false)
return false;
if (indexMetadata.isUnique !== tableIndex.isUnique)
return true;
if (indexMetadata.isSpatial !== tableIndex.isSpatial)
return true;
if (_this.connection.driver.isFullTextColumnTypeSupported() && indexMetadata.isFulltext !== tableIndex.isFulltext)
return true;
if (indexMetadata.columns.length !== tableIndex.columnNames.length)
return true;
return !indexMetadata.columns.every(function (column) { return tableIndex.columnNames.indexOf(column.databaseName) !== -1; });
}
return true;
})
.map(function (tableIndex) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.connection.logger.logSchemaBuild("dropping an index: \"" + tableIndex.name + "\" from table " + table.name);
return [4 /*yield*/, this.queryRunner.dropIndex(table, tableIndex)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
return [4 /*yield*/, Promise.all(dropQueries)];
case 1:
_e.sent();
return [2 /*return*/];
}
});
};
this_3 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_3(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_3_1 = _d.sent();
e_3 = { error: e_3_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_3) throw e_3.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.dropOldChecks = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_4, this_4, _a, _b, metadata, e_4_1;
var e_4, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
// Mysql does not support check constraints
if (this.connection.driver instanceof MysqlDriver || this.connection.driver instanceof AuroraDataApiDriver)
return [2 /*return*/];
_loop_4 = function (metadata) {
var table, oldChecks;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_4.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
oldChecks = table.checks.filter(function (tableCheck) {
return !metadata.checks.find(function (checkMetadata) { return checkMetadata.name === tableCheck.name; });
});
if (oldChecks.length === 0)
return [2 /*return*/, "continue"];
this_4.connection.logger.logSchemaBuild("dropping old check constraint: " + oldChecks.map(function (check) { return "\"" + check.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
return [4 /*yield*/, this_4.queryRunner.dropCheckConstraints(table, oldChecks)];
case 1:
_e.sent();
return [2 /*return*/];
}
});
};
this_4 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_4(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_4_1 = _d.sent();
e_4 = { error: e_4_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_4) throw e_4.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.dropCompositeUniqueConstraints = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_5, this_5, _a, _b, metadata, e_5_1;
var e_5, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_5 = function (metadata) {
var table, compositeUniques;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_5.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
compositeUniques = table.uniques.filter(function (tableUnique) {
return tableUnique.columnNames.length > 1 && !metadata.uniques.find(function (uniqueMetadata) { return uniqueMetadata.name === tableUnique.name; });
});
if (compositeUniques.length === 0)
return [2 /*return*/, "continue"];
this_5.connection.logger.logSchemaBuild("dropping old unique constraint: " + compositeUniques.map(function (unique) { return "\"" + unique.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
return [4 /*yield*/, this_5.queryRunner.dropUniqueConstraints(table, compositeUniques)];
case 1:
_e.sent();
return [2 /*return*/];
}
});
};
this_5 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_5(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_5_1 = _d.sent();
e_5 = { error: e_5_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_5) throw e_5.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.dropOldExclusions = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_6, this_6, _a, _b, metadata, e_6_1;
var e_6, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
// Only PostgreSQL supports exclusion constraints
if (!(this.connection.driver instanceof PostgresDriver))
return [2 /*return*/];
_loop_6 = function (metadata) {
var table, oldExclusions;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_6.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
oldExclusions = table.exclusions.filter(function (tableExclusion) {
return !metadata.exclusions.find(function (exclusionMetadata) { return exclusionMetadata.name === tableExclusion.name; });
});
if (oldExclusions.length === 0)
return [2 /*return*/, "continue"];
this_6.connection.logger.logSchemaBuild("dropping old exclusion constraint: " + oldExclusions.map(function (exclusion) { return "\"" + exclusion.name + "\""; }).join(", ") + " from table \"" + table.name + "\"");
return [4 /*yield*/, this_6.queryRunner.dropExclusionConstraints(table, oldExclusions)];
case 1:
_e.sent();
return [2 /*return*/];
}
});
};
this_6 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_6(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_6_1 = _d.sent();
e_6 = { error: e_6_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_6) throw e_6.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
/**
* Creates tables that do not exist in the database yet.
* New tables are created without foreign and primary keys.
* Primary key only can be created in conclusion with auto generated column.
*/
RdbmsSchemaBuilder.prototype.createNewTables = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_7, this_7, _a, _b, metadata, e_7_1;
var e_7, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_7 = function (metadata) {
var existTable, table;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
existTable = this_7.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (existTable)
return [2 /*return*/, "continue"];
this_7.connection.logger.logSchemaBuild("creating a new table: " + this_7.getTablePath(metadata));
table = Table.create(metadata, this_7.connection.driver);
return [4 /*yield*/, this_7.queryRunner.createTable(table, false, false)];
case 1:
_e.sent();
this_7.queryRunner.loadedTables.push(table);
return [2 /*return*/];
}
});
};
this_7 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.entityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_7(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_7_1 = _d.sent();
e_7 = { error: e_7_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_7) throw e_7.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.createViews = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_8, this_8, _a, _b, metadata, e_8_1;
var e_8, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_8 = function (metadata) {
var existView, view;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
existView = this_8.queryRunner.loadedViews.find(function (view) {
var viewExpression = typeof view.expression === "string" ? view.expression.trim() : view.expression(_this.connection).getQuery();
var metadataExpression = typeof metadata.expression === "string" ? metadata.expression.trim() : metadata.expression(_this.connection).getQuery();
return _this.getTablePath(view) === _this.getTablePath(metadata) && viewExpression === metadataExpression;
});
if (existView)
return [2 /*return*/, "continue"];
this_8.connection.logger.logSchemaBuild("creating a new view: " + this_8.getTablePath(metadata));
view = View.create(metadata, this_8.connection.driver);
return [4 /*yield*/, this_8.queryRunner.createView(view)];
case 1:
_e.sent();
this_8.queryRunner.loadedViews.push(view);
return [2 /*return*/];
}
});
};
this_8 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.viewEntityToSyncMetadatas), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
metadata = _b.value;
return [5 /*yield**/, _loop_8(metadata)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_8_1 = _d.sent();
e_8 = { error: e_8_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_8) throw e_8.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
RdbmsSchemaBuilder.prototype.dropOldViews = function () {
return __awaiter(this, void 0, void 0, function () {
var droppedViews, _loop_9, this_9, _a, _b, view, e_9_1;
var e_9, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
droppedViews = new Set();
_loop_9 = function (view) {
var existViewMetadata;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
existViewMetadata = this_9.viewEntityToSyncMetadatas.find(function (metadata) {
var viewExpression = typeof view.expression === "string" ? view.expression.trim() : view.expression(_this.connection).getQuery();
var metadataExpression = typeof metadata.expression === "string" ? metadata.expression.trim() : metadata.expression(_this.connection).getQuery();
return _this.getTablePath(view) === _this.getTablePath(metadata) && viewExpression === metadataExpression;
});
if (existViewMetadata)
return [2 /*return*/, "continue"];
this_9.connection.logger.logSchemaBuild("dropping an old view: " + view.name);
// drop an old view
return [4 /*yield*/, this_9.queryRunner.dropView(view)];
case 1:
// drop an old view
_e.sent();
droppedViews.add(view);
return [2 /*return*/];
}
});
};
this_9 = this;
_d.label = 1;
case 1:
_d.trys.push([1, 6, 7, 8]);
_a = __values(this.queryRunner.loadedViews), _b = _a.next();
_d.label = 2;
case 2:
if (!!_b.done) return [3 /*break*/, 5];
view = _b.value;
return [5 /*yield**/, _loop_9(view)];
case 3:
_d.sent();
_d.label = 4;
case 4:
_b = _a.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_9_1 = _d.sent();
e_9 = { error: e_9_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_9) throw e_9.error; }
return [7 /*endfinally*/];
case 8:
this.queryRunner.loadedViews = this.queryRunner.loadedViews.filter(function (view) { return !droppedViews.has(view); });
return [2 /*return*/];
}
});
});
};
/**
* Drops all columns that exist in the table, but does not exist in the metadata (left old).
* We drop their keys too, since it should be safe.
*/
RdbmsSchemaBuilder.prototype.dropRemovedColumns = function () {
return __awaiter(this, void 0, void 0, function () {
var _loop_10, this_10, _a, _b, metadata, e_10_1;
var e_10, _c;
var _this = this;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_loop_10 = function (metadata) {
var table, droppedTableColumns;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
table = this_10.queryRunner.loadedTables.find(function (table) { return _this.getTablePath(table) === _this.getTablePath(metadata); });
if (!table)
return [2 /*return*/, "continue"];
droppedTableColumns = table.columns.filter(function (tableColumn) {
return !metadata.columns.find(function (columnMetadata) { return columnMetadata.databaseName === tableColumn.name; });
});
if (droppedTableColumns.length === 0)
retur