-- 1.0.353：数据库层阻断无效租户再次污染可重建的外协交付 DWD。
CREATE TABLE IF NOT EXISTS `dwd_process_outsource_delivery_bak_1_0_353` LIKE `dwd_process_outsource_delivery`;
INSERT IGNORE INTO `dwd_process_outsource_delivery_bak_1_0_353`
SELECT * FROM `dwd_process_outsource_delivery` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `dwd_process_outsource_delivery` WHERE `tenant_id` IN (0,1,1300000000001);

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
   WHERE CONSTRAINT_SCHEMA=DATABASE()
     AND TABLE_NAME='dwd_process_outsource_delivery'
     AND CONSTRAINT_NAME='ck_dwd_process_outsource_valid_tenant')=0,
  'ALTER TABLE `dwd_process_outsource_delivery` ADD CONSTRAINT `ck_dwd_process_outsource_valid_tenant` CHECK (`tenant_id` NOT IN (0,1,1300000000001))',
  'DO 0'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
