'use strict'; const SOURCE_TABLE = 'wms_express_interface'; const SOURCE_COLUMN = 'platform_code'; const OLD_TARGET_TABLE = 'wms_platform'; const OLD_TARGET_COLUMN = 'code'; const NEW_TARGET_TABLE = 'wms_print_platform'; const NEW_TARGET_COLUMN = 'platform_code'; const NEW_CONSTRAINT_NAME = 'fk_wms_express_interface_print_platform_code'; /** * 获取数据库中的真实表名集合。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise>} 表名集合。 */ const getExistingTableSet = async queryInterface => { const tables = await queryInterface.showAllTables(); return new Set( tables.map(table => (typeof table === 'string' ? table : table.tableName)) ); }; /** * 安全读取指定表字段定义。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {Set} existingTableSet 已存在表名集合。 * @param {string} tableName 表名。 * @return {Promise} 字段定义;表不存在时返回 null。 */ const getTableColumns = async (queryInterface, existingTableSet, tableName) => { if (!existingTableSet.has(tableName)) { return null; } return queryInterface.describeTable(tableName); }; /** * 查询指定列上的外键信息。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 源表名。 * @param {string} columnName 源列名。 * @return {Promise>} 外键信息列表。 */ const getColumnForeignKeys = async (queryInterface, tableName, columnName) => { const [ rows ] = await queryInterface.sequelize.query( ` SELECT kcu.CONSTRAINT_NAME AS constraintName, kcu.TABLE_NAME AS tableName, kcu.COLUMN_NAME AS columnName, kcu.REFERENCED_TABLE_NAME AS referencedTableName, kcu.REFERENCED_COLUMN_NAME AS referencedColumnName, rc.UPDATE_RULE AS updateRule, rc.DELETE_RULE AS deleteRule FROM information_schema.KEY_COLUMN_USAGE kcu INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS rc ON rc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA AND rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME AND rc.TABLE_NAME = kcu.TABLE_NAME WHERE kcu.CONSTRAINT_SCHEMA = DATABASE() AND kcu.TABLE_NAME = :tableName AND kcu.COLUMN_NAME = :columnName AND kcu.REFERENCED_TABLE_NAME IS NOT NULL `, { replacements: { tableName, columnName, }, } ); return rows; }; /** * 删除指定列上指向目标表目标列的所有外键。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} sourceTable 源表名。 * @param {string} sourceColumn 源列名。 * @param {string} targetTable 目标表名。 * @param {string} targetColumn 目标列名。 * @return {Promise} 无返回值。 */ const dropForeignKeysByReference = async ( queryInterface, sourceTable, sourceColumn, targetTable, targetColumn ) => { const foreignKeys = await getColumnForeignKeys(queryInterface, sourceTable, sourceColumn); for (const foreignKey of foreignKeys) { if ( foreignKey.referencedTableName === targetTable && foreignKey.referencedColumnName === targetColumn ) { await queryInterface.removeConstraint(sourceTable, foreignKey.constraintName); } } }; /** * 判断指定引用关系外键是否已存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} sourceTable 源表名。 * @param {string} sourceColumn 源列名。 * @param {string} targetTable 目标表名。 * @param {string} targetColumn 目标列名。 * @return {Promise} 是否已存在。 */ const hasForeignKeyReference = async ( queryInterface, sourceTable, sourceColumn, targetTable, targetColumn ) => { const foreignKeys = await getColumnForeignKeys(queryInterface, sourceTable, sourceColumn); return foreignKeys.some(foreignKey => ( foreignKey.referencedTableName === targetTable && foreignKey.referencedColumnName === targetColumn )); }; /** * 为指定列添加目标外键。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} sourceTable 源表名。 * @param {string} sourceColumn 源列名。 * @param {string} targetTable 目标表名。 * @param {string} targetColumn 目标列名。 * @param {string} constraintName 外键名称。 * @return {Promise} 无返回值。 */ const addForeignKey = async ( queryInterface, sourceTable, sourceColumn, targetTable, targetColumn, constraintName ) => { await queryInterface.addConstraint(sourceTable, { fields: [ sourceColumn ], type: 'foreign key', name: constraintName, references: { table: targetTable, field: targetColumn, }, onUpdate: 'RESTRICT', onDelete: 'RESTRICT', }); }; module.exports = { /** * 将快递接口平台外键从平台表切换到打印平台表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async up(queryInterface) { const existingTableSet = await getExistingTableSet(queryInterface); const sourceColumns = await getTableColumns(queryInterface, existingTableSet, SOURCE_TABLE); const targetColumns = await getTableColumns(queryInterface, existingTableSet, NEW_TARGET_TABLE); if (!sourceColumns || !sourceColumns[SOURCE_COLUMN] || !targetColumns || !targetColumns[NEW_TARGET_COLUMN]) { return; } await dropForeignKeysByReference( queryInterface, SOURCE_TABLE, SOURCE_COLUMN, OLD_TARGET_TABLE, OLD_TARGET_COLUMN ); const hasNewForeignKey = await hasForeignKeyReference( queryInterface, SOURCE_TABLE, SOURCE_COLUMN, NEW_TARGET_TABLE, NEW_TARGET_COLUMN ); if (!hasNewForeignKey) { await addForeignKey( queryInterface, SOURCE_TABLE, SOURCE_COLUMN, NEW_TARGET_TABLE, NEW_TARGET_COLUMN, NEW_CONSTRAINT_NAME ); } }, /** * 回滚快递接口平台外键到原平台表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { const existingTableSet = await getExistingTableSet(queryInterface); const sourceColumns = await getTableColumns(queryInterface, existingTableSet, SOURCE_TABLE); const oldTargetColumns = await getTableColumns(queryInterface, existingTableSet, OLD_TARGET_TABLE); if (!sourceColumns || !sourceColumns[SOURCE_COLUMN]) { return; } await dropForeignKeysByReference( queryInterface, SOURCE_TABLE, SOURCE_COLUMN, NEW_TARGET_TABLE, NEW_TARGET_COLUMN ); if (!oldTargetColumns || !oldTargetColumns[OLD_TARGET_COLUMN]) { return; } const hasOldForeignKey = await hasForeignKeyReference( queryInterface, SOURCE_TABLE, SOURCE_COLUMN, OLD_TARGET_TABLE, OLD_TARGET_COLUMN ); if (!hasOldForeignKey) { await addForeignKey( queryInterface, SOURCE_TABLE, SOURCE_COLUMN, OLD_TARGET_TABLE, OLD_TARGET_COLUMN, 'fk_wms_express_interface_platform_code' ); } }, };