UNPKG

typeorm

Version:

Data-Mapper ORM for TypeScript and ES2023+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.

1,009 lines • 88.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuroraMysqlQueryRunner = void 0; const error_1 = require("../../error"); const NamedPlaceholdersNotSupportedError_1 = require("../../error/NamedPlaceholdersNotSupportedError"); 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 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 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 mysql database connection. */ class AuroraMysqlQueryRunner extends BaseQueryRunner_1.BaseQueryRunner { // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- constructor(driver, client) { super(); this.driver = driver; this.dataSource = driver.dataSource; this.client = client; 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. */ async connect() { return {}; } /** * Releases used database connection. * You cannot use query runner methods once its released. */ release() { this.isReleased = true; if (this.databaseConnection) this.databaseConnection.release(); return Promise.resolve(); } /** * Starts transaction on the current connection. * * @param isolationLevel */ async startTransaction(isolationLevel) { isolationLevel ??= this.dataSource.options.isolationLevel; (0, validate_isolation_level_1.validateIsolationLevel)(this.driver.supportedIsolationLevels, isolationLevel); this.isTransactionActive = true; try { await this.broadcaster.broadcast("BeforeTransactionStart"); } catch (err) { this.isTransactionActive = false; throw err; } if (this.transactionDepth === 0) { try { await this.client.startTransaction(); } catch (err) { this.isTransactionActive = false; throw err; } } 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(`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`); } else { await this.client.commitTransaction(); 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.client.rollbackTransaction(); this.isTransactionActive = false; } this.transactionDepth -= 1; await this.broadcaster.broadcast("AfterTransactionRollback"); } /** * Executes a raw SQL query. * * @param query * @param parameters * @param useStructuredResult */ async query(query, parameters, useStructuredResult = false) { if (this.isReleased) throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError(); if (parameters && !Array.isArray(parameters)) throw new NamedPlaceholdersNotSupportedError_1.NamedPlaceholdersNotSupportedError(); const raw = await this.client.query(query, parameters); const result = new QueryResult_1.QueryResult(); result.raw = raw; if (raw?.hasOwnProperty("records") && Array.isArray(raw.records)) { result.records = raw.records; } if (raw?.hasOwnProperty("numberOfRecordsUpdated")) { result.affected = raw.numberOfRecordsUpdated; } if (!useStructuredResult) { return result.raw; } return result; } /** * Returns raw data stream. * * @param query * @param parameters * @param onEnd * @param onError */ stream(query, parameters, onEnd, onError) { if (this.isReleased) throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError(); return new Promise(async (ok, fail) => { try { const databaseConnection = await this.connect(); const stream = databaseConnection.query(query, parameters); if (onEnd) stream.on("end", onEnd); if (onError) stream.on("error", onError); ok(stream); } catch (err) { fail(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) { throw new error_1.TypeORMError(`MySql driver does not support table schemas`); } /** * Checks if database with the given name exist. * * @param database */ async hasDatabase(database) { const result = await this.query(`SELECT * FROM \`INFORMATION_SCHEMA\`.\`SCHEMATA\` WHERE \`SCHEMA_NAME\` = ?`, [database]); return result.length ? true : false; } /** * Loads currently using database */ async getCurrentDatabase() { const query = await this.query(`SELECT DATABASE() AS \`db_name\``); return query[0]["db_name"]; } /** * Checks if schema with the given name exist. * * @param schema */ async hasSchema(schema) { throw new error_1.TypeORMError(`MySql driver does not support table schemas`); } /** * Loads currently using database schema */ async getCurrentSchema() { const query = await this.query(`SELECT SCHEMA() AS \`schema_name\``); return query[0]["schema_name"]; } /** * Checks if table with the given name exist in the database. * * @param tableOrName */ async hasTable(tableOrName) { const parsedTableName = this.driver.parseTableName(tableOrName); const sql = `SELECT * FROM \`INFORMATION_SCHEMA\`.\`COLUMNS\` WHERE \`TABLE_SCHEMA\` = ? AND \`TABLE_NAME\` = ?`; const result = await this.query(sql, [ parsedTableName.database, parsedTableName.tableName, ]); return result.length ? true : false; } /** * Checks if column with the given name exist in the given table. * * @param tableOrName * @param column */ async hasColumn(tableOrName, column) { const parsedTableName = this.driver.parseTableName(tableOrName); const columnName = InstanceChecker_1.InstanceChecker.isTableColumn(column) ? column.name : column; const sql = `SELECT * FROM \`INFORMATION_SCHEMA\`.\`COLUMNS\` WHERE \`TABLE_SCHEMA\` = ? AND \`TABLE_NAME\` = ? AND \`COLUMN_NAME\` = ?`; const result = await this.query(sql, [ parsedTableName.database, parsedTableName.tableName, columnName, ]); return result.length ? true : false; } /** * Creates a new database. * * @param database * @param ifNotExists */ async createDatabase(database, ifNotExists) { const up = ifNotExists ? `CREATE DATABASE IF NOT EXISTS ${this.driver.escape(database)}` : `CREATE DATABASE ${this.driver.escape(database)}`; const down = `DROP DATABASE ${this.driver.escape(database)}`; await this.executeQueries(new Query_1.Query(up), new Query_1.Query(down)); } /** * Drops database. * * @param database * @param ifExists */ async dropDatabase(database, ifExists) { const up = ifExists ? `DROP DATABASE IF EXISTS ${this.driver.escape(database)}` : `DROP DATABASE ${this.driver.escape(database)}`; const down = `CREATE DATABASE ${this.driver.escape(database)}`; await this.executeQueries(new Query_1.Query(up), new Query_1.Query(down)); } /** * 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 MySql driver.`); } /** * Drops table schema. * * @param schemaPath * @param ifExists */ async dropSchema(schemaPath, ifExists) { throw new error_1.TypeORMError(`Schema drop queries are not supported by MySql driver.`); } /** * Creates a new table. * * @param table * @param ifNotExists * @param createForeignKeys */ async createTable(table, ifNotExists = false, createForeignKeys = 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)); // we must first drop indices, than drop foreign keys, because drop queries runs in reversed order // and foreign keys will be dropped first as indices. This order is very important, because we can't drop index // if it related to the foreign key. // createTable does not need separate method to create indices, because it create indices in the same query with table creation. table.indices.forEach((index) => downQueries.push(this.dropIndexSql(table, index))); // 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))); return this.executeQueries(upQueries, downQueries); } /** * Drop the table. * * @param target * @param ifExists * @param dropForeignKeys */ async dropTable(target, ifExists, dropForeignKeys = 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(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 = []; if (dropForeignKeys) table.foreignKeys.forEach((foreignKey) => upQueries.push(this.dropForeignKeySql(table, foreignKey))); table.indices.forEach((index) => upQueries.push(this.dropIndexSql(table, index))); upQueries.push(this.dropTableSql(table)); downQueries.push(this.createTableSql(table, createForeignKeys)); 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(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. * * @param target * @param ifExists */ async dropView(target, ifExists) { const viewName = InstanceChecker_1.InstanceChecker.isView(target) ? target.name : target; const view = await this.getCachedView(viewName); await this.executeQueries([ await this.deleteViewDefinitionSql(view), this.dropViewSql(view, ifExists), ], [ await this.insertViewDefinitionSql(view), this.createViewSql(view), ]); } /** * Renames a 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 } = this.driver.parseTableName(oldTable); newTable.name = database ? `${database}.${newTableName}` : newTableName; // rename table upQueries.push(new Query_1.Query(`RENAME TABLE ${this.escapePath(oldTable)} TO ${this.escapePath(newTable)}`)); downQueries.push(new Query_1.Query(`RENAME TABLE ${this.escapePath(newTable)} TO ${this.escapePath(oldTable)}`)); // rename index constraints newTable.indices.forEach((index) => { // build new constraint name const columnNames = index.columnNames .map((column) => `\`${column}\``) .join(", "); const newIndexName = this.dataSource.namingStrategy.indexName(newTable, index.columnNames, index.where); // build queries let indexType = ""; if (index.isUnique) indexType += "UNIQUE "; if (index.isSpatial) indexType += "SPATIAL "; if (index.isFulltext) indexType += "FULLTEXT "; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} DROP INDEX \`${index.name}\`, ADD ${indexType}INDEX \`${newIndexName}\` (${columnNames})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(newTable)} DROP INDEX \`${newIndexName}\`, ADD ${indexType}INDEX \`${index.name}\` (${columnNames})`)); // replace constraint name index.name = newIndexName; }); // rename foreign key constraint newTable.foreignKeys.forEach((foreignKey) => { // build new constraint name const columnNames = foreignKey.columnNames .map((column) => `\`${column}\``) .join(", "); const referencedColumnNames = foreignKey.referencedColumnNames .map((column) => `\`${column}\``) .join(","); const newForeignKeyName = this.dataSource.namingStrategy.foreignKeyName(newTable, foreignKey.columnNames); // build queries let up = `ALTER TABLE ${this.escapePath(newTable)} DROP FOREIGN KEY \`${foreignKey.name}\`, ADD CONSTRAINT \`${newForeignKeyName}\` FOREIGN KEY (${columnNames}) ` + `REFERENCES ${this.escapePath(this.getTablePath(foreignKey))}(${referencedColumnNames})`; if (foreignKey.onDelete) up += ` ON DELETE ${foreignKey.onDelete}`; if (foreignKey.onUpdate) up += ` ON UPDATE ${foreignKey.onUpdate}`; let down = `ALTER TABLE ${this.escapePath(newTable)} DROP FOREIGN KEY \`${newForeignKeyName}\`, ADD CONSTRAINT \`${foreignKey.name}\` FOREIGN KEY (${columnNames}) ` + `REFERENCES ${this.escapePath(this.getTablePath(foreignKey))}(${referencedColumnNames})`; if (foreignKey.onDelete) down += ` ON DELETE ${foreignKey.onDelete}`; if (foreignKey.onUpdate) down += ` ON UPDATE ${foreignKey.onUpdate}`; upQueries.push(new Query_1.Query(up)); downQueries.push(new Query_1.Query(down)); // 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 = []; const skipColumnLevelPrimary = clonedTable.primaryColumns.length > 0; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD ${this.buildCreateColumnSql(column, skipColumnLevelPrimary, false)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP COLUMN \`${column.name}\``)); // create or update primary key constraint if (column.isPrimary && skipColumnLevelPrimary) { // if we already have generated column, we must temporary drop AUTO_INCREMENT property. const generatedColumn = clonedTable.columns.find((column) => column.isGenerated && column.generationStrategy === "increment"); if (generatedColumn) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${column.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(column, true)}`)); } const primaryColumns = clonedTable.primaryColumns; let columnNames = primaryColumns .map((column) => `\`${column.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD PRIMARY KEY (${columnNames})`)); primaryColumns.push(column); columnNames = primaryColumns .map((column) => `\`${column.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD PRIMARY KEY (${columnNames})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); // if we previously dropped AUTO_INCREMENT property, we must bring it back if (generatedColumn) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(column, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${column.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); } } // create column index const columnIndex = clonedTable.indices.find((index) => index.columnNames.length === 1 && index.columnNames[0] === column.name); if (columnIndex) { upQueries.push(this.createIndexSql(table, columnIndex)); downQueries.push(this.dropIndexSql(table, columnIndex)); } else if (column.isUnique) { const uniqueIndex = new TableIndex_1.TableIndex({ name: this.dataSource.namingStrategy.indexName(table, [ column.name, ]), columnNames: [column.name], isUnique: true, }); clonedTable.indices.push(uniqueIndex); clonedTable.uniques.push(new TableUnique_1.TableUnique({ name: uniqueIndex.name, columnNames: uniqueIndex.columnNames, })); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD UNIQUE INDEX \`${uniqueIndex.name}\` (\`${column.name}\`)`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP INDEX \`${uniqueIndex.name}\``)); } 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 "${table.name}" 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 oldColumnOrName * @param newColumn */ async changeColumn(tableOrName, oldColumnOrName, 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(oldColumnOrName) ? oldColumnOrName : table.columns.find((column) => column.name === oldColumnOrName); if (!oldColumn) throw new error_1.TypeORMError(`Column "${oldColumnOrName}" was not found in the "${table.name}" table.`); if ((newColumn.isGenerated !== oldColumn.isGenerated && newColumn.generationStrategy !== "uuid") || oldColumn.type !== newColumn.type || oldColumn.length !== newColumn.length || oldColumn.generatedType !== newColumn.generatedType) { await this.dropColumn(table, oldColumn); await this.addColumn(table, newColumn); // update cloned table clonedTable = table.clone(); } else { if (newColumn.name !== oldColumn.name) { // We don't change any column properties, just rename it. upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${oldColumn.name}\` \`${newColumn.name}\` ${this.buildCreateColumnSql(oldColumn, true, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${newColumn.name}\` \`${oldColumn.name}\` ${this.buildCreateColumnSql(oldColumn, true, true)}`)); // rename index constraints clonedTable.findColumnIndices(oldColumn).forEach((index) => { // build new constraint name index.columnNames.splice(index.columnNames.indexOf(oldColumn.name), 1); index.columnNames.push(newColumn.name); const columnNames = index.columnNames .map((column) => `\`${column}\``) .join(", "); const newIndexName = this.dataSource.namingStrategy.indexName(clonedTable, index.columnNames, index.where); // build queries let indexType = ""; if (index.isUnique) indexType += "UNIQUE "; if (index.isSpatial) indexType += "SPATIAL "; if (index.isFulltext) indexType += "FULLTEXT "; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP INDEX \`${index.name}\`, ADD ${indexType}INDEX \`${newIndexName}\` (${columnNames})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP INDEX \`${newIndexName}\`, ADD ${indexType}INDEX \`${index.name}\` (${columnNames})`)); // replace constraint name index.name = newIndexName; }); // rename foreign key constraints clonedTable .findColumnForeignKeys(oldColumn) .forEach((foreignKey) => { // build new constraint name foreignKey.columnNames.splice(foreignKey.columnNames.indexOf(oldColumn.name), 1); foreignKey.columnNames.push(newColumn.name); const columnNames = foreignKey.columnNames .map((column) => `\`${column}\``) .join(", "); const referencedColumnNames = foreignKey.referencedColumnNames .map((column) => `\`${column}\``) .join(","); const newForeignKeyName = this.dataSource.namingStrategy.foreignKeyName(clonedTable, foreignKey.columnNames); // build queries let up = `ALTER TABLE ${this.escapePath(table)} DROP FOREIGN KEY \`${foreignKey.name}\`, ADD CONSTRAINT \`${newForeignKeyName}\` FOREIGN KEY (${columnNames}) ` + `REFERENCES ${this.escapePath(this.getTablePath(foreignKey))}(${referencedColumnNames})`; if (foreignKey.onDelete) up += ` ON DELETE ${foreignKey.onDelete}`; if (foreignKey.onUpdate) up += ` ON UPDATE ${foreignKey.onUpdate}`; let down = `ALTER TABLE ${this.escapePath(table)} DROP FOREIGN KEY \`${newForeignKeyName}\`, ADD CONSTRAINT \`${foreignKey.name}\` FOREIGN KEY (${columnNames}) ` + `REFERENCES ${this.escapePath(this.getTablePath(foreignKey))}(${referencedColumnNames})`; if (foreignKey.onDelete) down += ` ON DELETE ${foreignKey.onDelete}`; if (foreignKey.onUpdate) down += ` ON UPDATE ${foreignKey.onUpdate}`; upQueries.push(new Query_1.Query(up)); downQueries.push(new Query_1.Query(down)); // 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)) { upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${oldColumn.name}\` ${this.buildCreateColumnSql(newColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${newColumn.name}\` ${this.buildCreateColumnSql(oldColumn, true)}`)); } if (newColumn.isPrimary !== oldColumn.isPrimary) { // if table have generated column, we must drop AUTO_INCREMENT before changing primary constraints. const generatedColumn = clonedTable.columns.find((column) => column.isGenerated && column.generationStrategy === "increment"); if (generatedColumn) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${generatedColumn.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(generatedColumn, true)}`)); } const primaryColumns = clonedTable.primaryColumns; // if primary column state changed, we must always drop existed constraint. if (primaryColumns.length > 0) { const columnNames = primaryColumns .map((column) => `\`${column.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD 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 columnNames = primaryColumns .map((column) => `\`${column.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD PRIMARY KEY (${columnNames})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); } 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 columnNames = primaryColumns .map((column) => `\`${column.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD PRIMARY KEY (${columnNames})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); } } // if we have generated column, and we dropped AUTO_INCREMENT property before, we must bring it back if (generatedColumn) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(generatedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${generatedColumn.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); } } if (newColumn.isUnique !== oldColumn.isUnique) { if (newColumn.isUnique === true) { const uniqueIndex = new TableIndex_1.TableIndex({ name: this.dataSource.namingStrategy.indexName(table, [ newColumn.name, ]), columnNames: [newColumn.name], isUnique: true, }); clonedTable.indices.push(uniqueIndex); clonedTable.uniques.push(new TableUnique_1.TableUnique({ name: uniqueIndex.name, columnNames: uniqueIndex.columnNames, })); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD UNIQUE INDEX \`${uniqueIndex.name}\` (\`${newColumn.name}\`)`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP INDEX \`${uniqueIndex.name}\``)); } else { const uniqueIndex = clonedTable.indices.find((index) => { return (index.columnNames.length === 1 && index.isUnique === true && !!index.columnNames.find((columnName) => columnName === newColumn.name)); }); clonedTable.indices.splice(clonedTable.indices.indexOf(uniqueIndex), 1); const tableUnique = clonedTable.uniques.find((unique) => unique.name === uniqueIndex.name); clonedTable.uniques.splice(clonedTable.uniques.indexOf(tableUnique), 1); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP INDEX \`${uniqueIndex.name}\``)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD UNIQUE INDEX \`${uniqueIndex.name}\` (\`${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 "${table.name}"`); } const clonedTable = table.clone(); const upQueries = []; const downQueries = []; // drop primary key constraint if (column.isPrimary) { // if table have generated column, we must drop AUTO_INCREMENT before changing primary constraints. const generatedColumn = clonedTable.columns.find((column) => column.isGenerated && column.generationStrategy === "increment"); if (generatedColumn) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${generatedColumn.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(generatedColumn, true)}`)); } // dropping primary key constraint const columnNames = clonedTable.primaryColumns .map((primaryColumn) => `\`${primaryColumn.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} DROP PRIMARY KEY`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} ADD 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 columnNames = clonedTable.primaryColumns .map((primaryColumn) => `\`${primaryColumn.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} ADD PRIMARY KEY (${columnNames})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)} DROP PRIMARY KEY`)); } // if we have generated column, and we dropped AUTO_INCREMENT property before, and this column is not current dropping column, we must bring it back if (generatedColumn && generatedColumn.name !== column.name) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(generatedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${generatedColumn.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); } } // drop 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.dropIndexSql(table, columnIndex)); downQueries.push(this.createIndexSql(table, columnIndex)); } else if (column.isUnique) { // we splice constraints both from table uniques and indices. const uniqueName = this.dataSource.namingStrategy.uniqueConstraintName(table, [ column.name, ]); const foundUnique = clonedTable.uniques.find((unique) => unique.name === uniqueName); if (foundUnique) clonedTable.uniques.splice(clonedTable.uniques.indexOf(foundUnique), 1); const indexName = this.dataSource.namingStrategy.indexName(table, [ column.name, ]); const foundIndex = clonedTable.indices.find((index) => index.name === indexName); if (foundIndex) clonedTable.indices.splice(clonedTable.indices.indexOf(foundIndex), 1); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP INDEX \`${indexName}\``)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD UNIQUE INDEX \`${indexName}\` (\`${column.name}\`)`)); } 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, true)}`)); 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 */ async createPrimaryKey(tableOrName, columnNames) { const table = InstanceChecker_1.InstanceChecker.isTable(tableOrName) ? tableOrName : await this.getCachedTable(tableOrName); const clonedTable = table.clone(); const up = this.createPrimaryKeySql(table, columnNames); const down = this.dropPrimaryKeySql(table); await this.executeQueries(up, down); clonedTable.columns.forEach((column) => { if (columnNames.find((columnName) => columnName === column.name)) column.isPrimary = true; }); 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 clonedTable = table.clone(); const columnNames = columns.map((column) => column.name); const upQueries = []; const downQueries = []; // if table have generated column, we must drop AUTO_INCREMENT before changing primary constraints. const generatedColumn = clonedTable.columns.find((column) => column.isGenerated && column.generationStrategy === "increment"); if (generatedColumn) { const nonGeneratedColumn = generatedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${generatedColumn.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(generatedColumn, true)}`)); } // if table already have primary columns, we must drop them. const primaryColumns = clonedTable.primaryColumns; if (primaryColumns.length > 0) { const columnNames = primaryColumns .map((column) => `\`${column.name}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD PRIMARY KEY (${columnNames})`)); } // update columns in table. clonedTable.columns .filter((column) => columnNames.indexOf(column.name) !== -1) .forEach((column) => { column.isPrimary = true; }); const columnNamesString = columnNames .map((columnName) => `\`${columnName}\``) .join(", "); upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} ADD PRIMARY KEY (${columnNamesString})`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} DROP PRIMARY KEY`)); // if we already have generated column or column is changed to generated, and we dropped AUTO_INCREMENT property before, we must bring it back const newOrExistGeneratedColumn = generatedColumn ?? columns.find((column) => column.isGenerated && column.generationStrategy === "increment"); if (newOrExistGeneratedColumn) { const nonGeneratedColumn = newOrExistGeneratedColumn.clone(); nonGeneratedColumn.isGenerated = false; nonGeneratedColumn.generationStrategy = undefined; upQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${nonGeneratedColumn.name}\` ${this.buildCreateColumnSql(newOrExistGeneratedColumn, true)}`)); downQueries.push(new Query_1.Query(`ALTER TABLE ${this.escapePath(table)} CHANGE \`${newOrExistGeneratedColumn.name}\` ${this.buildCreateColumnSql(nonGeneratedColumn, true)}`)); // if column changed to generated, we must update it in table const changedGeneratedColumn = clonedTable.columns.find((column) => column.name === newOrExistGeneratedColumn.name); changedGeneratedColumn.isGenerated = true; changedGeneratedColumn.generationStrategy = "increment"; } 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 Promise.resolve(); const up = this.dropPrimaryKeySql(table); const down = this.createPrimaryKeySql(table, table.primaryColumns.map((column) => column.name)); 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) { throw new error_1.TypeORMError(`MySql does not support unique constraints. Use unique index instead.`); } /** * Creates a new unique constraints. * * @param tableOrName * @param uniqueConstraints */ async createUniqueConstraints(tableOrName, uniqueConstraints) { throw new error_1.TypeORMError(`MySql does not support unique constraints. Use unique index instead.`); } /** * Drops a unique constraint. * * @param tableOrName * @param uniqueOrName * @param ifExists */ async dropUniqueConstraint(tableOrName, uniqueOrName, ifExists) { throw new error_1.TypeORMError(`MySql does not support unique constraints. Use unique index instead.`); } /** * Drops a unique constraints. * * @param tableOrName * @param uniqueConstraints * @param ifExists */ async dropUniqueConstraints(tableOrName, uniqueConstraints, ifExists) { throw new error_1.TypeORMErro