Update upwards migration to be idempotent

This commit is contained in:
advplyr 2025-02-08 12:37:34 -06:00
parent 9a261195b7
commit ef45f844e5

View File

@ -105,8 +105,13 @@ async function removeIndex(queryInterface, logger, tableName, columns) {
async function addColumn(queryInterface, logger, table, column, options) { async function addColumn(queryInterface, logger, table, column, options) {
logger.info(`${loggerPrefix} adding column "${column}" to table "${table}"`) logger.info(`${loggerPrefix} adding column "${column}" to table "${table}"`)
await queryInterface.addColumn(table, column, options) const tableDescription = await queryInterface.describeTable(table)
logger.info(`${loggerPrefix} added column "${column}" to table "${table}"`) if (!tableDescription[column]) {
await queryInterface.addColumn(table, column, options)
logger.info(`${loggerPrefix} added column "${column}" to table "${table}"`)
} else {
logger.info(`${loggerPrefix} column "${column}" already exists in table "${table}"`)
}
} }
async function removeColumn(queryInterface, logger, table, column) { async function removeColumn(queryInterface, logger, table, column) {
@ -129,6 +134,9 @@ async function copyColumn(queryInterface, logger, sourceTable, sourceColumn, sou
async function addTrigger(queryInterface, logger, sourceTable, sourceColumn, sourceIdColumn, targetTable, targetColumn, targetIdColumn) { async function addTrigger(queryInterface, logger, sourceTable, sourceColumn, sourceIdColumn, targetTable, targetColumn, targetIdColumn) {
logger.info(`${loggerPrefix} adding trigger to update ${targetTable}.${targetColumn} when ${sourceTable}.${sourceColumn} is updated`) logger.info(`${loggerPrefix} adding trigger to update ${targetTable}.${targetColumn} when ${sourceTable}.${sourceColumn} is updated`)
const triggerName = convertToSnakeCase(`update_${targetTable}_${targetColumn}`) const triggerName = convertToSnakeCase(`update_${targetTable}_${targetColumn}`)
await queryInterface.sequelize.query(`DROP TRIGGER IF EXISTS ${triggerName}`)
await queryInterface.sequelize.query(` await queryInterface.sequelize.query(`
CREATE TRIGGER ${triggerName} CREATE TRIGGER ${triggerName}
AFTER UPDATE OF ${sourceColumn} ON ${sourceTable} AFTER UPDATE OF ${sourceColumn} ON ${sourceTable}