fix: 增加ERP唯一键升级保护
This commit is contained in:
@@ -1,5 +1,45 @@
|
||||
-- WMS ERP 接口数据库幂等升级脚本。
|
||||
|
||||
-- 在任何唯一索引变更前检查历史重复数据,发现冲突立即终止且不清洗数据。
|
||||
DROP PROCEDURE IF EXISTS wms_erp_assert_unique_business_keys_20260903;
|
||||
DELIMITER $$
|
||||
CREATE PROCEDURE wms_erp_assert_unique_business_keys_20260903()
|
||||
BEGIN
|
||||
SET @wms_erp_duplicate_count = 0;
|
||||
SET @wms_erp_check_sql = IF(
|
||||
EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_iostorinv' AND column_name = 'erp_business_type')
|
||||
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_iostorinv' AND column_name = 'erp_bill_no'),
|
||||
'SELECT COUNT(*) INTO @wms_erp_duplicate_count FROM (SELECT 1 FROM wms_iostorinv WHERE erp_business_type IS NOT NULL AND erp_bill_no IS NOT NULL GROUP BY erp_business_type, erp_bill_no HAVING COUNT(*) > 1) duplicate_rows',
|
||||
'SET @wms_erp_duplicate_count = 0');
|
||||
PREPARE wms_erp_check_stmt FROM @wms_erp_check_sql;
|
||||
EXECUTE wms_erp_check_stmt;
|
||||
DEALLOCATE PREPARE wms_erp_check_stmt;
|
||||
IF @wms_erp_duplicate_count > 0 THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '出入库单存在重复ERP业务类型和单据号,无法增加唯一索引';
|
||||
END IF;
|
||||
|
||||
SET @wms_erp_check_sql = IF(
|
||||
EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_group_plate' AND column_name = 'ext_type')
|
||||
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_group_plate' AND column_name = 'ext_code')
|
||||
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_group_plate' AND column_name = 'ext_dtl_code'),
|
||||
'SELECT COUNT(*) INTO @wms_erp_duplicate_count FROM (SELECT 1 FROM wms_group_plate WHERE ext_type IS NOT NULL AND ext_code IS NOT NULL AND ext_dtl_code IS NOT NULL GROUP BY ext_type, ext_code, ext_dtl_code HAVING COUNT(*) > 1) duplicate_rows',
|
||||
'SET @wms_erp_duplicate_count = 0');
|
||||
PREPARE wms_erp_check_stmt FROM @wms_erp_check_sql;
|
||||
EXECUTE wms_erp_check_stmt;
|
||||
DEALLOCATE PREPARE wms_erp_check_stmt;
|
||||
IF @wms_erp_duplicate_count > 0 THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '组盘记录存在重复ERP来源明细,无法增加唯一索引';
|
||||
END IF;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
CALL wms_erp_assert_unique_business_keys_20260903();
|
||||
DROP PROCEDURE wms_erp_assert_unique_business_keys_20260903;
|
||||
|
||||
-- 出入库单增加 ERP 业务类型。
|
||||
SET @wms_erp_ddl = IF(
|
||||
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
|
||||
@@ -48,7 +88,7 @@ SET @wms_erp_ddl = IF(
|
||||
FROM information_schema.statistics WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_business_bill'), '') = 'erp_business_type,erp_bill_no'
|
||||
),
|
||||
'ALTER TABLE wms_iostorinv DROP INDEX uk_erp_business_bill',
|
||||
'ALTER TABLE wms_iostorinv DROP INDEX uk_erp_business_bill, ADD UNIQUE INDEX uk_erp_business_bill (erp_business_type, erp_bill_no)',
|
||||
'SELECT 1'));
|
||||
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
|
||||
EXECUTE wms_erp_ddl_stmt;
|
||||
@@ -84,7 +124,7 @@ SET @wms_erp_ddl = IF(
|
||||
FROM information_schema.statistics WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_group_plate' AND index_name = 'uk_ext_source_detail'), '') = 'ext_type,ext_code,ext_dtl_code'
|
||||
),
|
||||
'ALTER TABLE wms_group_plate DROP INDEX uk_ext_source_detail',
|
||||
'ALTER TABLE wms_group_plate DROP INDEX uk_ext_source_detail, ADD UNIQUE INDEX uk_ext_source_detail (ext_type, ext_code, ext_dtl_code)',
|
||||
'SELECT 1'));
|
||||
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
|
||||
EXECUTE wms_erp_ddl_stmt;
|
||||
@@ -120,7 +160,7 @@ SET @wms_erp_ddl = IF(
|
||||
FROM information_schema.statistics WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wms_group_plate' AND index_name = 'idx_vehicle_code_pcsn'), '') = 'vehicle_code,pcsn'
|
||||
),
|
||||
'ALTER TABLE wms_group_plate DROP INDEX idx_vehicle_code_pcsn',
|
||||
'ALTER TABLE wms_group_plate DROP INDEX idx_vehicle_code_pcsn, ADD INDEX idx_vehicle_code_pcsn (vehicle_code, pcsn)',
|
||||
'SELECT 1'));
|
||||
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
|
||||
EXECUTE wms_erp_ddl_stmt;
|
||||
@@ -143,9 +183,11 @@ EXECUTE wms_erp_ddl_stmt;
|
||||
DEALLOCATE PREPARE wms_erp_ddl_stmt;
|
||||
|
||||
-- 与 LMS 共用全局 ERP 配置,已有有效配置时不重复写入。
|
||||
SELECT GET_LOCK('nl_erp_interface_config', 60) INTO @erp_config_lock_acquired;
|
||||
SET @wms_erp_ddl = IF(
|
||||
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
|
||||
AND table_name = 'infra_config'),
|
||||
@erp_config_lock_acquired = 1
|
||||
AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
|
||||
AND table_name = 'infra_config'),
|
||||
'INSERT INTO infra_config (category, type, name, config_key, value, visible, remark, creator, updater, deleted)
|
||||
SELECT ''lms'', 2, ''ERP接口鉴权'', ''lms.erp.authorization'', ''user4'', b''0'', ''ERP调用LMS和WMS的鉴权值'', ''1'', ''1'', b''0''
|
||||
WHERE NOT EXISTS (SELECT 1 FROM infra_config WHERE config_key = ''lms.erp.authorization'' AND deleted = b''0'')',
|
||||
@@ -155,8 +197,9 @@ EXECUTE wms_erp_ddl_stmt;
|
||||
DEALLOCATE PREPARE wms_erp_ddl_stmt;
|
||||
|
||||
SET @wms_erp_ddl = IF(
|
||||
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
|
||||
AND table_name = 'infra_config'),
|
||||
@erp_config_lock_acquired = 1
|
||||
AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
|
||||
AND table_name = 'infra_config'),
|
||||
'INSERT INTO infra_config (category, type, name, config_key, value, visible, remark, creator, updater, deleted)
|
||||
SELECT ''lms'', 2, ''ERP接口基础地址'', ''lms.erp.base-url'', '''', b''0'', ''LMS调用ERP的基础地址'', ''1'', ''1'', b''0''
|
||||
WHERE NOT EXISTS (SELECT 1 FROM infra_config WHERE config_key = ''lms.erp.base-url'' AND deleted = b''0'')',
|
||||
@@ -164,3 +207,7 @@ SET @wms_erp_ddl = IF(
|
||||
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
|
||||
EXECUTE wms_erp_ddl_stmt;
|
||||
DEALLOCATE PREPARE wms_erp_ddl_stmt;
|
||||
SET @erp_config_lock_released = IF(
|
||||
@erp_config_lock_acquired = 1,
|
||||
RELEASE_LOCK('nl_erp_interface_config'),
|
||||
0);
|
||||
|
||||
Reference in New Issue
Block a user