'use strict'; const TABLE = 'wms_role'; const FK_NAME = 'fk_wms_role_store'; const INDEX_NAME = 'idx_store_id'; async function getColumnInfo(queryInterface, tableName, columnName) { const [ rows ] = await queryInterface.sequelize.query( ` SELECT COLUMN_TYPE, IS_NULLABLE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :tableName AND COLUMN_NAME = :columnName LIMIT 1 `, { replacements: { tableName, columnName, }, } ); return rows[0] || null; } async function hasConstraint(queryInterface, tableName, constraintName) { const [ rows ] = await queryInterface.sequelize.query( ` SELECT 1 FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = :tableName AND CONSTRAINT_NAME = :constraintName LIMIT 1 `, { replacements: { tableName, constraintName, }, } ); return rows.length > 0; } async function hasIndex(queryInterface, tableName, indexName) { const [ rows ] = await queryInterface.sequelize.query( ` SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :tableName AND INDEX_NAME = :indexName LIMIT 1 `, { replacements: { tableName, indexName, }, } ); return rows.length > 0; } async function getOrphanCount(queryInterface) { const [ rows ] = await queryInterface.sequelize.query( ` SELECT COUNT(*) AS cnt FROM wms_role r LEFT JOIN wms_store s ON s.id = r.store_id WHERE r.store_id IS NOT NULL AND s.id IS NULL ` ); return Number(rows[0]?.cnt || 0); } module.exports = { async up(queryInterface) { const storeIdColumn = await getColumnInfo(queryInterface, TABLE, 'store_id'); if (!storeIdColumn) { throw new Error('Column wms_role.store_id not found.'); } const columnType = String(storeIdColumn.COLUMN_TYPE || '').toLowerCase(); if (columnType !== 'bigint(20) unsigned') { await queryInterface.sequelize.query( 'ALTER TABLE `wms_role` MODIFY COLUMN `store_id` bigint(20) unsigned NULL COMMENT \'所属仓库ID\'' ); } const orphanCount = await getOrphanCount(queryInterface); if (orphanCount > 0) { throw new Error( `Cannot add ${FK_NAME}: found ${orphanCount} orphan rows in wms_role.store_id. ` + 'Please clean orphan rows first, then rerun migration.' ); } const indexExists = await hasIndex(queryInterface, TABLE, INDEX_NAME); if (!indexExists) { await queryInterface.sequelize.query( 'ALTER TABLE `wms_role` ADD INDEX `idx_store_id` (`store_id`)' ); } const fkExists = await hasConstraint(queryInterface, TABLE, FK_NAME); if (!fkExists) { await queryInterface.sequelize.query( 'ALTER TABLE `wms_role` ' + 'ADD CONSTRAINT `fk_wms_role_store` ' + 'FOREIGN KEY (`store_id`) REFERENCES `wms_store` (`id`) ' + 'ON UPDATE RESTRICT ON DELETE SET NULL' ); } }, async down(queryInterface) { const fkExists = await hasConstraint(queryInterface, TABLE, FK_NAME); if (fkExists) { await queryInterface.sequelize.query( 'ALTER TABLE `wms_role` DROP FOREIGN KEY `fk_wms_role_store`' ); } const indexExists = await hasIndex(queryInterface, TABLE, INDEX_NAME); if (indexExists) { await queryInterface.sequelize.query( 'ALTER TABLE `wms_role` DROP INDEX `idx_store_id`' ); } const storeIdColumn = await getColumnInfo(queryInterface, TABLE, 'store_id'); if (storeIdColumn) { const columnType = String(storeIdColumn.COLUMN_TYPE || '').toLowerCase(); if (columnType !== 'bigint(20)') { await queryInterface.sequelize.query( 'ALTER TABLE `wms_role` MODIFY COLUMN `store_id` bigint(20) NULL COMMENT \'所属仓库ID\'' ); } } }, };