typeorm
Version:
Data-Mapper ORM for TypeScript and ES2023+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.
2,091 lines • 99 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 validate_isolation_level_1 = require("../validate-isolation-level");
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.dataSource = driver.dataSource;
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.
*
* @param isolationLevel
*/
async startTransaction(isolationLevel) {
isolationLevel ??=
this.dataSource.options.isolationLevel ?? "READ COMMITTED";
(0, validate_isolation_level_1.validateIsolationLevel)(this.driver.supportedIsolationLevels, isolationLevel);
if (this.isReleased)
throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
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.
*
* @param query
* @param parameters
* @param useStructuredResult
*/
async query(query, parameters, useStructuredResult = false) {
if (this.isReleased)
throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
const databaseConnection = await this.connect();
this.driver.dataSource.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.dataSource.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.dataSource.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.
*
* @param query
* @param parameters
* @param onEnd
* @param onError
*/
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.dataSource.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.dataSource.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.
*
* @param database
*/
async getSchemas(database) {
return Promise.resolve([]);
}
/**
* Checks if database with the given name exist.
*
* @param database
*/
async hasDatabase(database) {
try {
const query = await this.query(`SELECT 1 AS "exists" FROM global_name@${this.driver.escape(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.
*
* @param schema
*/
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.
*
* @param tableOrName
*/
async hasTable(tableOrName) {
const { tableName } = this.driver.parseTableName(tableOrName);
const sql = `SELECT "TABLE_NAME" FROM "USER_TABLES" WHERE "TABLE_NAME" = :1`;
const result = await this.query(sql, [tableName]);
return result.length ? true : false;
}
/**
* Checks if column with the given name exist in the given table.
*
* @param tableOrName
* @param columnName
*/
async hasColumn(tableOrName, columnName) {
const { tableName } = this.driver.parseTableName(tableOrName);
const sql = `SELECT "COLUMN_NAME" FROM "USER_TAB_COLS" WHERE "TABLE_NAME" = :1 AND "COLUMN_NAME" = :2`;
const result = await this.query(sql, [tableName, columnName]);
return result.length ? true : false;
}
/**
* Creates a new database.
*
* @param database
* @param ifNotExists
*/
async createDatabase(database, ifNotExists) {
// Even with `IF NOT EXISTS` we get:
// ORA-01501: CREATE DATABASE failed
// ORA-01100: database already mounted
if (ifNotExists) {
try {
await this.query(`CREATE DATABASE IF NOT EXISTS ${this.driver.escape(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 ${this.driver.escape(database)}`);
}
}
/**
* Drops database.
*
* @param database
* @param ifExists
*/
async dropDatabase(database, ifExists) {
return Promise.resolve();
}
/**
* Creates a new table schema.
*
* @param schemaPath
* @param ifNotExists
*/
async createSchema(schemaPath, ifNotExists) {
throw new error_1.TypeORMError(`Schema create queries are not supported by Oracle driver.`);
}
/**
* Drops table schema.
*
* @param schemaPath
* @param ifExists
*/
async dropSchema(schemaPath, ifExists) {
throw new error_1.TypeORMError(`Schema drop queries are not supported by Oracle driver.`);
}
/**
* Creates a new table.
*
* @param table
* @param ifNotExists
* @param createForeignKeys
* @param createIndices
*/
async createTable(table, ifNotExists = false, createForeignKeys = true, createIndices = true) {
if (ifNotExists) {
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.
index.name ??= this.dataSource.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.
*
* @param tableOrName
* @param ifExists
* @param dropForeignKeys
* @param dropIndices
*/
async dropTable(tableOrName, ifExists, 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 (ifExists) {
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.
*
* @param view
* @param syncWithMetadata
*/
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.
*
* @param target
* @param ifExists
*/
async dropView(target, ifExists) {
const viewName = InstanceChecker_1.InstanceChecker.isView(target) ? target.name : target;
const view = ifExists
? await this.getCachedView(viewName).catch(() => undefined)
: await this.getCachedView(viewName);
if (!view)
return;
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.
*
* @param oldTableOrName
* @param newTableName
*/
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.dataSource.namingStrategy.primaryKeyName(oldTable, columnNames);
const newPkName = this.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.
*
* @param tableOrName
* @param column
*/
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 ??
this.dataSource.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 ??
this.dataSource.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.dataSource.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.
*
* @param tableOrName
* @param columns
*/
async addColumns(tableOrName, columns) {
for (const column of columns) {
await this.addColumn(tableOrName, column);
}
}
/**
* Renames column in the given table.
*
* @param tableOrName
* @param oldTableColumnOrName
* @param newTableColumnOrName
*/
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;
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.
*
* @param tableOrName
* @param oldTableColumnOrName
* @param newColumn
*/
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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.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.dataSource.driver.createFullType(newColumn)} ${defaultUp} ${nullableUp}`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} MODIFY "${oldColumn.name}" ${this.dataSource.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 ??
this.dataSource.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 ??
this.dataSource.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 ??
this.dataSource.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.dataSource.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.
*
* @param tableOrName
* @param changedColumns
*/
async changeColumns(tableOrName, changedColumns) {
for (const { oldColumn, newColumn } of changedColumns) {
await this.changeColumn(tableOrName, oldColumn, newColumn);
}
}
/**
* Drops column in the table.
*
* @param tableOrName
* @param columnOrName
* @param ifExists
*/
async dropColumn(tableOrName, columnOrName, ifExists) {
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) {
if (ifExists)
return;
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 ??
this.dataSource.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 ??
this.dataSource.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.
*
* @param tableOrName
* @param columns
* @param ifExists
*/
async dropColumns(tableOrName, columns, ifExists) {
for (const column of [...columns]) {
await this.dropColumn(tableOrName, column, ifExists);
}
}
/**
* Creates a new primary key.
*
* @param tableOrName
* @param columnNames
* @param constraintName
*/
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.
*
* @param tableOrName
* @param columns
*/
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) {
const pkName = primaryColumns[0].primaryKeyConstraintName ??
this.dataSource.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map((column) => column.name));
const columnNamesString = 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 (${columnNamesString})`));
}
// update columns in table.
clonedTable.columns
.filter((column) => columnNames.indexOf(column.name) !== -1)
.forEach((column) => {
column.isPrimary = true;
});
const pkName = primaryColumns[0].primaryKeyConstraintName ??
this.dataSource.namingStrategy.primaryKeyName(clonedTable, columnNames);
const columnNamesString = columnNames
.map((columnName) => `"${columnName}"`)
.join(", ");
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNamesString})`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${pkName}"`));
await this.executeQueries(upQueries, downQueries);
this.replaceCachedTable(table, clonedTable);
}
/**
* Drops a primary key.
*
* @param tableOrName
* @param constraintName
* @param ifExists
*/
async dropPrimaryKey(tableOrName, constraintName, ifExists) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
if (ifExists && table.primaryColumns.length === 0)
return;
const up = this.dropPrimaryKeySql(table);
const down = this.createPrimaryKeySql(table, table.primaryColumns.map((column) => column.name), constraintName);
await this.executeQueries(up, down);
table.primaryColumns.forEach((column) => {
column.isPrimary = false;
});
}
/**
* Creates a new unique constraint.
*
* @param tableOrName
* @param uniqueConstraint
*/
async createUniqueConstraint(tableOrName, uniqueConstraint) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
// new unique constraint may be passed without name. In this case we generate unique name manually.
uniqueConstraint.name ??=
this.dataSource.namingStrategy.uniqueConstraintName(table, uniqueConstraint.columnNames);
const up = this.createUniqueConstraintSql(table, uniqueConstraint);
const down = this.dropUniqueConstraintSql(table, uniqueConstraint);
await this.executeQueries(up, down);
table.addUniqueConstraint(uniqueConstraint);
}
/**
* Creates a new unique constraints.
*
* @param tableOrName
* @param uniqueConstraints
*/
async createUniqueConstraints(tableOrName, uniqueConstraints) {
const promises = uniqueConstraints.map((uniqueConstraint) => this.createUniqueConstraint(tableOrName, uniqueConstraint));
await Promise.all(promises);
}
/**
* Drops a unique constraint.
*
* @param tableOrName
* @param uniqueOrName
* @param ifExists
*/
async dropUniqueConstraint(tableOrName, uniqueOrName, ifExists) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const uniqueConstraint = InstanceChecker_1.InstanceChecker.isTableUnique(uniqueOrName)
? uniqueOrName
: table.uniques.find((u) => u.name === uniqueOrName);
if (!uniqueConstraint) {
if (ifExists)
return;
throw new error_1.TypeORMError(`Supplied unique constraint was not found in table ${table.name}`);
}
const up = this.dropUniqueConstraintSql(table, uniqueConstraint);
const down = this.createUniqueConstraintSql(table, uniqueConstraint);
await this.executeQueries(up, down);
table.removeUniqueConstraint(uniqueConstraint);
}
/**
* Drops unique constraints.
*
* @param tableOrName
* @param uniqueConstraints
* @param ifExists
*/
async dropUniqueConstraints(tableOrName, uniqueConstraints, ifExists) {
const promises = uniqueConstraints.map((uniqueConstraint) => this.dropUniqueConstraint(tableOrName, uniqueConstraint, ifExists));
await Promise.all(promises);
}
/**
* Creates new check constraint.
*
* @param tableOrName
* @param checkConstraint
*/
async createCheckConstraint(tableOrName, checkConstraint) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
// new unique constraint may be passed without name. In this case we generate unique name manually.
checkConstraint.name ??=
this.dataSource.namingStrategy.checkConstraintName(table, checkConstraint.expression);
const up = this.createCheckConstraintSql(table, checkConstraint);
const down = this.dropCheckConstraintSql(table, checkConstraint);
await this.executeQueries(up, down);
table.addCheckConstraint(checkConstraint);
}
/**
* Creates new check constraints.
*
* @param tableOrName
* @param checkConstraints
*/
async createCheckConstraints(tableOrName, checkConstraints) {
const promises = checkConstraints.map((checkConstraint) => this.createCheckConstraint(tableOrName, checkConstraint));
await Promise.all(promises);
}
/**
* Drops check constraint.
*
* @param tableOrName
* @param checkOrName
* @param ifExists
*/
async dropCheckConstraint(tableOrName, checkOrName, ifExists) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const checkConstraint = InstanceChecker_1.InstanceChecker.isTableCheck(checkOrName)
? checkOrName
: table.checks.find((c) => c.name === checkOrName);
if (!checkConstraint) {
if (ifExists)
return;
throw new error_1.TypeORMError(`Supplied check constraint was not found in table ${table.name}`);
}
const up = this.dropCheckConstraintSql(table, checkConstraint);
const down = this.createCheckConstraintSql(table, checkConstraint);
await this.executeQueries(up, down);
table.removeCheckConstraint(checkConstraint);
}
/**
* Drops check constraints.
*
* @param tableOrName
* @param checkConstraints
* @param ifExists
*/
async dropCheckConstraints(tableOrName, checkConstraints, ifExists) {
const promises = checkConstraints.map((checkConstraint) => this.dropCheckConstraint(tableOrName, checkConstraint, ifExists));
await Promise.all(promises);
}
/**
* Creates a new exclusion constraint.
*
* @param tableOrName
* @param exclusionConstraint
*/
async createExclusionConstraint(tableOrName, exclusionConstraint) {
throw new error_1.TypeORMError(`Oracle does not support exclusion constraints.`);
}
/**
* Creates a new exclusion constraints.
*
* @param tableOrName
* @param exclusionConstraints
*/
async createExclusionConstraints(tableOrName, exclusionConstraints) {
throw new error_1.TypeORMError(`Oracle does not support exclusion constraints.`);
}
/**
* Drops exclusion constraint.
*
* @param tableOrName
* @param exclusionOrName
* @param ifExists
*/
async dropExclusionConstraint(tableOrName, exclusionOrName, ifExists) {
throw new error_1.TypeORMError(`Oracle does not support exclusion constraints.`);
}
/**
* Drops exclusion constraints.
*
* @param tableOrName
* @param exclusionConstraints
* @param ifExists
*/
async dropExclusionConstraints(tableOrName, exclusionConstraints, ifExists) {
throw new error_1.TypeORMError(`Oracle does not support exclusion constraints.`);
}
/**
* Creates a new foreign key.
*
* @param tableOrName
* @param foreignKey
*/
async createForeignKey(tableOrName, foreignKey) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
// new FK may be passed without name. In this case we generate FK name manually.
foreignKey.name ??= this.dataSource.namingStrategy.foreignKeyName(table, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
const up = this.createForeignKeySql(table, foreignKey);
const down = this.dropForeignKeySql(table, foreignKey);
await this.executeQueries(up, down);
table.addForeignKey(foreignKey);
}
/**
* Creates a new foreign keys.
*
* @param tableOrName
* @param foreignKeys
*/
async createForeignKeys(tableOrName, foreignKeys) {
const promises = foreignKeys.map((foreignKey) => this.createForeignKey(tableOrName, foreignKey));
await Promise.all(promises);
}
/**
* Drops a foreign key from the table.
*
* @param tableOrName
* @param foreignKeyOrName
* @param ifExists
*/
async dropForeignKey(tableOrName, foreignKeyOrName, ifExists) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const foreignKey = InstanceChecker_1.InstanceChecker.isTableForeignKey(foreignKeyOrName)
? foreignKeyOrName
: table.foreignKeys.find((fk) => fk.name === foreignKeyOrName);
if (!foreignKey) {
if (ifExists)
return;
throw new error_1.TypeORMError(`Supplied foreign key was not found in table ${table.name}`);
}
foreignKey.name ??= this.dataSource.namingStrategy.foreignKeyName(table, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
const up = this.dropForeignKeySql(table, foreignKey);
const down = this.createForeignKeySql(table, foreignKey);
await this.executeQueries(up, down);
table.removeForeignKey(foreignKey);
}
/**
* Drops a foreign keys from the table.
*
* @param tableOrName
* @param foreignKeys
* @param ifExists
*/
async dropForeignKeys(tableOrName, foreignKeys, ifExists) {
const promises = foreignKeys.map((foreignKey) => this.dropForeignKey(tableOrName, foreignKey, ifExists));
await Promise.all(promises);
}
/**
* Creates a new index.
*
* @param tableOrName
* @param index
*/
async createIndex(tableOrName, index) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
// new index may be passed without name. In this case we generate index name manually.
index.name ??= this.generateIndexName(table, index);
const up = this.createIndexSql(table, index);
const down = this.dropIndexSql(index);
await this.executeQueries(up, down);
table.addIndex(index);
}
/**
* Creates a new indices
*
* @param tableOrName
* @param indices
*/
async createIndices(tableOrName, indices) {
const promises = indices.map((index) => this.createIndex(tableOrName, index));
await Promise.all(promises);
}
/**
* Drops an index from the table.
*
* @param tableOrName
* @param indexOrName
* @param ifExists
*/
async dropIndex(tableOrName, indexOrName, ifExists) {
const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName)
? tableOrName
: await this.getCachedTable(tableOrName);
const index = InstanceChecker_1.InstanceChecker.isTableIndex(indexOrName)
? indexOrName
: table.indices.find((i) => i.name === indexOrName);
if (!index) {
if (ifExists)
return;
throw new error_1.TypeORMError(`Supplied index ${indexOrName} was not found in table ${table.name}`);
}
// old index may be passed without name. In this case we generate index name manually.
index.name ??= this.generateIndexName(table, index);
const up = this.dropIndexSql(index);
const down = this.createIndexSql(table, index);
await this.executeQueries(up, down);
table.removeIndex(index);
}
/**
* Drops an indices from the table.
*
* @param tableOrName
* @param indices
* @param ifExists
*/
async dropIndices(tableOrName, indices, ifExists) {
const promises = indices.map((index) => this.dropIndex(tableOrName, index, ifExists));
await Promise.all(promises);
}
/**
* Clears all table contents.
* Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.
*
* @param tableName
* @param options
* @param options.cascade
*/
async clearTable(tableName, options) {
const cascade = options?.cascade ? " CASCADE" : "";
await this.query(`TRUNCATE TABLE ${this.escapePath(tableName)}${cascade}`);
}
/**
* Removes all tables from the currently connected database.
*/
async clearDatabase() {
const isAnotherTransactionActive = this.isTransactionActive;
if (!isAnotherTransactionActive)
await this.startTransaction();
try {
// drop views
const dropViewsQuery = `SELECT 'DROP VIEW "' || VIEW_NAME || '"' AS "query" FROM "USER_VIEWS"`;
const dropViewQueries = await this.query(dropViewsQuery);
await Promise.all(dropViewQueries.map((query) => this.query(query["query"])));
// drop materialized views
const dropMatViewsQuery = `SELECT 'DROP MATERIALIZED VIEW "' || MVIEW_NAME || '"' AS "query" FROM "USER_MVIEWS"`;
const dropMatViewQueries = await this.query(dropMatViewsQuery);
await Promise.all(dropMatViewQueries.map((query) => this.query(query["query"])));
// drop tables
const dropTablesQuery = `SELECT 'DROP TABLE "' || TABLE_NAME || '" CASCADE CONSTRAINTS' AS "query" FROM "USER_TABLES"`;
const dropTableQueries = await this.query(dropTablesQuery);
await Promise.all(dropTableQueries.map((query) => this.query(query["query"])));
if (!isAnotherTransactionActive)
await this.commitTransaction();
}
catch (error) {
try {
// we throw original error even if rollback thrown an error
if (!isAnotherTransactionActive)
await this.rollbackTransaction();
}
catch (rollbackError) { }
throw error;
}
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
async loadViews(viewNames) {
const hasTable = await this.hasTable(this.getTypeormMetadataTableName());
if (!hasTable) {
return [];
}
viewNames ??= [];
const currentDatabase = await this.getCurrentDatabase();
const currentSchema = await this.getCurrentSchema();
const viewsCondition = viewNames
.map((viewName) => this.driver.parseTableName(viewName))
.map(({ schema, tableName }) => {
schema ??= this.driver.options.schema ?? currentSchema;
return `("T"."schema" = '${schema}' AND "T"."name" = '${tableName}')`;
})
.join(" OR ");
let query = `SELECT "T".* FROM ${this.escapePath(this.getTypeormMetadataTableName())} "T" ` +
`INNER JOIN "USER_OBJECTS" "O" ON "O"."OBJECT_NAME" = "T"."name" AND "O"."OBJECT_TYPE" IN ( 'MATERIALIZED VIEW', 'VIEW' ) ` +
`WHERE "T"."type" IN ('${MetadataTableType_1.MetadataTableType.MATERIALIZED_VIEW}', '${MetadataTableType_1.MetadataTableType.VIEW}')`;
if (viewsCondition.length > 0)
query += ` AND ${viewsCondition}`;
const dbViews = await this.query(query);
return dbViews.map((dbView) => {
const parsedName = this.driver.parseTableName(dbView["name"]);
const view = new View_1.View();
view.database =
parsedName.database ?? dbView["database"] ?? currentDatabase;
view.schema = parsedName.schema ?? dbView["schema"] ?? currentSchema;
view.name = parsedName.tableName;
view.expression = dbView["value"];
view.materialized =
dbView["type"] === MetadataTableType_1.MetadataTableType.MATERIALIZED_VIEW;
return view;
});
}
/**
* Loads all tables (with given names) from the database and creates a Table from them.
*
* @param tableNames
*/
async loadTables(tableNames) {
if (tableNames?.length === 0) {
return [];
}
const dbTables = [];
const currentSchema = await this.getCurrentSchema();
const currentDatabase = await this.getCurrentDatabase();
if (!tableNames) {
const tablesSql = `SELECT "TABLE_NAME", "OWNER" FROM "ALL_TABLES"`;
dbTables.push(...(await this.query(tablesSql)));
}
else {
const tablesCondition = tableNames
.map((tableName) => {
const parts = tableName.split(".");
if (parts.length >= 3) {
const [, schema, name] = parts;
return `("OWNER" = '${schema}' AND "TABLE_NAME" = '${name}')`;
}
else if (parts.length === 2) {
const [schema, name] = parts;
return `("OWNER" = '${schema}' AND "TABLE_NAME" = '${name}')`;
}
else if (parts.length === 1) {
const [name] = parts;
return `("TABLE_NAME" = '${name}')`;
}
else {
return `(1=0)`;
}
})
.join(" OR ");
const tablesSql = `SELECT "TABLE_NAME", "OWNER" FROM "ALL_TABLES" WHERE ${tablesCondition}`;
dbTables.push(...(await this.query(tablesSql)));
}
// if tables were not found in the db, no need to proceed
if (dbTables.length === 0) {
return [];
}
// load tables, columns, indices and foreign keys
const columnsCondition = dbTables
.map(({ TABLE_NAME, OWNER }) => {
return `("C"."OWNER" = '${OWNER}' AND "C"."TABLE_NAME" = '${TABLE_NAME}')`;
})
.join(" OR ");
const columnsSql = `SELECT * FROM "ALL_TAB_COLS" "C" WHERE (${columnsCondition})`;
const indicesSql = `SELECT "C"."INDEX_NAME", "C"."OWNER", "C"."TABLE_NAME", "C"."UNIQUENESS", ` +
`LISTAGG ("COL"."COLUMN_NAME", ',') WITHIN GROUP (ORDER BY "COL"."COLUMN_NAME") AS "COLUMN_NAMES" ` +
`FROM "ALL_INDEXES" "C" ` +
`INNER JOIN "ALL_IND_COLUMNS" "COL" ON "COL"."INDEX_OWNER" = "C"."OWNER" AND "COL"."INDEX_NAME" = "C"."INDEX_NAME" ` +
`LEFT JOIN "ALL_CONSTRAINTS" "CON" ON "CON"."OWNER" = "C"."OWNER" AND "CON"."CONSTRAINT_NAME" = "C"."INDEX_NAME" ` +
`WHERE (${columnsCondition}) AND "CON"."CONSTRAINT_NAME" IS NULL ` +
`GROUP BY "C"."INDEX_NAME", "C"."OWNER", "C"."TABLE_NAME", "C"."UNIQUENESS"`;
const foreignKeysSql = `SELECT "C"."CONSTRAINT_NAME", "C"."OWNER", "C"."TABLE_NAME", "COL"."COLUMN_NAME", "REF_COL"."TABLE_NAME" AS "REFERENCED_TABLE_NAME", ` +
`"REF_COL"."COLUMN_NAME" AS "REFERENCED_COLUMN_NAME", "C"."DELETE_RULE" AS "ON_DELETE" ` +
`FROM "ALL_CONSTRAINTS" "C" ` +
`INNER JOIN "ALL_CONS_COLUMNS" "COL" ON "COL"."OWNER" = "C"."OWNER" AND "COL"."CONSTRAINT_NAME" = "C"."CONSTRAINT_NAME" ` +
`INNER JOIN "ALL_CONS_COLUMNS" "REF_COL" ON "REF_COL"."OWNER" = "C"."R_OWNER" AND "REF_COL"."CONSTRAINT_NAME" = "C"."R_CONSTRAINT_NAME" AND "REF_COL"."POSITION" = "COL"."POSITION" ` +
`WHERE (${columnsCondition}) AND "C"."CONSTRAINT_TYPE" = 'R'`;
const constraintsSql = `SELECT "C"."CONSTRAINT_NAME", "C"."CONSTRAINT_TYPE", "C"."OWNER", "C"."TABLE_NAME", "COL"."COLUMN_NAME", "C"."SEARCH_CONDITION" ` +
`FROM "ALL_CONSTRAINTS" "C" ` +
`INNER JOIN "ALL_CONS_COLUMNS" "COL" ON "COL"."OWNER" = "C"."OWNER" AND "COL"."CONSTRAINT_NAME" = "C"."CONSTRAINT_NAME" ` +
`WHERE (${columnsCondition}) AND "C"."CONSTRAINT_TYPE" IN ('C', 'U', 'P') AND "C"."GENERATED" = 'USER NAME'`;
const [dbColumns, dbIndices, dbForeignKeys, dbConstraints,] = await Promise.all([
this.query(columnsSql),
this.query(indicesSql),
this.query(foreignKeysSql),
this.query(constraintsSql),
]);
// create tables for loaded tables
return await Promise.all(dbTables.map(async (dbTable) => {
const table = new Table_1.Table();
const owner = dbTable["OWNER"] === currentSchema &&
(!this.driver.options.schema ||
this.driver.options.schema === currentSchema)
? undefined
: dbTable["OWNER"];
table.database = currentDatabase;
table.schema = dbTable["OWNER"];
table.name = this.driver.buildTableName(dbTable["TABLE_NAME"], owner);
// create columns from the loaded columns
table.columns = await Promise.all(dbColumns
.filter((dbColumn) => dbColumn["OWNER"] === dbTable["OWNER"] &&
dbColumn["TABLE_NAME"] ===
dbTable["TABLE_NAME"] &&
// Filter out auto-generated virtual columns,
// since TypeORM will have no info about them.
!(dbColumn["VIRTUAL_COLUMN"] === "YES" &&
dbColumn["USER_GENERATED"] === "NO"))
.map(async (dbColumn) => {
const columnConstraints = dbConstraints.filter((dbConstraint) => dbConstraint["OWNER"] ===
dbColumn["OWNER"] &&
dbConstraint["TABLE_NAME"] ===
dbColumn["TABLE_NAME"] &&
dbConstraint["COLUMN_NAME"] ===
dbColumn["COLUMN_NAME"]);
const uniqueConstraints = columnConstraints.filter((constraint) => constraint["CONSTRAINT_TYPE"] === "U");
const isConstraintComposite = uniqueConstraints.every((uniqueConstraint) => {
return dbConstraints.some((dbConstraint) => dbConstraint["OWNER"] ===
dbColumn["OWNER"] &&
dbConstraint["TABLE_NAME"] ===
dbColumn["TABLE_NAME"] &&
dbConstraint["COLUMN_NAME"] !==
dbColumn["COLUMN_NAME"] &&
dbConstraint["CONSTRAINT_NAME"] ===
uniqueConstraint["CONSTRAINT_NAME"] &&
dbConstraint["CONSTRAINT_TYPE"] ===
"U");
});
const tableColumn = new TableColumn_1.TableColumn();
tableColumn.name = dbColumn["COLUMN_NAME"];
tableColumn.type =
dbColumn["DATA_TYPE"].toLowerCase();
if (tableColumn.type.indexOf("(") !== -1)
tableColumn.type = tableColumn.type.replace(/\([0-9]*\)/, "");
// check only columns that have length property
if (this.driver.withLengthColumnTypes.indexOf(tableColumn.type) !== -1) {
const length = tableColumn.type === "raw"
? dbColumn["DATA_LENGTH"]
: dbColumn["CHAR_COL_DECL_LENGTH"];
tableColumn.length =
length &&
!this.isDefaultColumnLength(table, tableColumn, length)
? length.toString()
: "";
}
if (tableColumn.type === "number" ||
tableColumn.type === "float") {
if (dbColumn["DATA_PRECISION"] !== null &&
!this.isDefaultColumnPrecision(table, tableColumn, dbColumn["DATA_PRECISION"]))
tableColumn.precision =
dbColumn["DATA_PRECISION"];
if (dbColumn["DATA_SCALE"] !== null &&
!this.isDefaultColumnScale(table, tableColumn, dbColumn["DATA_SCALE"]))
tableColumn.scale = dbColumn["DATA_SCALE"];
}
else if ((tableColumn.type === "timestamp" ||
tableColumn.type ===
"timestamp with time zone" ||
tableColumn.type ===
"timestamp with local time zone") &&
dbColumn["DATA_SCALE"] !== null) {
tableColumn.precision =
!this.isDefaultColumnPrecision(table, tableColumn, dbColumn["DATA_SCALE"])
? dbColumn["DATA_SCALE"]
: undefined;
}
tableColumn.default =
dbColumn["DATA_DEFAULT"] !== null &&
dbColumn["DATA_DEFAULT"] !== undefined &&
dbColumn["VIRTUAL_COLUMN"] === "NO" &&
dbColumn["DATA_DEFAULT"].trim() !== "NULL"
? (tableColumn.default =
dbColumn["DATA_DEFAULT"].trim())
: undefined;
const primaryConstraint = columnConstraints.find((constraint) => constraint["CONSTRAINT_TYPE"] === "P");
if (primaryConstraint) {
tableColumn.isPrimary = true;
// find another columns involved in primary key constraint
const anotherPrimaryConstraints = dbConstraints.filter((constraint) => constraint["OWNER"] ===
dbColumn["OWNER"] &&
constraint["TABLE_NAME"] ===
dbColumn["TABLE_NAME"] &&
constraint["COLUMN_NAME"] !==
dbColumn["COLUMN_NAME"] &&
constraint["CONSTRAINT_TYPE"] ===
"P");
// collect all column names
const columnNames = anotherPrimaryConstraints.map((constraint) => constraint["COLUMN_NAME"]);
columnNames.push(dbColumn["COLUMN_NAME"]);
// build default primary key constraint name
const pkName = this.dataSource.namingStrategy.primaryKeyName(table, columnNames);
// if primary key has user-defined constraint name, write it in table column
if (primaryConstraint["CONSTRAINT_NAME"] !==
pkName) {
tableColumn.primaryKeyConstraintName =
primaryConstraint["CONSTRAINT_NAME"];
}
}
tableColumn.isNullable =
dbColumn["NULLABLE"] === "Y";
tableColumn.isUnique =
uniqueConstraints.length > 0 &&
!isConstraintComposite;
tableColumn.isGenerated =
dbColumn["IDENTITY_COLUMN"] === "YES";
if (tableColumn.isGenerated) {
tableColumn.generationStrategy = "increment";
tableColumn.default = undefined;
}
tableColumn.comment = ""; // todo
if (dbColumn["VIRTUAL_COLUMN"] === "YES") {
tableColumn.generatedType = "VIRTUAL";
const asExpressionQuery = this.selectTypeormMetadataSql({
table: dbTable["TABLE_NAME"],
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: tableColumn.name,
});
const results = await this.query(asExpressionQuery.query, asExpressionQuery.parameters);
if (results[0]?.value) {
tableColumn.asExpression = results[0].value;
}
else {
tableColumn.asExpression = "";
}
}
return tableColumn;
}));
// find unique constraints of table, group them by constraint name and build TableUnique.
const tableUniqueConstraints = OrmUtils_1.OrmUtils.uniq(dbConstraints.filter((dbConstraint) => {
return (dbConstraint["TABLE_NAME"] ===
dbTable["TABLE_NAME"] &&
dbConstraint["OWNER"] === dbTable["OWNER"] &&
dbConstraint["CONSTRAINT_TYPE"] === "U");
}), (dbConstraint) => dbConstraint["CONSTRAINT_NAME"]);
table.uniques = tableUniqueConstraints.map((constraint) => {
const uniques = dbConstraints.filter((dbC) => dbC["CONSTRAINT_NAME"] ===
constraint["CONSTRAINT_NAME"]);
return new TableUnique_1.TableUnique({
name: constraint["CONSTRAINT_NAME"],
columnNames: uniques.map((u) => u["COLUMN_NAME"]),
});
});
// find check constraints of table, group them by constraint name and build TableCheck.
const tableCheckConstraints = OrmUtils_1.OrmUtils.uniq(dbConstraints.filter((dbConstraint) => {
return (dbConstraint["TABLE_NAME"] ===
dbTable["TABLE_NAME"] &&
dbConstraint["OWNER"] === dbTable["OWNER"] &&
dbConstraint["CONSTRAINT_TYPE"] === "C");
}), (dbConstraint) => dbConstraint["CONSTRAINT_NAME"]);
table.checks = tableCheckConstraints.map((constraint) => {
const checks = dbConstraints.filter((dbC) => dbC["TABLE_NAME"] === constraint["TABLE_NAME"] &&
dbC["OWNER"] === constraint["OWNER"] &&
dbC["CONSTRAINT_NAME"] ===
constraint["CONSTRAINT_NAME"]);
return new TableCheck_1.TableCheck({
name: constraint["CONSTRAINT_NAME"],
columnNames: checks.map((c) => c["COLUMN_NAME"]),
expression: constraint["SEARCH_CONDITION"],
});
});
// find foreign key constraints of table, group them by constraint name and build TableForeignKey.
const tableForeignKeyConstraints = OrmUtils_1.OrmUtils.uniq(dbForeignKeys.filter((dbForeignKey) => dbForeignKey["OWNER"] === dbTable["OWNER"] &&
dbForeignKey["TABLE_NAME"] ===
dbTable["TABLE_NAME"]), (dbForeignKey) => dbForeignKey["CONSTRAINT_NAME"]);
table.foreignKeys = tableForeignKeyConstraints.map((dbForeignKey) => {
const foreignKeys = dbForeignKeys.filter((dbFk) => dbFk["TABLE_NAME"] ===
dbForeignKey["TABLE_NAME"] &&
dbFk["OWNER"] === dbForeignKey["OWNER"] &&
dbFk["CONSTRAINT_NAME"] ===
dbForeignKey["CONSTRAINT_NAME"]);
return new TableForeignKey_1.TableForeignKey({
name: dbForeignKey["CONSTRAINT_NAME"],
columnNames: foreignKeys.map((dbFk) => dbFk["COLUMN_NAME"]),
referencedDatabase: table.database,
referencedSchema: dbForeignKey["OWNER"],
referencedTableName: dbForeignKey["REFERENCED_TABLE_NAME"],
referencedColumnNames: foreignKeys.map((dbFk) => dbFk["REFERENCED_COLUMN_NAME"]),
onDelete: dbForeignKey["ON_DELETE"],
onUpdate: "NO ACTION", // Oracle does not have onUpdate option in FK's, but we need it for proper synchronization
});
});
// Attempt to map auto-generated virtual columns to their
// referenced columns, through its 'DATA_DEFAULT' property.
//
// An example of this happening is when a column of type
// TIMESTAMP WITH TIME ZONE is indexed. Oracle will create a
// virtual column of type TIMESTAMP with a default value of
// SYS_EXTRACT_UTC(<column>).
const autoGenVirtualDbColumns = dbColumns
.filter((dbColumn) => dbColumn["OWNER"] === dbTable["OWNER"] &&
dbColumn["TABLE_NAME"] === dbTable["TABLE_NAME"] &&
dbColumn["VIRTUAL_COLUMN"] === "YES" &&
dbColumn["USER_GENERATED"] === "NO")
.reduce((acc, x) => {
const referencedDbColumn = dbColumns.find((dbColumn) => x["DATA_DEFAULT"].includes(dbColumn["COLUMN_NAME"]));
if (!referencedDbColumn)
return acc;
return {
...acc,
[x["COLUMN_NAME"]]: referencedDbColumn["COLUMN_NAME"],
};
}, {});
// create TableIndex objects from the loaded indices
table.indices = dbIndices
.filter((dbIndex) => dbIndex["TABLE_NAME"] === dbTable["TABLE_NAME"] &&
dbIndex["OWNER"] === dbTable["OWNER"])
.map((dbIndex) => {
//
const columnNames = dbIndex["COLUMN_NAMES"]
.split(",")
.map((columnName) => autoGenVirtualDbColumns[columnName] ??
columnName);
return new TableIndex_1.TableIndex({
name: dbIndex["INDEX_NAME"],
columnNames,
isUnique: dbIndex["UNIQUENESS"] === "UNIQUE",
});
});
return table;
}));
}
/**
* Builds and returns SQL for create table.
*
* @param table
* @param createForeignKeys
*/
createTableSql(table, createForeignKeys) {
const columnDefinitions = table.columns
.map((column) => this.buildCreateColumnSql(column))
.join(", ");
let sql = `CREATE TABLE ${this.escapePath(table)} (${columnDefinitions}`;
table.columns
.filter((column) => column.isUnique)
.forEach((column) => {
const isUniqueExist = table.uniques.some((unique) => unique.columnNames.length === 1 &&
unique.columnNames[0] === column.name);
if (!isUniqueExist)
table.uniques.push(new TableUnique_1.TableUnique({
name: this.dataSource.namingStrategy.uniqueConstraintName(table, [column.name]),
columnNames: [column.name],
}));
});
if (table.uniques.length > 0) {
const uniquesSql = table.uniques
.map((unique) => {
const uniqueName = unique.name ??
this.dataSource.namingStrategy.uniqueConstraintName(table, unique.columnNames);
const columnNames = unique.columnNames
.map((columnName) => `"${columnName}"`)
.join(", ");
return `CONSTRAINT "${uniqueName}" UNIQUE (${columnNames})`;
})
.join(", ");
sql += `, ${uniquesSql}`;
}
if (table.checks.length > 0) {
const checksSql = table.checks
.map((check) => {
const checkName = check.name ??
this.dataSource.namingStrategy.checkConstraintName(table, check.expression);
return `CONSTRAINT "${checkName}" CHECK (${check.expression})`;
})
.join(", ");
sql += `, ${checksSql}`;
}
if (table.foreignKeys.length > 0 && createForeignKeys) {
const foreignKeysSql = table.foreignKeys
.map((fk) => {
const columnNames = fk.columnNames
.map((columnName) => `"${columnName}"`)
.join(", ");
fk.name ??= this.dataSource.namingStrategy.foreignKeyName(table, fk.columnNames, this.getTablePath(fk), fk.referencedColumnNames);
const referencedColumnNames = fk.referencedColumnNames
.map((columnName) => `"${columnName}"`)
.join(", ");
let constraint = `CONSTRAINT "${fk.name}" FOREIGN KEY (${columnNames}) REFERENCES ${this.escapePath(this.getTablePath(fk))} (${referencedColumnNames})`;
if (fk.onDelete && fk.onDelete !== "NO ACTION") {
// Oracle does not support NO ACTION, but we set NO ACTION by default in EntityMetadata
constraint += ` ON DELETE ${fk.onDelete}`;
}
return constraint;
})
.join(", ");
sql += `, ${foreignKeysSql}`;
}
const primaryColumns = table.columns.filter((column) => column.isPrimary);
if (primaryColumns.length > 0) {
const primaryKeyName = primaryColumns[0].primaryKeyConstraintName ??
this.dataSource.namingStrategy.primaryKeyName(table, primaryColumns.map((column) => column.name));
const columnNames = primaryColumns
.map((column) => `"${column.name}"`)
.join(", ");
sql += `, CONSTRAINT "${primaryKeyName}" PRIMARY KEY (${columnNames})`;
}
sql += `)`;
return new Query_1.Query(sql);
}
/**
* Builds drop table sql.
*
* @param tableOrName
* @param ifExists
*/
dropTableSql(tableOrName, ifExists) {
const query = ifExists
? `DROP TABLE IF EXISTS ${this.escapePath(tableOrName)}`
: `DROP TABLE ${this.escapePath(tableOrName)}`;
return new Query_1.Query(query);
}
createViewSql(view) {
const materializedClause = view.materialized ? "MATERIALIZED " : "";
if (typeof view.expression === "string") {
return new Query_1.Query(`CREATE ${materializedClause}VIEW ${this.escapePath(view)} AS ${view.expression}`);
}
else {
return new Query_1.Query(`CREATE ${materializedClause}VIEW ${this.escapePath(view)} AS ${view.expression(this.dataSource).getQuery()}`);
}
}
insertViewDefinitionSql(view) {
const expression = typeof view.expression === "string"
? view.expression.trim()
: view.expression(this.dataSource).getQuery();
const type = view.materialized
? MetadataTableType_1.MetadataTableType.MATERIALIZED_VIEW
: MetadataTableType_1.MetadataTableType.VIEW;
const { schema, tableName } = this.driver.parseTableName(view);
return this.insertTypeormMetadataSql({
type: type,
name: tableName,
schema: schema,
value: expression,
});
}
/**
* Builds drop view sql.
*
* @param view
*/
dropViewSql(view) {
const materializedClause = view.materialized ? "MATERIALIZED " : "";
return new Query_1.Query(`DROP ${materializedClause}VIEW ${this.escapePath(view)}`);
}
/**
* Builds remove view sql.
*
* @param view
*/
deleteViewDefinitionSql(view) {
const type = view.materialized
? MetadataTableType_1.MetadataTableType.MATERIALIZED_VIEW
: MetadataTableType_1.MetadataTableType.VIEW;
return this.deleteTypeormMetadataSql({ type, name: view.name });
}
/**
* Builds create index sql.
*
* @param table
* @param index
*/
createIndexSql(table, index) {
const columns = index.columnNames
.map((columnName) => `"${columnName}"`)
.join(", ");
return new Query_1.Query(`CREATE ${index.isUnique ? "UNIQUE " : ""}INDEX "${index.name}" ON ${this.escapePath(table)} (${columns})`);
}
/**
* Builds drop index sql.
*
* @param indexOrName
*/
dropIndexSql(indexOrName) {
const indexName = InstanceChecker_1.InstanceChecker.isTableIndex(indexOrName)
? indexOrName.name
: indexOrName;
return new Query_1.Query(`DROP INDEX "${indexName}"`);
}
/**
* Builds create primary key sql.
*
* @param table
* @param columnNames
* @param constraintName
*/
createPrimaryKeySql(table, columnNames, constraintName) {
const primaryKeyName = constraintName ??
this.dataSource.namingStrategy.primaryKeyName(table, columnNames);
const columnNamesString = columnNames
.map((columnName) => `"${columnName}"`)
.join(", ");
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${primaryKeyName}" PRIMARY KEY (${columnNamesString})`);
}
/**
* Builds drop primary key sql.
*
* @param table
*/
dropPrimaryKeySql(table) {
if (!table.primaryColumns.length)
throw new error_1.TypeORMError(`Table ${table} has no primary keys.`);
const columnNames = table.primaryColumns.map((column) => column.name);
const constraintName = table.primaryColumns[0].primaryKeyConstraintName;
const primaryKeyName = constraintName ??
this.dataSource.namingStrategy.primaryKeyName(table, columnNames);
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${primaryKeyName}"`);
}
/**
* Builds create unique constraint sql.
*
* @param table
* @param uniqueConstraint
*/
createUniqueConstraintSql(table, uniqueConstraint) {
const columnNames = uniqueConstraint.columnNames
.map((column) => `"` + column + `"`)
.join(", ");
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${uniqueConstraint.name}" UNIQUE (${columnNames})`);
}
/**
* Builds drop unique constraint sql.
*
* @param table
* @param uniqueOrName
*/
dropUniqueConstraintSql(table, uniqueOrName) {
const uniqueName = InstanceChecker_1.InstanceChecker.isTableUnique(uniqueOrName)
? uniqueOrName.name
: uniqueOrName;
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${uniqueName}"`);
}
/**
* Builds create check constraint sql.
*
* @param table
* @param checkConstraint
*/
createCheckConstraintSql(table, checkConstraint) {
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${checkConstraint.name}" CHECK (${checkConstraint.expression})`);
}
/**
* Builds drop check constraint sql.
*
* @param table
* @param checkOrName
*/
dropCheckConstraintSql(table, checkOrName) {
const checkName = InstanceChecker_1.InstanceChecker.isTableCheck(checkOrName)
? checkOrName.name
: checkOrName;
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${checkName}"`);
}
/**
* Builds create foreign key sql.
*
* @param table
* @param foreignKey
*/
createForeignKeySql(table, foreignKey) {
const columnNames = foreignKey.columnNames
.map((column) => `"` + column + `"`)
.join(", ");
const referencedColumnNames = foreignKey.referencedColumnNames
.map((column) => `"` + column + `"`)
.join(",");
let sql = `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${foreignKey.name}" FOREIGN KEY (${columnNames}) ` +
`REFERENCES ${this.escapePath(this.getTablePath(foreignKey))} (${referencedColumnNames})`;
// Oracle does not support NO ACTION, but we set NO ACTION by default in EntityMetadata
if (foreignKey.onDelete && foreignKey.onDelete !== "NO ACTION") {
sql += ` ON DELETE ${foreignKey.onDelete}`;
}
return new Query_1.Query(sql);
}
/**
* Builds drop foreign key sql.
*
* @param table
* @param foreignKeyOrName
*/
dropForeignKeySql(table, foreignKeyOrName) {
const foreignKeyName = InstanceChecker_1.InstanceChecker.isTableForeignKey(foreignKeyOrName)
? foreignKeyOrName.name
: foreignKeyOrName;
return new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${foreignKeyName}"`);
}
/**
* Builds a query for create column.
*
* @param column
*/
buildCreateColumnSql(column) {
let c = `"${column.name}" ` + this.dataSource.driver.createFullType(column);
if (column.charset)
c += " CHARACTER SET " + column.charset;
if (column.collation)
c += " COLLATE " + column.collation;
if (column.asExpression)
c += ` AS (${column.asExpression}) VIRTUAL`;
if (column.default !== undefined && column.default !== null)
// DEFAULT must be placed before NOT NULL
c += " DEFAULT " + column.default;
if (column.isNullable !== true && !column.isGenerated)
// NOT NULL is not supported with GENERATED
c += " NOT NULL";
if (column.isGenerated === true &&
column.generationStrategy === "increment")
c += " GENERATED BY DEFAULT AS IDENTITY";
return c;
}
/**
* Escapes given table or view path.
*
* @param target
*/
escapePath(target) {
// Ignore database when escaping paths
const { schema, tableName } = this.driver.parseTableName(target);
if (schema && schema !== this.driver.schema) {
return `"${schema}"."${tableName}"`;
}
return `"${tableName}"`;
}
/**
* Change table comment.
*
* @param tableOrName
* @param comment
*/
changeTableComment(tableOrName, comment) {
throw new error_1.TypeORMError(`oracle driver does not support change table comment.`);
}
}
exports.OracleQueryRunner = OracleQueryRunner;
//# sourceMappingURL=OracleQueryRunner.js.map