-- 订单异常事件表:补齐 Handler* / ConcurrencyStamp(幂等;列已存在则自动跳过) -- 修复:Unknown column 'HandlerNames' / 'ConcurrencyStamp' in 'field list' -- MySQL 5.7+ / 8.0 SET @db := DATABASE(); -- HandlerNames SET @col := ( SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'HandlerNames' ); SET @sql := IF( @col = 0, 'ALTER TABLE `t_order_exception_event` ADD COLUMN `HandlerNames` VARCHAR(256) NULL COMMENT ''处理人快照''', 'SELECT ''skip HandlerNames'' AS msg' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- HandlerType SET @col := ( SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'HandlerType' ); SET @sql := IF( @col = 0, 'ALTER TABLE `t_order_exception_event` ADD COLUMN `HandlerType` VARCHAR(16) NULL COMMENT ''处理人类型''', 'SELECT ''skip HandlerType'' AS msg' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- HandlerUserId SET @col := ( SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'HandlerUserId' ); SET @sql := IF( @col = 0, 'ALTER TABLE `t_order_exception_event` ADD COLUMN `HandlerUserId` INT NULL COMMENT ''处理人用户Id(customer 时为订单客户账号)''', 'SELECT ''skip HandlerUserId'' AS msg' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- ConcurrencyStamp(AggregateRoot 并发戳;避免 DEFAULT(UUID()) 触发 1674 复制不安全) SET @col := ( SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'ConcurrencyStamp' ); SET @sql := IF( @col = 0, 'ALTER TABLE `t_order_exception_event` ADD COLUMN `ConcurrencyStamp` CHAR(36) NULL COMMENT ''并发戳''', 'SELECT ''skip ConcurrencyStamp add'' AS msg' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @col := ( SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'ConcurrencyStamp' ); SET @sql := IF( @col > 0, 'UPDATE `t_order_exception_event` SET `ConcurrencyStamp` = LOWER(CONCAT( LPAD(HEX(`Id`), 8, ''0''), ''-0000-4000-8000-'', LPAD(HEX(`Id` * 9973), 12, ''0'') )) WHERE `ConcurrencyStamp` IS NULL OR `ConcurrencyStamp` = ''''', 'SELECT ''skip ConcurrencyStamp backfill'' AS msg' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @col := ( SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'ConcurrencyStamp' ); SET @isNullable := ( SELECT IFNULL(MAX(CASE WHEN IS_NULLABLE = 'YES' THEN 1 ELSE 0 END), 0) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_exception_event' AND COLUMN_NAME = 'ConcurrencyStamp' ); SET @sql := IF( @col > 0 AND @isNullable = 1, 'ALTER TABLE `t_order_exception_event` MODIFY COLUMN `ConcurrencyStamp` CHAR(36) NOT NULL COMMENT ''并发戳''', 'SELECT ''skip ConcurrencyStamp not null'' AS msg' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;