typeorm
Version:
Data-Mapper ORM for TypeScript and ES2021+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.
975 lines (974 loc) • 94.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OracleQueryRunner = void 0;
const error_1 = require("../../error");
const QueryFailedError_1 = require("../../error/QueryFailedError");
const QueryRunnerAlreadyReleasedError_1 = require("../../error/QueryRunnerAlreadyReleasedError");
const TransactionNotStartedError_1 = require("../../error/TransactionNotStartedError");
const BaseQueryRunner_1 = require("../../query-runner/BaseQueryRunner");
const QueryResult_1 = require("../../query-runner/QueryResult");
const Table_1 = require("../../schema-builder/table/Table");
const TableCheck_1 = require("../../schema-builder/table/TableCheck");
const TableColumn_1 = require("../../schema-builder/table/TableColumn");
const TableForeignKey_1 = require("../../schema-builder/table/TableForeignKey");
const TableIndex_1 = require("../../schema-builder/table/TableIndex");
const TableUnique_1 = require("../../schema-builder/table/TableUnique");
const View_1 = require("../../schema-builder/view/View");
const Broadcaster_1 = require("../../subscriber/Broadcaster");
const BroadcasterResult_1 = require("../../subscriber/BroadcasterResult");
const InstanceChecker_1 = require("../../util/InstanceChecker");
const OrmUtils_1 = require("../../util/OrmUtils");
const Query_1 = require("../Query");
const MetadataTableType_1 = require("../types/MetadataTableType");
/**
* Runs queries on a single oracle database connection.
*/
class OracleQueryRunner extends BaseQueryRunner_1.BaseQueryRunner {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(driver, mode) {
super();
this.driver = driver;
this.connection = driver.connection;
this.broadcaster = new Broadcaster_1.Broadcaster(this);
this.mode = mode;
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Creates/uses database connection from the connection pool to perform further operations.
* Returns obtained database connection.
*/
connect() {
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((connection) => {
this.databaseConnection = connection;
return this.databaseConnection;
});
}
else {
// master
this.databaseConnectionPromise = this.driver
.obtainMasterConnection()
.then((connection) => {
this.databaseConnection = connection;
return this.databaseConnection;
});
}
return this.databaseConnectionPromise;
}
/**
* Releases used database connection.
* You cannot use query runner methods once its released.
*/
async release() {
this.isReleased = true;
if (!this.databaseConnection) {
return;
}
await this.databaseConnection.close();
}
/**
* Starts transaction.
*/
async startTransaction(isolationLevel = "READ COMMITTED") {
if (this.isReleased)
throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
// await this.query("START TRANSACTION");
if (isolationLevel !== "SERIALIZABLE" &&
isolationLevel !== "READ COMMITTED") {
throw new error_1.TypeORMError(`Oracle only supports SERIALIZABLE and READ COMMITTED isolation`);
}
this.isTransactionActive = true;
try {
await this.broadcaster.broadcast("BeforeTransactionStart");
}
catch (err) {
this.isTransactionActive = false;
throw err;
}
if (this.transactionDepth === 0) {
await this.query("SET TRANSACTION ISOLATION LEVEL " + isolationLevel);
}
else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`);
}
this.transactionDepth += 1;
await this.broadcaster.broadcast("AfterTransactionStart");
}
/**
* Commits transaction.
* Error will be thrown if transaction was not started.
*/
async commitTransaction() {
if (!this.isTransactionActive)
throw new TransactionNotStartedError_1.TransactionNotStartedError();
await this.broadcaster.broadcast("BeforeTransactionCommit");
if (this.transactionDepth === 1) {
await this.query("COMMIT");
this.isTransactionActive = false;
}
this.transactionDepth -= 1;
await this.broadcaster.broadcast("AfterTransactionCommit");
}
/**
* Rollbacks transaction.
* Error will be thrown if transaction was not started.
*/
async rollbackTransaction() {
if (!this.isTransactionActive)
throw new TransactionNotStartedError_1.TransactionNotStartedError();
await this.broadcaster.broadcast("BeforeTransactionRollback");
if (this.transactionDepth > 1) {
await this.query(`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`);
}
else {
await this.query("ROLLBACK");
this.isTransactionActive = false;
}
this.transactionDepth -= 1;
await this.broadcaster.broadcast("AfterTransactionRollback");
}
/**
* Executes a given SQL query.
*/
async query(query, parameters, useStructuredResult = false) {
if (this.isReleased)
throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
const databaseConnection = await this.connect();
this.driver.connection.logger.logQuery(query, parameters, this);
await this.broadcaster.broadcast("BeforeQuery", query, parameters);
const broadcasterResult = new BroadcasterResult_1.BroadcasterResult();
const queryStartTime = Date.now();
try {
const executionOptions = {
autoCommit: !this.isTransactionActive,
outFormat: this.driver.oracle.OUT_FORMAT_OBJECT,
};
const raw = await databaseConnection.execute(query, parameters || {}, executionOptions);
// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.options.maxQueryExecutionTime;
const queryEndTime = Date.now();
const queryExecutionTime = queryEndTime - queryStartTime;
this.broadcaster.broadcastAfterQueryEvent(broadcasterResult, query, parameters, true, queryExecutionTime, raw, undefined);
if (maxQueryExecutionTime &&
queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);
const result = new QueryResult_1.QueryResult();
result.raw =
raw.rows ||
raw.outBinds ||
raw.rowsAffected ||
raw.implicitResults;
if (raw?.hasOwnProperty("rows") && Array.isArray(raw.rows)) {
result.records = raw.rows;
}
if (raw?.hasOwnProperty("outBinds") &&
Array.isArray(raw.outBinds)) {
result.records = raw.outBinds;
}
if (raw?.hasOwnProperty("implicitResults") &&
Array.isArray(raw.implicitResults)) {
result.records = raw.implicitResults;
}
if (raw?.hasOwnProperty("rowsAffected")) {
result.affected = raw.rowsAffected;
}
if (useStructuredResult) {
return result;
}
else {
return result.raw;
}
}
catch (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
this.broadcaster.broadcastAfterQueryEvent(broadcasterResult, query, parameters, false, undefined, undefined, err);
throw new QueryFailedError_1.QueryFailedError(query, parameters, err);
}
finally {
await broadcasterResult.wait();
}
}
/**
* Returns raw data stream.
*/
async stream(query, parameters, onEnd, onError) {
if (this.isReleased) {
throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
}
const executionOptions = {
autoCommit: !this.isTransactionActive,
outFormat: this.driver.oracle.OUT_FORMAT_OBJECT,
};
const databaseConnection = await this.connect();
this.driver.connection.logger.logQuery(query, parameters, this);
try {
const stream = databaseConnection.queryStream(query, parameters, executionOptions);
if (onEnd) {
stream.on("end", onEnd);
}
if (onError) {
stream.on("error", onError);
}
return stream;
}
catch (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
throw new QueryFailedError_1.QueryFailedError(query, parameters, err);
}
}
/**
* Returns all available database names including system databases.
*/
async getDatabases() {
return Promise.resolve([]);
}
/**
* Returns all available schema names including system schemas.
* If database parameter specified, returns schemas of that database.
*/
async getSchemas(database) {
return Promise.resolve([]);
}
/**
* Checks if database with the given name exist.
*/
async hasDatabase(database) {
try {
const query = await this.query(`SELECT 1 AS "exists" FROM global_name@"${database}"`);
return query.length > 0;
}
catch (e) {
return false;
}
}
/**
* Loads currently using database
*/
async getCurrentDatabase() {
const query = await this.query(`SELECT SYS_CONTEXT('USERENV','DB_NAME') AS "db_name" FROM dual`);
return query[0]["db_name"];
}
/**
* Checks if schema with the given name exist.
*/
async hasSchema(schema) {
return Promise.resolve(false);
}
/**
* Loads currently using database schema
*/
async getCurrentSchema() {
const query = await this.query(`SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') AS "schema_name" FROM dual`);
return query[0]["schema_name"];
}
/**
* Checks if table with the given name exist in the database.
*/
async hasTable(tableOrName) {
const { tableName } = this.driver.parseTableName(tableOrName);
const sql = `SELECT "TABLE_NAME" FROM "USER_TABLES" WHERE "TABLE_NAME" = '${tableName}'`;
const result = await this.query(sql);
return result.length ? true : false;
}
/**
* Checks if column with the given name exist in the given table.
*/
async hasColumn(tableOrName, columnName) {
const { tableName } = this.driver.parseTableName(tableOrName);
const sql = `SELECT "COLUMN_NAME" FROM "USER_TAB_COLS" WHERE "TABLE_NAME" = '${tableName}' AND "COLUMN_NAME" = '${columnName}'`;
const result = await this.query(sql);
return result.length ? true : false;
}
/**
* Creates a new database.
*/
async createDatabase(database, ifNotExist) {
// Even with `IF NOT EXISTS` we get:
// ORA-01501: CREATE DATABASE failed
// ORA-01100: database already mounted
if (ifNotExist) {
try {
await this.query(`CREATE DATABASE IF NOT EXISTS "${database}";`);
}
catch (e) {
// if (e instanceof QueryFailedError) {
if (e.message.includes("ORA-01100: database already mounted")) {
return;
}
// }
throw e;
}
}
else {
await this.query(`CREATE DATABASE "${database}"`);
}
}
/**
* Drops database.
*/
async dropDatabase(database, ifExist) {
return Promise.resolve();
}
/**
* Creates a new table schema.
*/
async createSchema(schemaPath, ifNotExist) {
throw new error_1.TypeORMError(`Schema create queries are not supported by Oracle driver.`);
}
/**
* Drops table schema.
*/
async dropSchema(schemaPath, ifExist) {
throw new error_1.TypeORMError(`Schema drop queries are not supported by Oracle driver.`);
}
/**
* Creates a new table.
*/
async createTable(table, ifNotExist = false, createForeignKeys = true, createIndices = true) {
if (ifNotExist) {
const isTableExist = await this.hasTable(table);
if (isTableExist)
return Promise.resolve();
}
const upQueries = [];
const 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((foreignKey) => downQueries.push(this.dropForeignKeySql(table, foreignKey)));
if (createIndices) {
table.indices.forEach((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, index.columnNames, index.where);
upQueries.push(this.createIndexSql(table, index));
downQueries.push(this.dropIndexSql(index));
});
}
// if table have column with generated type, we must add the expression to the metadata table
const generatedColumns = table.columns.filter((column) => column.generatedType && column.asExpression);
for (const column of generatedColumns) {
const insertQuery = this.insertTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
value: column.asExpression,
});
const deleteQuery = this.deleteTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
});
upQueries.push(insertQuery);
downQueries.push(deleteQuery);
}
await this.executeQueries(upQueries, downQueries);
}
/**
* Drops the table.
*/
async dropTable(tableOrName, ifExist, dropForeignKeys = true, dropIndices = true) {
// It needs because if table does not exist and dropForeignKeys or dropIndices is true, we don't need
// to perform drop queries for foreign keys and indices.
if (ifExist) {
const isTableExist = await this.hasTable(tableOrName);
if (!isTableExist)
return Promise.resolve();
}
// if dropTable called with dropForeignKeys = true, we must create foreign keys in down query.
const createForeignKeys = dropForeignKeys;
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const upQueries = [];
const downQueries = [];
if (dropIndices) {
table.indices.forEach((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((foreignKey) => upQueries.push(this.dropForeignKeySql(table, foreignKey)));
upQueries.push(this.dropTableSql(table));
downQueries.push(this.createTableSql(table, createForeignKeys));
// if table had columns with generated type, we must remove the expression from the metadata table
const generatedColumns = table.columns.filter((column) => column.generatedType && column.asExpression);
for (const column of generatedColumns) {
const deleteQuery = this.deleteTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
});
const insertQuery = this.insertTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
value: column.asExpression,
});
upQueries.push(deleteQuery);
downQueries.push(insertQuery);
}
await this.executeQueries(upQueries, downQueries);
}
/**
* Creates a new view.
*/
async createView(view, syncWithMetadata = false) {
const upQueries = [];
const downQueries = [];
upQueries.push(this.createViewSql(view));
if (syncWithMetadata)
upQueries.push(this.insertViewDefinitionSql(view));
downQueries.push(this.dropViewSql(view));
if (syncWithMetadata)
downQueries.push(this.deleteViewDefinitionSql(view));
await this.executeQueries(upQueries, downQueries);
}
/**
* Drops the view.
*/
async dropView(target) {
const viewName = InstanceChecker_1.InstanceChecker.isView(target) ? target.name : target;
const view = await this.getCachedView(viewName);
const upQueries = [];
const downQueries = [];
upQueries.push(this.deleteViewDefinitionSql(view));
upQueries.push(this.dropViewSql(view));
downQueries.push(this.insertViewDefinitionSql(view));
downQueries.push(this.createViewSql(view));
await this.executeQueries(upQueries, downQueries);
}
/**
* Renames the given table.
*/
async renameTable(oldTableOrName, newTableName) {
const upQueries = [];
const downQueries = [];
const oldTable = InstanceChecker_1.InstanceChecker.isTable(oldTableOrName)
? oldTableOrName
: await this.getCachedTable(oldTableOrName);
const newTable = oldTable.clone();
const { database: dbName, tableName: oldTableName } = this.driver.parseTableName(oldTable);
newTable.name = dbName ? `${dbName}.${newTableName}` : newTableName;
// rename table
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(oldTable)} RENAME TO "${newTableName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME TO "${oldTableName}"`));
// rename primary key constraint
if (newTable.primaryColumns.length > 0 &&
!newTable.primaryColumns[0].primaryKeyConstraintName) {
const columnNames = newTable.primaryColumns.map((column) => column.name);
const oldPkName = this.connection.namingStrategy.primaryKeyName(oldTable, columnNames);
const newPkName = this.connection.namingStrategy.primaryKeyName(newTable, columnNames);
// build queries
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME CONSTRAINT "${oldPkName}" TO "${newPkName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME CONSTRAINT "${newPkName}" TO "${oldPkName}"`));
}
// rename unique constraints
newTable.uniques.forEach((unique) => {
const oldUniqueName = this.connection.namingStrategy.uniqueConstraintName(oldTable, unique.columnNames);
// Skip renaming if Unique has user defined constraint name
if (unique.name !== oldUniqueName)
return;
// build new constraint name
const newUniqueName = this.connection.namingStrategy.uniqueConstraintName(newTable, unique.columnNames);
// build queries
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME CONSTRAINT "${unique.name}" TO "${newUniqueName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME CONSTRAINT "${newUniqueName}" TO "${unique.name}"`));
// replace constraint name
unique.name = newUniqueName;
});
// rename index constraints
newTable.indices.forEach((index) => {
const oldIndexName = this.connection.namingStrategy.indexName(oldTable, index.columnNames, index.where);
// Skip renaming if Index has user defined constraint name
if (index.name !== oldIndexName)
return;
// build new constraint name
const newIndexName = this.connection.namingStrategy.indexName(newTable, index.columnNames, index.where);
// build queries
upQueries.push(new Query_1.Query(`ALTER INDEX "${index.name}" RENAME TO "${newIndexName}"`));
downQueries.push(new Query_1.Query(`ALTER INDEX "${newIndexName}" RENAME TO "${index.name}"`));
// replace constraint name
index.name = newIndexName;
});
// rename foreign key constraints
newTable.foreignKeys.forEach((foreignKey) => {
const oldForeignKeyName = this.connection.namingStrategy.foreignKeyName(oldTable, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
// Skip renaming if foreign key has user defined constraint name
if (foreignKey.name !== oldForeignKeyName)
return;
// build new constraint name
const newForeignKeyName = this.connection.namingStrategy.foreignKeyName(newTable, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
// build queries
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME CONSTRAINT "${foreignKey.name}" TO "${newForeignKeyName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} RENAME CONSTRAINT "${newForeignKeyName}" TO "${foreignKey.name}"`));
// replace constraint name
foreignKey.name = newForeignKeyName;
});
await this.executeQueries(upQueries, downQueries);
// rename old table and replace it in cached tabled;
oldTable.name = newTable.name;
this.replaceCachedTable(oldTable, newTable);
}
/**
* Creates a new column from the column in the table.
*/
async addColumn(tableOrName, column) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const clonedTable = table.clone();
const upQueries = [];
const downQueries = [];
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD ${this.buildCreateColumnSql(column)}`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP COLUMN "${column.name}"`));
// create or update primary key constraint
if (column.isPrimary) {
const primaryColumns = clonedTable.primaryColumns;
// if table already have primary key, me must drop it and recreate again
if (primaryColumns.length > 0) {
const pkName = primaryColumns[0].primaryKeyConstraintName
? primaryColumns[0].primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map((column) => column.name));
const columnNames = primaryColumns
.map((column) => `"${column.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${pkName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
}
primaryColumns.push(column);
const pkName = primaryColumns[0].primaryKeyConstraintName
? primaryColumns[0].primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map((column) => column.name));
const columnNames = primaryColumns
.map((column) => `"${column.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${pkName}"`));
}
// create column index
const columnIndex = clonedTable.indices.find((index) => 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) {
const uniqueConstraint = new TableUnique_1.TableUnique({
name: this.connection.namingStrategy.uniqueConstraintName(table, [column.name]),
columnNames: [column.name],
});
clonedTable.uniques.push(uniqueConstraint);
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${uniqueConstraint.name}" UNIQUE ("${column.name}")`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${uniqueConstraint.name}"`));
}
if (column.generatedType && column.asExpression) {
const insertQuery = this.insertTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
value: column.asExpression,
});
const deleteQuery = this.deleteTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
});
upQueries.push(insertQuery);
downQueries.push(deleteQuery);
}
await this.executeQueries(upQueries, downQueries);
clonedTable.addColumn(column);
this.replaceCachedTable(table, clonedTable);
}
/**
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName, columns) {
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}
/**
* Renames column in the given table.
*/
async renameColumn(tableOrName, oldTableColumnOrName, newTableColumnOrName) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const oldColumn = InstanceChecker_1.InstanceChecker.isTableColumn(oldTableColumnOrName)
? oldTableColumnOrName
: table.columns.find((c) => c.name === oldTableColumnOrName);
if (!oldColumn)
throw new error_1.TypeORMError(`Column "${oldTableColumnOrName}" was not found in the ${this.escapePath(table)} table.`);
let newColumn = undefined;
if (InstanceChecker_1.InstanceChecker.isTableColumn(newTableColumnOrName)) {
newColumn = newTableColumnOrName;
}
else {
newColumn = oldColumn.clone();
newColumn.name = newTableColumnOrName;
}
await this.changeColumn(table, oldColumn, newColumn);
}
/**
* Changes a column in the table.
*/
async changeColumn(tableOrName, oldTableColumnOrName, newColumn) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
let clonedTable = table.clone();
const upQueries = [];
const downQueries = [];
const oldColumn = InstanceChecker_1.InstanceChecker.isTableColumn(oldTableColumnOrName)
? oldTableColumnOrName
: table.columns.find((column) => column.name === oldTableColumnOrName);
if (!oldColumn)
throw new error_1.TypeORMError(`Column "${oldTableColumnOrName}" was not found in the ${this.escapePath(table)} table.`);
if ((newColumn.isGenerated !== oldColumn.isGenerated &&
newColumn.generationStrategy !== "uuid") ||
oldColumn.type !== newColumn.type ||
oldColumn.length !== newColumn.length ||
oldColumn.generatedType !== newColumn.generatedType ||
oldColumn.asExpression !== newColumn.asExpression) {
// 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
await this.dropColumn(table, oldColumn);
await this.addColumn(table, newColumn);
// update cloned table
clonedTable = table.clone();
}
else {
if (newColumn.name !== oldColumn.name) {
// rename column
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME COLUMN "${oldColumn.name}" TO "${newColumn.name}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME COLUMN "${newColumn.name}" TO "${oldColumn.name}"`));
// rename column primary key constraint
if (oldColumn.isPrimary === true &&
!oldColumn.primaryKeyConstraintName) {
const primaryColumns = clonedTable.primaryColumns;
// build old primary constraint name
const columnNames = primaryColumns.map((column) => column.name);
const 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);
// build new primary constraint name
const newPkName = this.connection.namingStrategy.primaryKeyName(clonedTable, columnNames);
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME CONSTRAINT "${oldPkName}" TO "${newPkName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME CONSTRAINT "${newPkName}" TO "${oldPkName}"`));
}
// rename unique constraints
clonedTable.findColumnUniques(oldColumn).forEach((unique) => {
const oldUniqueName = this.connection.namingStrategy.uniqueConstraintName(clonedTable, unique.columnNames);
// Skip renaming if Unique has user defined constraint name
if (unique.name !== oldUniqueName)
return;
// build new constraint name
unique.columnNames.splice(unique.columnNames.indexOf(oldColumn.name), 1);
unique.columnNames.push(newColumn.name);
const newUniqueName = this.connection.namingStrategy.uniqueConstraintName(clonedTable, unique.columnNames);
// build queries
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME CONSTRAINT "${unique.name}" TO "${newUniqueName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME CONSTRAINT "${newUniqueName}" TO "${unique.name}"`));
// replace constraint name
unique.name = newUniqueName;
});
// rename index constraints
clonedTable.findColumnIndices(oldColumn).forEach((index) => {
const oldIndexName = this.connection.namingStrategy.indexName(clonedTable, index.columnNames, index.where);
// Skip renaming if Index has user defined constraint name
if (index.name !== oldIndexName)
return;
// build new constraint name
index.columnNames.splice(index.columnNames.indexOf(oldColumn.name), 1);
index.columnNames.push(newColumn.name);
const newIndexName = this.connection.namingStrategy.indexName(clonedTable, index.columnNames, index.where);
// build queries
upQueries.push(new Query_1.Query(`ALTER INDEX "${index.name}" RENAME TO "${newIndexName}"`));
downQueries.push(new Query_1.Query(`ALTER INDEX "${newIndexName}" RENAME TO "${index.name}"`));
// replace constraint name
index.name = newIndexName;
});
// rename foreign key constraints
clonedTable
.findColumnForeignKeys(oldColumn)
.forEach((foreignKey) => {
const foreignKeyName = this.connection.namingStrategy.foreignKeyName(clonedTable, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
// Skip renaming if foreign key has user defined constraint name
if (foreignKey.name !== foreignKeyName)
return;
// build new constraint name
foreignKey.columnNames.splice(foreignKey.columnNames.indexOf(oldColumn.name), 1);
foreignKey.columnNames.push(newColumn.name);
const newForeignKeyName = this.connection.namingStrategy.foreignKeyName(clonedTable, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
// build queries
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME CONSTRAINT "${foreignKey.name}" TO "${newForeignKeyName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} RENAME CONSTRAINT "${newForeignKeyName}" TO "${foreignKey.name}"`));
// replace constraint name
foreignKey.name = newForeignKeyName;
});
// rename old column in the Table object
const oldTableColumn = clonedTable.columns.find((column) => column.name === oldColumn.name);
clonedTable.columns[clonedTable.columns.indexOf(oldTableColumn)].name = newColumn.name;
oldColumn.name = newColumn.name;
}
if (this.isColumnChanged(oldColumn, newColumn, true)) {
let defaultUp = "";
let defaultDown = "";
let nullableUp = "";
let 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(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} MODIFY "${oldColumn.name}" ${this.connection.driver.createFullType(newColumn)} ${defaultUp} ${nullableUp}`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} MODIFY "${oldColumn.name}" ${this.connection.driver.createFullType(oldColumn)} ${defaultDown} ${nullableDown}`));
}
if (newColumn.isPrimary !== oldColumn.isPrimary) {
const primaryColumns = clonedTable.primaryColumns;
// if primary column state changed, we must always drop existed constraint.
if (primaryColumns.length > 0) {
const pkName = primaryColumns[0].primaryKeyConstraintName
? primaryColumns[0].primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map((column) => column.name));
const columnNames = primaryColumns
.map((column) => `"${column.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${pkName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
}
if (newColumn.isPrimary === true) {
primaryColumns.push(newColumn);
// update column in table
const column = clonedTable.columns.find((column) => column.name === newColumn.name);
column.isPrimary = true;
const pkName = primaryColumns[0].primaryKeyConstraintName
? primaryColumns[0].primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map((column) => column.name));
const columnNames = primaryColumns
.map((column) => `"${column.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${pkName}"`));
}
else {
const primaryColumn = primaryColumns.find((c) => c.name === newColumn.name);
primaryColumns.splice(primaryColumns.indexOf(primaryColumn), 1);
// update column in table
const column = clonedTable.columns.find((column) => column.name === newColumn.name);
column.isPrimary = false;
// if we have another primary keys, we must recreate constraint.
if (primaryColumns.length > 0) {
const pkName = primaryColumns[0]
.primaryKeyConstraintName
? primaryColumns[0].primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map((column) => column.name));
const columnNames = primaryColumns
.map((column) => `"${column.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${pkName}"`));
}
}
}
if (newColumn.isUnique !== oldColumn.isUnique) {
if (newColumn.isUnique === true) {
const uniqueConstraint = new TableUnique_1.TableUnique({
name: this.connection.namingStrategy.uniqueConstraintName(table, [newColumn.name]),
columnNames: [newColumn.name],
});
clonedTable.uniques.push(uniqueConstraint);
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${uniqueConstraint.name}" UNIQUE ("${newColumn.name}")`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${uniqueConstraint.name}"`));
}
else {
const uniqueConstraint = clonedTable.uniques.find((unique) => {
return (unique.columnNames.length === 1 &&
!!unique.columnNames.find((columnName) => columnName === newColumn.name));
});
clonedTable.uniques.splice(clonedTable.uniques.indexOf(uniqueConstraint), 1);
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${uniqueConstraint.name}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${uniqueConstraint.name}" UNIQUE ("${newColumn.name}")`));
}
}
await this.executeQueries(upQueries, downQueries);
this.replaceCachedTable(table, clonedTable);
}
}
/**
* Changes a column in the table.
*/
async changeColumns(tableOrName, changedColumns) {
for (const { oldColumn, newColumn } of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn);
}
}
/**
* Drops column in the table.
*/
async dropColumn(tableOrName, columnOrName) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const column = InstanceChecker_1.InstanceChecker.isTableColumn(columnOrName)
? columnOrName
: table.findColumnByName(columnOrName);
if (!column)
throw new error_1.TypeORMError(`Column "${columnOrName}" was not found in table ${this.escapePath(table)}`);
const clonedTable = table.clone();
const upQueries = [];
const downQueries = [];
// drop primary key constraint
if (column.isPrimary) {
const pkName = column.primaryKeyConstraintName
? column.primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, clonedTable.primaryColumns.map((column) => column.name));
const columnNames = clonedTable.primaryColumns
.map((primaryColumn) => `"${primaryColumn.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} DROP CONSTRAINT "${pkName}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
// update column in table
const tableColumn = clonedTable.findColumnByName(column.name);
tableColumn.isPrimary = false;
// if primary key have multiple columns, we must recreate it without dropped column
if (clonedTable.primaryColumns.length > 0) {
const pkName = clonedTable.primaryColumns[0]
.primaryKeyConstraintName
? clonedTable.primaryColumns[0].primaryKeyConstraintName
: this.connection.namingStrategy.primaryKeyName(clonedTable, clonedTable.primaryColumns.map((column) => column.name));
const columnNames = clonedTable.primaryColumns
.map((primaryColumn) => `"${primaryColumn.name}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} DROP CONSTRAINT "${pkName}"`));
}
}
// drop column index
const columnIndex = clonedTable.indices.find((index) => index.columnNames.length === 1 &&
index.columnNames[0] === column.name);
if (columnIndex) {
upQueries.push(this.dropIndexSql(columnIndex));
downQueries.push(this.createIndexSql(table, columnIndex));
}
// drop column check
const columnCheck = clonedTable.checks.find((check) => !!check.columnNames &&
check.columnNames.length === 1 &&
check.columnNames[0] === column.name);
if (columnCheck) {
clonedTable.checks.splice(clonedTable.checks.indexOf(columnCheck), 1);
upQueries.push(this.dropCheckConstraintSql(table, columnCheck));
downQueries.push(this.createCheckConstraintSql(table, columnCheck));
}
// drop column unique
const columnUnique = clonedTable.uniques.find((unique) => unique.columnNames.length === 1 &&
unique.columnNames[0] === column.name);
if (columnUnique) {
clonedTable.uniques.splice(clonedTable.uniques.indexOf(columnUnique), 1);
upQueries.push(this.dropUniqueConstraintSql(table, columnUnique));
downQueries.push(this.createUniqueConstraintSql(table, columnUnique));
}
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP COLUMN "${column.name}"`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD ${this.buildCreateColumnSql(column)}`));
if (column.generatedType && column.asExpression) {
const deleteQuery = this.deleteTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
});
const insertQuery = this.insertTypeormMetadataSql({
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
value: column.asExpression,
});
upQueries.push(deleteQuery);
downQueries.push(insertQuery);
}
await this.executeQueries(upQueries, downQueries);
clonedTable.removeColumn(column);
this.replaceCachedTable(table, clonedTable);
}
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName, columns) {
for (const column of columns) {
await this.dropColumn(tableOrName, column);
}
}
/**
* Creates a new primary key.
*/
async createPrimaryKey(tableOrName, columnNames, constraintName) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const clonedTable = table.clone();
const up = this.createPrimaryKeySql(table, columnNames, constraintName);
// mark columns as primary, because dropPrimaryKeySql build constraint name from table primary column names.
clonedTable.columns.forEach((column) => {
if (columnNames.find((columnName) => columnName === column.name))
column.isPrimary = true;
});
const down = this.dropPrimaryKeySql(clonedTable);
await this.executeQueries(up, down);
this.replaceCachedTable(table, clonedTable);
}
/**
* Updates composite primary keys.
*/
async updatePrimaryKeys(tableOrName, columns) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const columnNames = columns.map((column) => column.name);
const clonedTable = table.clone();
const upQueries = [];
const downQueries = [];
// if table already have primary columns, we must drop them.
const primaryColumns = clonedTable.primaryColumns;
if (primaryColumns.length > 0) {