116 lines
3.4 KiB
JavaScript
116 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
const TABLE = 'wms_role_permission';
|
|
const REF_TABLE = 'wms_permission';
|
|
const FK_NAME = 'fk_wms_role_permission_permission';
|
|
|
|
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 getOrphanCount(queryInterface) {
|
|
const [ rows ] = await queryInterface.sequelize.query(
|
|
`
|
|
SELECT COUNT(*) AS cnt
|
|
FROM wms_role_permission rp
|
|
LEFT JOIN wms_permission p ON p.id = rp.permission_id
|
|
WHERE p.id IS NULL
|
|
`
|
|
);
|
|
|
|
return Number(rows[0]?.cnt || 0);
|
|
}
|
|
|
|
module.exports = {
|
|
async up(queryInterface) {
|
|
const permissionIdColumn = await getColumnInfo(queryInterface, TABLE, 'permission_id');
|
|
if (!permissionIdColumn) {
|
|
throw new Error('Column wms_role_permission.permission_id not found.');
|
|
}
|
|
|
|
const columnType = String(permissionIdColumn.COLUMN_TYPE || '').toLowerCase();
|
|
|
|
// permission.id is int(11), keep child key type aligned before adding FK.
|
|
if (columnType !== 'int(11)') {
|
|
await queryInterface.sequelize.query(
|
|
'ALTER TABLE `wms_role_permission` MODIFY COLUMN `permission_id` int(11) NOT 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_permission.permission_id. ` +
|
|
'Please clean orphan rows first, then rerun migration.'
|
|
);
|
|
}
|
|
|
|
const exists = await hasConstraint(queryInterface, TABLE, FK_NAME);
|
|
if (!exists) {
|
|
await queryInterface.sequelize.query(
|
|
'ALTER TABLE `wms_role_permission` ' +
|
|
'ADD CONSTRAINT `fk_wms_role_permission_permission` ' +
|
|
'FOREIGN KEY (`permission_id`) REFERENCES `wms_permission` (`id`) ' +
|
|
'ON UPDATE RESTRICT ON DELETE CASCADE'
|
|
);
|
|
}
|
|
},
|
|
|
|
async down(queryInterface) {
|
|
const exists = await hasConstraint(queryInterface, TABLE, FK_NAME);
|
|
if (exists) {
|
|
await queryInterface.sequelize.query(
|
|
'ALTER TABLE `wms_role_permission` DROP FOREIGN KEY `fk_wms_role_permission_permission`'
|
|
);
|
|
}
|
|
|
|
const permissionIdColumn = await getColumnInfo(queryInterface, TABLE, 'permission_id');
|
|
if (permissionIdColumn) {
|
|
const columnType = String(permissionIdColumn.COLUMN_TYPE || '').toLowerCase();
|
|
if (columnType !== 'bigint(20) unsigned') {
|
|
await queryInterface.sequelize.query(
|
|
'ALTER TABLE `wms_role_permission` MODIFY COLUMN `permission_id` bigint(20) unsigned NOT NULL COMMENT \'权限ID\''
|
|
);
|
|
}
|
|
}
|
|
},
|
|
};
|