diff --git a/sql/migrate_agent_module_20261029.sql b/sql/migrate_agent_module_20261029.sql index 78a5ba3..ef74284 100644 --- a/sql/migrate_agent_module_20261029.sql +++ b/sql/migrate_agent_module_20261029.sql @@ -1,5 +1,6 @@ -- 渠道代理与返佣(首版轻量)。可重复执行。 -- 含:t_agent、t_agent_commission_detail;t_customer / t_order 扩展 AgentId。 +-- AggregateRoot 需要 ConcurrencyStamp。 SET @db := DATABASE(); @@ -22,11 +23,25 @@ CREATE TABLE IF NOT EXISTS `t_agent` ( `CreateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `UpdateUserId` INT NOT NULL DEFAULT 0, `UpdateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + -- 不用 DEFAULT(UUID()):statement 复制下会报 1674;运行时由 ORM 写入 + `ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '' COMMENT '并发戳', PRIMARY KEY (`Id`), KEY `ix_agent_code` (`AgentCode`), KEY `ix_agent_userid` (`UserId`) ) COMMENT='渠道代理主数据'; +-- 已建表补 ConcurrencyStamp(可重复) +SET @exists := ( + SELECT COUNT(1) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_agent' AND COLUMN_NAME = 'ConcurrencyStamp' +); +SET @sql := IF( + @exists = 0, + 'ALTER TABLE `t_agent` ADD COLUMN `ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '''' COMMENT ''并发戳'' AFTER `UpdateTime`', + 'SELECT ''t_agent.ConcurrencyStamp already exists'' AS info' +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + -- ---------- t_agent_commission_detail ---------- CREATE TABLE IF NOT EXISTS `t_agent_commission_detail` ( `Id` INT NOT NULL AUTO_INCREMENT, @@ -47,12 +62,24 @@ CREATE TABLE IF NOT EXISTS `t_agent_commission_detail` ( `CreateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `UpdateUserId` INT NOT NULL DEFAULT 0, `UpdateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '' COMMENT '并发戳', PRIMARY KEY (`Id`), KEY `ix_agent_comm_agent` (`AgentId`), KEY `ix_agent_comm_order` (`OrderId`), KEY `ix_agent_comm_status` (`Status`) ) COMMENT='代理返佣明细(首版无余额入账)'; +SET @exists := ( + SELECT COUNT(1) FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_agent_commission_detail' AND COLUMN_NAME = 'ConcurrencyStamp' +); +SET @sql := IF( + @exists = 0, + 'ALTER TABLE `t_agent_commission_detail` ADD COLUMN `ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '''' COMMENT ''并发戳'' AFTER `UpdateTime`', + 'SELECT ''t_agent_commission_detail.ConcurrencyStamp already exists'' AS info' +); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + -- ---------- t_customer.AgentId ---------- SET @exists := ( SELECT COUNT(1) FROM information_schema.COLUMNS