typeorm
Version:
Data-Mapper ORM for TypeScript and ES2021+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.
962 lines (961 loc) • 128 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CockroachQueryRunner = 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 TableExclusion_1 = require("../../schema-builder/table/TableExclusion");
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 VersionUtils_1 = require("../../util/VersionUtils");
const Query_1 = require("../Query");
const MetadataTableType_1 = require("../types/MetadataTableType");
/**
* Runs queries on a single postgres database connection.
*/
class CockroachQueryRunner extends BaseQueryRunner_1.BaseQueryRunner {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(driver, mode) {
super();
/**
* Stores all executed queries to be able to run them again if transaction fails.
*/
this.queries = [];
/**
* Indicates if running queries must be stored
*/
this.storeQueries = false;
/**
* Current number of transaction retries in case of 40001 error.
*/
this.transactionRetries = 0;
this.driver = driver;
this.connection = driver.connection;
this.mode = mode;
this.broadcaster = new Broadcaster_1.Broadcaster(this);
}
// -------------------------------------------------------------------------
// 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, release]) => {
this.driver.connectedQueryRunners.push(this);
this.databaseConnection = connection;
const onErrorCallback = (err) => this.releaseConnection(err);
this.releaseCallback = (err) => {
this.databaseConnection.removeListener("error", onErrorCallback);
release(err);
};
this.databaseConnection.on("error", onErrorCallback);
return this.databaseConnection;
});
}
else {
// master
this.databaseConnectionPromise = this.driver
.obtainMasterConnection()
.then(([connection, release]) => {
this.driver.connectedQueryRunners.push(this);
this.databaseConnection = connection;
const onErrorCallback = (err) => this.releaseConnection(err);
this.releaseCallback = (err) => {
this.databaseConnection.removeListener("error", onErrorCallback);
release(err);
};
this.databaseConnection.on("error", onErrorCallback);
return this.databaseConnection;
});
}
return this.databaseConnectionPromise;
}
/**
* Release a connection back to the pool, optionally specifying an Error to release with.
* Per pg-pool documentation this will prevent the pool from re-using the broken connection.
*/
async releaseConnection(err) {
if (this.isReleased) {
return;
}
this.isReleased = true;
if (this.releaseCallback) {
this.releaseCallback(err);
this.releaseCallback = undefined;
}
const index = this.driver.connectedQueryRunners.indexOf(this);
if (index !== -1) {
this.driver.connectedQueryRunners.splice(index, 1);
}
}
/**
* Releases used database connection.
* You cannot use query runner methods once its released.
*/
release() {
return this.releaseConnection();
}
/**
* Starts transaction.
*/
async startTransaction(isolationLevel) {
this.isTransactionActive = true;
this.transactionRetries = 0;
try {
await this.broadcaster.broadcast("BeforeTransactionStart");
}
catch (err) {
this.isTransactionActive = false;
throw err;
}
if (this.transactionDepth === 0) {
await this.query("START TRANSACTION");
await this.query("SAVEPOINT cockroach_restart");
if (isolationLevel) {
await this.query("SET TRANSACTION ISOLATION LEVEL " + isolationLevel);
}
}
else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`);
}
this.transactionDepth += 1;
this.storeQueries = true;
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(`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`);
this.transactionDepth -= 1;
}
else {
this.storeQueries = false;
await this.query("RELEASE SAVEPOINT cockroach_restart");
await this.query("COMMIT");
this.queries = [];
this.isTransactionActive = false;
this.transactionRetries = 0;
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 {
this.storeQueries = false;
await this.query("ROLLBACK");
this.queries = [];
this.isTransactionActive = false;
this.transactionRetries = 0;
}
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();
if (this.isTransactionActive && this.storeQueries) {
this.queries.push({ query, parameters });
}
try {
const raw = await new Promise((ok, fail) => {
databaseConnection.query(query, parameters, (err, raw) => (err ? fail(err) : ok(raw)));
});
// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.options.maxQueryExecutionTime;
const queryEndTime = Date.now();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime &&
queryExecutionTime > maxQueryExecutionTime) {
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);
}
const result = new QueryResult_1.QueryResult();
if (raw.hasOwnProperty("rowCount")) {
result.affected = raw.rowCount;
}
if (raw.hasOwnProperty("rows")) {
result.records = raw.rows;
}
switch (raw.command) {
case "DELETE":
// for DELETE query additionally return number of affected rows
result.raw = [raw.rows, raw.rowCount];
break;
default:
result.raw = raw.rows;
}
this.broadcaster.broadcastAfterQueryEvent(broadcasterResult, query, parameters, true, queryExecutionTime, raw, undefined);
if (useStructuredResult) {
return result;
}
else {
return result.raw;
}
}
catch (err) {
if (err.code === "40001" &&
this.isTransactionActive &&
this.transactionRetries <
(this.driver.options.maxTransactionRetries || 5)) {
this.transactionRetries += 1;
this.storeQueries = false;
await this.query("ROLLBACK TO SAVEPOINT cockroach_restart");
const sleepTime = 2 ** this.transactionRetries *
0.1 *
(Math.random() + 0.5) *
1000;
await new Promise((resolve) => setTimeout(resolve, sleepTime));
let result = undefined;
for (const q of this.queries) {
this.driver.connection.logger.logQuery(`Retrying transaction for query "${q.query}"`, q.parameters, this);
result = await this.query(q.query, q.parameters);
}
this.transactionRetries = 0;
this.storeQueries = true;
return result;
}
else {
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) {
const QueryStream = this.driver.loadStreamDependency();
if (this.isReleased) {
throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
}
const databaseConnection = await this.connect();
this.driver.connection.logger.logQuery(query, parameters, this);
const stream = databaseConnection.query(new QueryStream(query, parameters));
if (onEnd) {
stream.on("end", onEnd);
}
if (onError) {
stream.on("error", onError);
}
return stream;
}
/**
* 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) {
const result = await this.query(`SELECT * FROM "pg_database" WHERE "datname" = '${database}'`);
return result.length ? true : false;
}
/**
* Loads currently using database
*/
async getCurrentDatabase() {
const query = await this.query(`SELECT * FROM current_database()`);
return query[0]["current_database"];
}
/**
* Checks if schema with the given name exist.
*/
async hasSchema(schema) {
const result = await this.query(`SELECT * FROM "information_schema"."schemata" WHERE "schema_name" = '${schema}'`);
return result.length ? true : false;
}
/**
* Loads currently using database schema
*/
async getCurrentSchema() {
const query = await this.query(`SELECT * FROM current_schema()`);
return query[0]["current_schema"];
}
/**
* Checks if table with the given name exist in the database.
*/
async hasTable(tableOrName) {
const parsedTableName = this.driver.parseTableName(tableOrName);
if (!parsedTableName.schema) {
parsedTableName.schema = await this.getCurrentSchema();
}
const sql = `SELECT * FROM "information_schema"."tables" WHERE "table_schema" = '${parsedTableName.schema}' AND "table_name" = '${parsedTableName.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 parsedTableName = this.driver.parseTableName(tableOrName);
if (!parsedTableName.schema) {
parsedTableName.schema = await this.getCurrentSchema();
}
const sql = `SELECT * FROM "information_schema"."columns" WHERE "table_schema" = '${parsedTableName.schema}' AND "table_name" = '${parsedTableName.tableName}' AND "column_name" = '${columnName}'`;
const result = await this.query(sql);
return result.length ? true : false;
}
/**
* Creates a new database.
*/
async createDatabase(database, ifNotExist) {
const up = `CREATE DATABASE ${ifNotExist ? "IF NOT EXISTS " : ""} "${database}"`;
const down = `DROP DATABASE "${database}"`;
await this.executeQueries(new Query_1.Query(up), new Query_1.Query(down));
}
/**
* Drops database.
*/
async dropDatabase(database, ifExist) {
const up = `DROP DATABASE ${ifExist ? "IF EXISTS " : ""} "${database}"`;
const down = `CREATE DATABASE "${database}"`;
await this.executeQueries(new Query_1.Query(up), new Query_1.Query(down));
}
/**
* Creates a new table schema.
*/
async createSchema(schemaPath, ifNotExist) {
const schema = schemaPath.indexOf(".") === -1
? schemaPath
: schemaPath.split(".")[1];
const up = ifNotExist
? `CREATE SCHEMA IF NOT EXISTS "${schema}"`
: `CREATE SCHEMA "${schema}"`;
const down = `DROP SCHEMA "${schema}" CASCADE`;
await this.executeQueries(new Query_1.Query(up), new Query_1.Query(down));
}
/**
* Drops table schema.
*/
async dropSchema(schemaPath, ifExist, isCascade) {
const schema = schemaPath.indexOf(".") === -1
? schemaPath
: schemaPath.split(".")[1];
const up = ifExist
? `DROP SCHEMA IF EXISTS "${schema}" ${isCascade ? "CASCADE" : ""}`
: `DROP SCHEMA "${schema}" ${isCascade ? "CASCADE" : ""}`;
const down = `CREATE SCHEMA "${schema}"`;
await this.executeQueries(new Query_1.Query(up), new Query_1.Query(down));
}
/**
* 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 = [];
// if table have column with ENUM type, we must create this type in postgres.
const enumColumns = table.columns.filter((column) => column.type === "enum" || column.type === "simple-enum");
const createdEnumTypes = [];
for (const column of enumColumns) {
// TODO: Should also check if values of existing type matches expected ones
const hasEnum = await this.hasEnumType(table, column);
const enumName = this.buildEnumName(table, column);
// if enum with the same "enumName" is defined more then once, me must prevent double creation
if (!hasEnum && createdEnumTypes.indexOf(enumName) === -1) {
createdEnumTypes.push(enumName);
upQueries.push(this.createEnumTypeSql(table, column, enumName));
downQueries.push(this.dropEnumTypeSql(table, column, enumName));
}
}
table.columns
.filter((column) => column.isGenerated &&
column.generationStrategy === "increment")
.forEach((column) => {
upQueries.push(new Query_1.Query(`CREATE SEQUENCE ${this.escapePath(this.buildSequencePath(table, column))}`));
downQueries.push(new Query_1.Query(`DROP SEQUENCE ${this.escapePath(this.buildSequencePath(table, column))}`));
});
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
.filter((index) => !index.isUnique)
.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(table, 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 currentSchema = await this.getCurrentSchema();
let { schema } = this.driver.parseTableName(table);
if (!schema) {
schema = currentSchema;
}
const insertQuery = this.insertTypeormMetadataSql({
schema: schema,
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
value: column.asExpression,
});
const deleteQuery = this.deleteTypeormMetadataSql({
schema: schema,
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(target, 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(target);
if (!isTableExist)
return Promise.resolve();
}
// if dropTable called with dropForeignKeys = true, we must create foreign keys in down query.
const createForeignKeys = dropForeignKeys;
const tablePath = this.getTablePath(target);
const table = await this.getCachedTable(tablePath);
const upQueries = [];
const downQueries = [];
// foreign keys must be dropped before indices, because fk's rely on indices
if (dropForeignKeys)
table.foreignKeys.forEach((foreignKey) => upQueries.push(this.dropForeignKeySql(table, foreignKey)));
if (dropIndices) {
table.indices.forEach((index) => {
upQueries.push(this.dropIndexSql(table, index));
downQueries.push(this.createIndexSql(table, index));
});
}
upQueries.push(this.dropTableSql(table));
downQueries.push(this.createTableSql(table, createForeignKeys));
table.columns
.filter((column) => column.isGenerated &&
column.generationStrategy === "increment")
.forEach((column) => {
upQueries.push(new Query_1.Query(`DROP SEQUENCE ${this.escapePath(this.buildSequencePath(table, column))}`));
downQueries.push(new Query_1.Query(`CREATE SEQUENCE ${this.escapePath(this.buildSequencePath(table, column))}`));
});
// 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 currentSchema = await this.getCurrentSchema();
let { schema } = this.driver.parseTableName(table);
if (!schema) {
schema = currentSchema;
}
const deleteQuery = this.deleteTypeormMetadataSql({
schema: schema,
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
});
const insertQuery = this.insertTypeormMetadataSql({
schema: schema,
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(await this.insertViewDefinitionSql(view));
downQueries.push(this.dropViewSql(view));
if (syncWithMetadata)
downQueries.push(await 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(await this.deleteViewDefinitionSql(view));
upQueries.push(this.dropViewSql(view));
downQueries.push(await 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 { schema: schemaName, tableName: oldTableName } = this.driver.parseTableName(oldTable);
newTable.name = schemaName
? `${schemaName}.${newTableName}`
: newTableName;
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 column 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);
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 { schema } = this.driver.parseTableName(newTable);
const newIndexName = this.connection.namingStrategy.indexName(newTable, index.columnNames, index.where);
// build queries
const up = schema
? `ALTER INDEX "${schema}"."${index.name}" RENAME TO "${newIndexName}"`
: `ALTER INDEX "${index.name}" RENAME TO "${newIndexName}"`;
const down = schema
? `ALTER INDEX "${schema}"."${newIndexName}" RENAME TO "${index.name}"`
: `ALTER INDEX "${newIndexName}" RENAME TO "${index.name}"`;
upQueries.push(new Query_1.Query(up));
downQueries.push(new Query_1.Query(down));
// 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;
});
// rename ENUM types
const enumColumns = newTable.columns.filter((column) => column.type === "enum" || column.type === "simple-enum");
for (const column of enumColumns) {
// skip renaming for user-defined enum name
if (column.enumName)
continue;
const oldEnumType = await this.getUserDefinedTypeName(oldTable, column);
upQueries.push(new Query_1.Query(`ALTER TYPE "${oldEnumType.schema}"."${oldEnumType.name}" RENAME TO ${this.buildEnumName(newTable, column, false)}`));
downQueries.push(new Query_1.Query(`ALTER TYPE ${this.buildEnumName(newTable, column)} RENAME TO "${oldEnumType.name}"`));
}
await this.executeQueries(upQueries, downQueries);
}
/**
* 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 = [];
if (column.generationStrategy === "increment") {
throw new error_1.TypeORMError(`Adding sequential generated columns into existing table is not supported`);
}
if (column.type === "enum" || column.type === "simple-enum") {
const hasEnum = await this.hasEnumType(table, column);
if (!hasEnum) {
upQueries.push(this.createEnumTypeSql(table, column));
downQueries.push(this.dropEnumTypeSql(table, column));
}
}
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD ${this.buildCreateColumnSql(table, 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
// todo: https://go.crdb.dev/issue-v/48026/v21.1
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}"`));
}
if (column.generatedType && column.asExpression) {
const currentSchema = await this.getCurrentSchema();
let { schema } = this.driver.parseTableName(table);
if (!schema) {
schema = currentSchema;
}
const insertQuery = this.insertTypeormMetadataSql({
schema: schema,
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
value: column.asExpression,
});
const deleteQuery = this.deleteTypeormMetadataSql({
schema: schema,
table: table.name,
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
name: column.name,
});
upQueries.push(insertQuery);
downQueries.push(deleteQuery);
}
// create column index
const columnIndex = clonedTable.indices.find((index) => index.columnNames.length === 1 &&
index.columnNames[0] === column.name);
if (columnIndex) {
// CockroachDB stores unique indices as UNIQUE constraints
if (columnIndex.isUnique) {
const unique = new TableUnique_1.TableUnique({
name: this.connection.namingStrategy.uniqueConstraintName(table, columnIndex.columnNames),
columnNames: columnIndex.columnNames,
});
upQueries.push(this.createUniqueConstraintSql(table, unique));
downQueries.push(this.dropIndexSql(table, unique));
clonedTable.uniques.push(unique);
}
else {
upQueries.push(this.createIndexSql(table, columnIndex));
downQueries.push(this.dropIndexSql(table, 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(this.createUniqueConstraintSql(table, uniqueConstraint));
downQueries.push(this.dropIndexSql(table, uniqueConstraint.name)); // CockroachDB creates indices for unique constraints
}
// create column's comment
if (column.comment) {
upQueries.push(new Query_1.Query(`COMMENT ON COLUMN ${this.escapePath(table)}."${column.name}" IS ${this.escapeComment(column.comment)}`));
downQueries.push(new Query_1.Query(`COMMENT ON COLUMN ${this.escapePath(table)}."${column.name}" IS ${this.escapeComment(column.comment)}`));
}
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 "${table.name}" table.`);
let newColumn;
if (InstanceChecker_1.InstanceChecker.isTableColumn(newTableColumnOrName)) {
newColumn = newTableColumnOrName;
}
else {
newColumn = oldColumn.clone();
newColumn.name = newTableColumnOrName;
}
return 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 = [];
let defaultValueChanged = false;
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 "${table.name}" table.`);
if (oldColumn.type !== newColumn.type ||
oldColumn.length !== newColumn.length ||
newColumn.isArray !== oldColumn.isArray ||
oldColumn.generatedType !== newColumn.generatedType ||
oldColumn.asExpression !== newColumn.asExpression) {
// To avoid data conversion, we just recreate column
await this.dropColumn(table, oldColumn);
await this.addColumn(table, newColumn);
// update cloned table
clonedTable = table.clone();
}
else {
if (oldColumn.name !== newColumn.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 ENUM type
if (oldColumn.type === "enum" ||
oldColumn.type === "simple-enum") {
const oldEnumType = await this.getUserDefinedTypeName(table, oldColumn);
upQueries.push(new Query_1.Query(`ALTER TYPE "${oldEnumType.schema}"."${oldEnumType.name}" RENAME TO ${this.buildEnumName(table, newColumn, false)}`));
downQueries.push(new Query_1.Query(`ALTER TYPE ${this.buildEnumName(table, newColumn)} RENAME TO "${oldEnumType.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 { schema } = this.driver.parseTableName(table);
const newIndexName = this.connection.namingStrategy.indexName(clonedTable, index.columnNames, index.where);
// build queries
const up = schema
? `ALTER INDEX "${schema}"."${index.name}" RENAME TO "${newIndexName}"`
: `ALTER INDEX "${index.name}" RENAME TO "${newIndexName}"`;
const down = schema
? `ALTER INDEX "${schema}"."${newIndexName}" RENAME TO "${index.name}"`
: `ALTER INDEX "${newIndexName}" RENAME TO "${index.name}"`;
upQueries.push(new Query_1.Query(up));
downQueries.push(new Query_1.Query(down));
// 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 (newColumn.precision !== oldColumn.precision ||
newColumn.scale !== oldColumn.scale) {
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${newColumn.name}" TYPE ${this.driver.createFullType(newColumn)}`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${newColumn.name}" TYPE ${this.driver.createFullType(oldColumn)}`));
}
if (oldColumn.isNullable !== newColumn.isNullable) {
if (newColumn.isNullable) {
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${oldColumn.name}" DROP NOT NULL`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${oldColumn.name}" SET NOT NULL`));
}
else {
upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${oldColumn.name}" SET NOT NULL`));
downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${oldColumn.name}" DROP NOT NULL`));
}
}
if (oldColumn.comment !== newColumn.comment) {
upQueries.push(new Query_1.Query(`COMMENT ON COLUMN ${this.escapePath(table)}."${oldColumn.name}" IS ${this.escapeComment(newColumn.comment)}`));
downQueries.push(new Query_1.Query(`COMMENT ON COLUMN ${this.escapePath(table)}."${newColumn.name}" IS ${this.escapeComment(oldColumn.comment)}`));
}
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}"`)