UNPKG

typeorm

Version:

Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.

870 lines • 105 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [0, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { TransactionAlreadyStartedError } from "../../error/TransactionAlreadyStartedError"; import { TransactionNotStartedError } from "../../error/TransactionNotStartedError"; import { TableColumn } from "../../schema-builder/table/TableColumn"; import { Table } from "../../schema-builder/table/Table"; import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"; import { TableIndex } from "../../schema-builder/table/TableIndex"; import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"; import { QueryFailedError } from "../../error/QueryFailedError"; import { TableUnique } from "../../schema-builder/table/TableUnique"; import { Broadcaster } from "../../subscriber/Broadcaster"; import { BaseQueryRunner } from "../../query-runner/BaseQueryRunner"; import { OrmUtils } from "../../util/OrmUtils"; import { TableCheck } from "../../schema-builder/table/TableCheck"; import { PromiseUtils } from "../../index"; /** * Runs queries on a single oracle database connection. */ var OracleQueryRunner = /** @class */ (function (_super) { __extends(OracleQueryRunner, _super); // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- function OracleQueryRunner(driver, mode) { if (mode === void 0) { mode = "master"; } var _this = _super.call(this) || this; _this.driver = driver; _this.connection = driver.connection; _this.broadcaster = new Broadcaster(_this); _this.mode = mode; return _this; } // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- /** * Creates/uses database connection from the connection pool to perform further operations. * Returns obtained database connection. */ OracleQueryRunner.prototype.connect = function () { var _this = this; if (this.databaseConnection) return Promise.resolve(this.databaseConnection); if (this.databaseConnectionPromise) return this.databaseConnectionPromise; if (this.mode === "slave" && this.driver.isReplicated) { this.databaseConnectionPromise = this.driver.obtainSlaveConnection().then(function (connection) { _this.databaseConnection = connection; return _this.databaseConnection; }); } else { // master this.databaseConnectionPromise = this.driver.obtainMasterConnection().then(function (connection) { _this.databaseConnection = connection; return _this.databaseConnection; }); } return this.databaseConnectionPromise; }; /** * Releases used database connection. * You cannot use query runner methods once its released. */ OracleQueryRunner.prototype.release = function () { var _this = this; return new Promise(function (ok, fail) { _this.isReleased = true; if (_this.databaseConnection) { _this.databaseConnection.close(function (err) { if (err) return fail(err); ok(); }); } else { ok(); } }); }; /** * Starts transaction. */ OracleQueryRunner.prototype.startTransaction = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { if (this.isReleased) throw new QueryRunnerAlreadyReleasedError(); if (this.isTransactionActive) throw new TransactionAlreadyStartedError(); // await this.query("START TRANSACTION"); this.isTransactionActive = true; return [2 /*return*/]; }); }); }; /** * Commits transaction. * Error will be thrown if transaction was not started. */ OracleQueryRunner.prototype.commitTransaction = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isTransactionActive) throw new TransactionNotStartedError(); return [4 /*yield*/, this.query("COMMIT")]; case 1: _a.sent(); this.isTransactionActive = false; return [2 /*return*/]; } }); }); }; /** * Rollbacks transaction. * Error will be thrown if transaction was not started. */ OracleQueryRunner.prototype.rollbackTransaction = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isTransactionActive) throw new TransactionNotStartedError(); return [4 /*yield*/, this.query("ROLLBACK")]; case 1: _a.sent(); this.isTransactionActive = false; return [2 /*return*/]; } }); }); }; /** * Executes a given SQL query. */ OracleQueryRunner.prototype.query = function (query, parameters) { var _this = this; if (this.isReleased) throw new QueryRunnerAlreadyReleasedError(); return new Promise(function (ok, fail) { return __awaiter(_this, void 0, void 0, function () { var _this = this; var queryStartTime_1, handler, executionOptions, databaseConnection, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); this.driver.connection.logger.logQuery(query, parameters, this); queryStartTime_1 = +new Date(); handler = function (err, result) { // log slow queries if maxQueryExecution time is set var maxQueryExecutionTime = _this.driver.connection.options.maxQueryExecutionTime; var queryEndTime = +new Date(); var queryExecutionTime = queryEndTime - queryStartTime_1; if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime) _this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, _this); if (err) { _this.driver.connection.logger.logQueryError(err, query, parameters, _this); return fail(new QueryFailedError(query, parameters, err)); } ok(result.rows || result.outBinds); }; executionOptions = { autoCommit: this.isTransactionActive ? false : true }; return [4 /*yield*/, this.connect()]; case 1: databaseConnection = _a.sent(); databaseConnection.execute(query, parameters || {}, executionOptions, handler); return [3 /*break*/, 3]; case 2: err_1 = _a.sent(); fail(err_1); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }); }; /** * Returns raw data stream. */ OracleQueryRunner.prototype.stream = function (query, parameters, onEnd, onError) { throw new Error("Stream is not supported by Oracle driver."); }; /** * Returns all available database names including system databases. */ OracleQueryRunner.prototype.getDatabases = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, Promise.resolve([])]; }); }); }; /** * Returns all available schema names including system schemas. * If database parameter specified, returns schemas of that database. */ OracleQueryRunner.prototype.getSchemas = function (database) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, Promise.resolve([])]; }); }); }; /** * Checks if database with the given name exist. */ OracleQueryRunner.prototype.hasDatabase = function (database) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, Promise.resolve(false)]; }); }); }; /** * Checks if schema with the given name exist. */ OracleQueryRunner.prototype.hasSchema = function (schema) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, Promise.resolve(false)]; }); }); }; /** * Checks if table with the given name exist in the database. */ OracleQueryRunner.prototype.hasTable = function (tableOrName) { return __awaiter(this, void 0, void 0, function () { var tableName, sql, result; return __generator(this, function (_a) { switch (_a.label) { case 0: tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName; sql = "SELECT \"TABLE_NAME\" FROM \"USER_TABLES\" WHERE \"TABLE_NAME\" = '" + tableName + "'"; return [4 /*yield*/, this.query(sql)]; case 1: result = _a.sent(); return [2 /*return*/, result.length ? true : false]; } }); }); }; /** * Checks if column with the given name exist in the given table. */ OracleQueryRunner.prototype.hasColumn = function (tableOrName, columnName) { return __awaiter(this, void 0, void 0, function () { var tableName, sql, result; return __generator(this, function (_a) { switch (_a.label) { case 0: tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName; sql = "SELECT \"COLUMN_NAME\" FROM \"USER_TAB_COLS\" WHERE \"TABLE_NAME\" = '" + tableName + "' AND \"COLUMN_NAME\" = '" + columnName + "'"; return [4 /*yield*/, this.query(sql)]; case 1: result = _a.sent(); return [2 /*return*/, result.length ? true : false]; } }); }); }; /** * Creates a new database. */ OracleQueryRunner.prototype.createDatabase = function (database, ifNotExist) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.query("CREATE DATABASE IF NOT EXISTS \"" + database + "\"")]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Drops database. */ OracleQueryRunner.prototype.dropDatabase = function (database, ifExist) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, Promise.resolve()]; }); }); }; /** * Creates a new table schema. */ OracleQueryRunner.prototype.createSchema = function (schemas, ifNotExist) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { throw new Error("Schema create queries are not supported by Oracle driver."); }); }); }; /** * Drops table schema. */ OracleQueryRunner.prototype.dropSchema = function (schemaPath, ifExist) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { throw new Error("Schema drop queries are not supported by Oracle driver."); }); }); }; /** * Creates a new table. */ OracleQueryRunner.prototype.createTable = function (table, ifNotExist, createForeignKeys, createIndices) { if (ifNotExist === void 0) { ifNotExist = false; } if (createForeignKeys === void 0) { createForeignKeys = true; } if (createIndices === void 0) { createIndices = true; } return __awaiter(this, void 0, void 0, function () { var _this = this; var isTableExist, upQueries, downQueries; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!ifNotExist) return [3 /*break*/, 2]; return [4 /*yield*/, this.hasTable(table)]; case 1: isTableExist = _a.sent(); if (isTableExist) return [2 /*return*/, Promise.resolve()]; _a.label = 2; case 2: upQueries = []; downQueries = []; upQueries.push(this.createTableSql(table, createForeignKeys)); downQueries.push(this.dropTableSql(table)); // if createForeignKeys is true, we must drop created foreign keys in down query. // createTable does not need separate method to create foreign keys, because it create fk's in the same query with table creation. if (createForeignKeys) table.foreignKeys.forEach(function (foreignKey) { return downQueries.push(_this.dropForeignKeySql(table, foreignKey)); }); if (createIndices) { table.indices.forEach(function (index) { // new index may be passed without name. In this case we generate index name manually. if (!index.name) index.name = _this.connection.namingStrategy.indexName(table.name, index.columnNames, index.where); upQueries.push(_this.createIndexSql(table, index)); downQueries.push(_this.dropIndexSql(index)); }); } return [4 /*yield*/, this.executeQueries(upQueries, downQueries)]; case 3: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Drops the table. */ OracleQueryRunner.prototype.dropTable = function (tableOrName, ifExist, dropForeignKeys, dropIndices) { if (dropForeignKeys === void 0) { dropForeignKeys = true; } if (dropIndices === void 0) { dropIndices = true; } return __awaiter(this, void 0, void 0, function () { var _this = this; var isTableExist, createForeignKeys, table, _a, upQueries, downQueries; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!ifExist) return [3 /*break*/, 2]; return [4 /*yield*/, this.hasTable(tableOrName)]; case 1: isTableExist = _b.sent(); if (!isTableExist) return [2 /*return*/, Promise.resolve()]; _b.label = 2; case 2: createForeignKeys = dropForeignKeys; if (!(tableOrName instanceof Table)) return [3 /*break*/, 3]; _a = tableOrName; return [3 /*break*/, 5]; case 3: return [4 /*yield*/, this.getCachedTable(tableOrName)]; case 4: _a = _b.sent(); _b.label = 5; case 5: table = _a; upQueries = []; downQueries = []; if (dropIndices) { table.indices.forEach(function (index) { upQueries.push(_this.dropIndexSql(index)); downQueries.push(_this.createIndexSql(table, index)); }); } // if dropForeignKeys is true, we just drop the table, otherwise we also drop table foreign keys. // createTable does not need separate method to create foreign keys, because it create fk's in the same query with table creation. if (dropForeignKeys) table.foreignKeys.forEach(function (foreignKey) { return upQueries.push(_this.dropForeignKeySql(table, foreignKey)); }); upQueries.push(this.dropTableSql(table)); downQueries.push(this.createTableSql(table, createForeignKeys)); return [4 /*yield*/, this.executeQueries(upQueries, downQueries)]; case 6: _b.sent(); return [2 /*return*/]; } }); }); }; /** * Renames the given table. */ OracleQueryRunner.prototype.renameTable = function (oldTableOrName, newTableOrName) { return __awaiter(this, void 0, void 0, function () { var _this = this; var upQueries, downQueries, oldTable, _a, newTable, columnNames, oldPkName, newPkName; return __generator(this, function (_b) { switch (_b.label) { case 0: upQueries = []; downQueries = []; if (!(oldTableOrName instanceof Table)) return [3 /*break*/, 1]; _a = oldTableOrName; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.getCachedTable(oldTableOrName)]; case 2: _a = _b.sent(); _b.label = 3; case 3: oldTable = _a; newTable = oldTable.clone(); if (newTableOrName instanceof Table) { newTable = newTableOrName; } else { newTable.name = newTableOrName; } // rename table upQueries.push("ALTER TABLE \"" + oldTable.name + "\" RENAME TO \"" + newTable.name + "\""); downQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME TO \"" + oldTable.name + "\""); // rename primary key constraint if (newTable.primaryColumns.length > 0) { columnNames = newTable.primaryColumns.map(function (column) { return column.name; }); oldPkName = this.connection.namingStrategy.primaryKeyName(oldTable, columnNames); newPkName = this.connection.namingStrategy.primaryKeyName(newTable, columnNames); // build queries upQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME CONSTRAINT \"" + oldPkName + "\" TO \"" + newPkName + "\""); downQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME CONSTRAINT \"" + newPkName + "\" TO \"" + oldPkName + "\""); } // rename unique constraints newTable.uniques.forEach(function (unique) { // build new constraint name var newUniqueName = _this.connection.namingStrategy.uniqueConstraintName(newTable, unique.columnNames); // build queries upQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME CONSTRAINT \"" + unique.name + "\" TO \"" + newUniqueName + "\""); downQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME CONSTRAINT \"" + newUniqueName + "\" TO \"" + unique.name + "\""); // replace constraint name unique.name = newUniqueName; }); // rename index constraints newTable.indices.forEach(function (index) { // build new constraint name var newIndexName = _this.connection.namingStrategy.indexName(newTable, index.columnNames, index.where); // build queries upQueries.push("ALTER INDEX \"" + index.name + "\" RENAME TO \"" + newIndexName + "\""); downQueries.push("ALTER INDEX \"" + newIndexName + "\" RENAME TO \"" + index.name + "\""); // replace constraint name index.name = newIndexName; }); // rename foreign key constraints newTable.foreignKeys.forEach(function (foreignKey) { // build new constraint name var newForeignKeyName = _this.connection.namingStrategy.foreignKeyName(newTable, foreignKey.columnNames); // build queries upQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME CONSTRAINT \"" + foreignKey.name + "\" TO \"" + newForeignKeyName + "\""); downQueries.push("ALTER TABLE \"" + newTable.name + "\" RENAME CONSTRAINT \"" + newForeignKeyName + "\" TO \"" + foreignKey.name + "\""); // replace constraint name foreignKey.name = newForeignKeyName; }); return [4 /*yield*/, this.executeQueries(upQueries, downQueries)]; case 4: _b.sent(); // rename old table and replace it in cached tabled; oldTable.name = newTable.name; this.replaceCachedTable(oldTable, newTable); return [2 /*return*/]; } }); }); }; /** * Creates a new column from the column in the table. */ OracleQueryRunner.prototype.addColumn = function (tableOrName, column) { return __awaiter(this, void 0, void 0, function () { var table, _a, clonedTable, upQueries, downQueries, primaryColumns, pkName_1, columnNames_1, pkName, columnNames, columnIndex, uniqueConstraint; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(tableOrName instanceof Table)) return [3 /*break*/, 1]; _a = tableOrName; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.getCachedTable(tableOrName)]; case 2: _a = _b.sent(); _b.label = 3; case 3: table = _a; clonedTable = table.clone(); upQueries = []; downQueries = []; upQueries.push("ALTER TABLE \"" + table.name + "\" ADD " + this.buildCreateColumnSql(column)); downQueries.push("ALTER TABLE \"" + table.name + "\" DROP COLUMN \"" + column.name + "\""); // create or update primary key constraint if (column.isPrimary) { primaryColumns = clonedTable.primaryColumns; // if table already have primary key, me must drop it and recreate again if (primaryColumns.length > 0) { pkName_1 = this.connection.namingStrategy.primaryKeyName(clonedTable.name, primaryColumns.map(function (column) { return column.name; })); columnNames_1 = primaryColumns.map(function (column) { return "\"" + column.name + "\""; }).join(", "); upQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + pkName_1 + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + pkName_1 + "\" PRIMARY KEY (" + columnNames_1 + ")"); } primaryColumns.push(column); pkName = this.connection.namingStrategy.primaryKeyName(clonedTable.name, primaryColumns.map(function (column) { return column.name; })); columnNames = primaryColumns.map(function (column) { return "\"" + column.name + "\""; }).join(", "); upQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + pkName + "\" PRIMARY KEY (" + columnNames + ")"); downQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + pkName + "\""); } columnIndex = clonedTable.indices.find(function (index) { return index.columnNames.length === 1 && index.columnNames[0] === column.name; }); if (columnIndex) { clonedTable.indices.splice(clonedTable.indices.indexOf(columnIndex), 1); upQueries.push(this.createIndexSql(table, columnIndex)); downQueries.push(this.dropIndexSql(columnIndex)); } // create unique constraint if (column.isUnique) { uniqueConstraint = new TableUnique({ name: this.connection.namingStrategy.uniqueConstraintName(table.name, [column.name]), columnNames: [column.name] }); clonedTable.uniques.push(uniqueConstraint); upQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + uniqueConstraint.name + "\" UNIQUE (\"" + column.name + "\")"); downQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + uniqueConstraint.name + "\""); } return [4 /*yield*/, this.executeQueries(upQueries, downQueries)]; case 4: _b.sent(); clonedTable.addColumn(column); this.replaceCachedTable(table, clonedTable); return [2 /*return*/]; } }); }); }; /** * Creates a new columns from the column in the table. */ OracleQueryRunner.prototype.addColumns = function (tableOrName, columns) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, PromiseUtils.runInSequence(columns, function (column) { return _this.addColumn(tableOrName, column); })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Renames column in the given table. */ OracleQueryRunner.prototype.renameColumn = function (tableOrName, oldTableColumnOrName, newTableColumnOrName) { return __awaiter(this, void 0, void 0, function () { var table, _a, oldColumn, newColumn; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(tableOrName instanceof Table)) return [3 /*break*/, 1]; _a = tableOrName; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.getCachedTable(tableOrName)]; case 2: _a = _b.sent(); _b.label = 3; case 3: table = _a; oldColumn = oldTableColumnOrName instanceof TableColumn ? oldTableColumnOrName : table.columns.find(function (c) { return c.name === oldTableColumnOrName; }); if (!oldColumn) throw new Error("Column \"" + oldTableColumnOrName + "\" was not found in the \"" + table.name + "\" table."); newColumn = undefined; if (newTableColumnOrName instanceof TableColumn) { newColumn = newTableColumnOrName; } else { newColumn = oldColumn.clone(); newColumn.name = newTableColumnOrName; } return [4 /*yield*/, this.changeColumn(table, oldColumn, newColumn)]; case 4: _b.sent(); return [2 /*return*/]; } }); }); }; /** * Changes a column in the table. */ OracleQueryRunner.prototype.changeColumn = function (tableOrName, oldTableColumnOrName, newColumn) { return __awaiter(this, void 0, void 0, function () { var _this = this; var table, _a, clonedTable, upQueries, downQueries, oldColumn, primaryColumns, columnNames, oldPkName, newPkName, oldTableColumn, defaultUp, defaultDown, nullableUp, nullableDown, primaryColumns, pkName, columnNames, column, pkName, columnNames, primaryColumn, column, pkName, columnNames, uniqueConstraint, uniqueConstraint; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(tableOrName instanceof Table)) return [3 /*break*/, 1]; _a = tableOrName; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.getCachedTable(tableOrName)]; case 2: _a = _b.sent(); _b.label = 3; case 3: table = _a; clonedTable = table.clone(); upQueries = []; downQueries = []; oldColumn = oldTableColumnOrName instanceof TableColumn ? oldTableColumnOrName : table.columns.find(function (column) { return column.name === oldTableColumnOrName; }); if (!oldColumn) throw new Error("Column \"" + oldTableColumnOrName + "\" was not found in the \"" + table.name + "\" table."); if (!((newColumn.isGenerated !== oldColumn.isGenerated && newColumn.generationStrategy !== "uuid") || oldColumn.type !== newColumn.type || oldColumn.length !== newColumn.length)) return [3 /*break*/, 6]; // Oracle does not support changing of IDENTITY column, so we must drop column and recreate it again. // Also, we recreate column if column type changed return [4 /*yield*/, this.dropColumn(table, oldColumn)]; case 4: // Oracle does not support changing of IDENTITY column, so we must drop column and recreate it again. // Also, we recreate column if column type changed _b.sent(); return [4 /*yield*/, this.addColumn(table, newColumn)]; case 5: _b.sent(); // update cloned table clonedTable = table.clone(); return [3 /*break*/, 8]; case 6: if (newColumn.name !== oldColumn.name) { // rename column upQueries.push("ALTER TABLE \"" + table.name + "\" RENAME COLUMN \"" + oldColumn.name + "\" TO \"" + newColumn.name + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" RENAME COLUMN \"" + newColumn.name + "\" TO \"" + oldColumn.name + "\""); // rename column primary key constraint if (oldColumn.isPrimary === true) { primaryColumns = clonedTable.primaryColumns; columnNames = primaryColumns.map(function (column) { return column.name; }); oldPkName = this.connection.namingStrategy.primaryKeyName(clonedTable, columnNames); // replace old column name with new column name columnNames.splice(columnNames.indexOf(oldColumn.name), 1); columnNames.push(newColumn.name); newPkName = this.connection.namingStrategy.primaryKeyName(clonedTable, columnNames); upQueries.push("ALTER TABLE \"" + table.name + "\" RENAME CONSTRAINT \"" + oldPkName + "\" TO \"" + newPkName + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" RENAME CONSTRAINT \"" + newPkName + "\" TO \"" + oldPkName + "\""); } // rename unique constraints clonedTable.findColumnUniques(oldColumn).forEach(function (unique) { // build new constraint name unique.columnNames.splice(unique.columnNames.indexOf(oldColumn.name), 1); unique.columnNames.push(newColumn.name); var newUniqueName = _this.connection.namingStrategy.uniqueConstraintName(clonedTable, unique.columnNames); // build queries upQueries.push("ALTER TABLE \"" + table.name + "\" RENAME CONSTRAINT \"" + unique.name + "\" TO \"" + newUniqueName + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" RENAME CONSTRAINT \"" + newUniqueName + "\" TO \"" + unique.name + "\""); // replace constraint name unique.name = newUniqueName; }); // rename index constraints clonedTable.findColumnIndices(oldColumn).forEach(function (index) { // build new constraint name index.columnNames.splice(index.columnNames.indexOf(oldColumn.name), 1); index.columnNames.push(newColumn.name); var newIndexName = _this.connection.namingStrategy.indexName(clonedTable, index.columnNames, index.where); // build queries upQueries.push("ALTER INDEX \"" + index.name + "\" RENAME TO \"" + newIndexName + "\""); downQueries.push("ALTER INDEX \"" + newIndexName + "\" RENAME TO \"" + index.name + "\""); // replace constraint name index.name = newIndexName; }); // rename foreign key constraints clonedTable.findColumnForeignKeys(oldColumn).forEach(function (foreignKey) { // build new constraint name foreignKey.columnNames.splice(foreignKey.columnNames.indexOf(oldColumn.name), 1); foreignKey.columnNames.push(newColumn.name); var newForeignKeyName = _this.connection.namingStrategy.foreignKeyName(clonedTable, foreignKey.columnNames); // build queries upQueries.push("ALTER TABLE \"" + table.name + "\" RENAME CONSTRAINT \"" + foreignKey.name + "\" TO \"" + newForeignKeyName + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" RENAME CONSTRAINT \"" + newForeignKeyName + "\" TO \"" + foreignKey.name + "\""); // replace constraint name foreignKey.name = newForeignKeyName; }); oldTableColumn = clonedTable.columns.find(function (column) { return column.name === oldColumn.name; }); clonedTable.columns[clonedTable.columns.indexOf(oldTableColumn)].name = newColumn.name; oldColumn.name = newColumn.name; } if (this.isColumnChanged(oldColumn, newColumn, true)) { defaultUp = ""; defaultDown = ""; nullableUp = ""; nullableDown = ""; // changing column default if (newColumn.default !== null && newColumn.default !== undefined) { defaultUp = "DEFAULT " + newColumn.default; if (oldColumn.default !== null && oldColumn.default !== undefined) { defaultDown = "DEFAULT " + oldColumn.default; } else { defaultDown = "DEFAULT NULL"; } } else if (oldColumn.default !== null && oldColumn.default !== undefined) { defaultUp = "DEFAULT NULL"; defaultDown = "DEFAULT " + oldColumn.default; } // changing column isNullable property if (newColumn.isNullable !== oldColumn.isNullable) { if (newColumn.isNullable === true) { nullableUp = "NULL"; nullableDown = "NOT NULL"; } else { nullableUp = "NOT NULL"; nullableDown = "NULL"; } } upQueries.push("ALTER TABLE \"" + table.name + "\" MODIFY \"" + oldColumn.name + "\" " + this.connection.driver.createFullType(newColumn) + " " + defaultUp + " " + nullableUp); downQueries.push("ALTER TABLE \"" + table.name + "\" MODIFY \"" + oldColumn.name + "\" " + this.connection.driver.createFullType(oldColumn) + " " + defaultDown + " " + nullableDown); } if (newColumn.isPrimary !== oldColumn.isPrimary) { primaryColumns = clonedTable.primaryColumns; // if primary column state changed, we must always drop existed constraint. if (primaryColumns.length > 0) { pkName = this.connection.namingStrategy.primaryKeyName(clonedTable.name, primaryColumns.map(function (column) { return column.name; })); columnNames = primaryColumns.map(function (column) { return "\"" + column.name + "\""; }).join(", "); upQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + pkName + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + pkName + "\" PRIMARY KEY (" + columnNames + ")"); } if (newColumn.isPrimary === true) { primaryColumns.push(newColumn); column = clonedTable.columns.find(function (column) { return column.name === newColumn.name; }); column.isPrimary = true; pkName = this.connection.namingStrategy.primaryKeyName(clonedTable.name, primaryColumns.map(function (column) { return column.name; })); columnNames = primaryColumns.map(function (column) { return "\"" + column.name + "\""; }).join(", "); upQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + pkName + "\" PRIMARY KEY (" + columnNames + ")"); downQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + pkName + "\""); } else { primaryColumn = primaryColumns.find(function (c) { return c.name === newColumn.name; }); primaryColumns.splice(primaryColumns.indexOf(primaryColumn), 1); column = clonedTable.columns.find(function (column) { return column.name === newColumn.name; }); column.isPrimary = false; // if we have another primary keys, we must recreate constraint. if (primaryColumns.length > 0) { pkName = this.connection.namingStrategy.primaryKeyName(clonedTable.name, primaryColumns.map(function (column) { return column.name; })); columnNames = primaryColumns.map(function (column) { return "\"" + column.name + "\""; }).join(", "); upQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + pkName + "\" PRIMARY KEY (" + columnNames + ")"); downQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + pkName + "\""); } } } if (newColumn.isUnique !== oldColumn.isUnique) { if (newColumn.isUnique === true) { uniqueConstraint = new TableUnique({ name: this.connection.namingStrategy.uniqueConstraintName(table.name, [newColumn.name]), columnNames: [newColumn.name] }); clonedTable.uniques.push(uniqueConstraint); upQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + uniqueConstraint.name + "\" UNIQUE (\"" + newColumn.name + "\")"); downQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + uniqueConstraint.name + "\""); } else { uniqueConstraint = clonedTable.uniques.find(function (unique) { return unique.columnNames.length === 1 && !!unique.columnNames.find(function (columnName) { return columnName === newColumn.name; }); }); clonedTable.uniques.splice(clonedTable.uniques.indexOf(uniqueConstraint), 1); upQueries.push("ALTER TABLE \"" + table.name + "\" DROP CONSTRAINT \"" + uniqueConstraint.name + "\""); downQueries.push("ALTER TABLE \"" + table.name + "\" ADD CONSTRAINT \"" + uniqueConstraint.name + "\" UNIQUE (\"" + newColumn.name + "\")"); } } return [4 /*yield*/, this.executeQueries(upQueries, downQueries)]; case 7: _b.sent(); this.replaceCachedTable(table, clonedTable); _b.label = 8; case 8: return [2 /*return*/]; } }); }); }; /** * Changes a column in the table. */ OracleQueryRunner.prototype.changeColumns = function (tableOrName, changedColumns) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, PromiseUtils.runInSequence(changedColumns, function (changedColumn) { return _this.changeColumn(tableOrName, changedColumn.oldColumn, changedColumn.newColumn); })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Drops column in the table. */ OracleQueryRunner.prototype.dropColumn = function (tableOrName, columnOrName) { return __awaiter(this, void 0, void 0, function () { var table, _a, column, clonedTable, upQueries, downQueries, pkName, columnNames, tableColumn, pkName_2, columnNames_2, columnIndex, columnCheck, columnUnique; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(tableOrName instanceof Table))