杂务:对齐 SQL 与雪花 ID 策略
This commit is contained in:
@@ -104,3 +104,54 @@ CREATE TABLE IF NOT EXISTS `message_source_system_channel` (
|
||||
UNIQUE KEY `uk_message_source_system_channel` (`system_id`, `channel_type`),
|
||||
KEY `idx_message_source_system_channel_id` (`channel_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Per source system default channels';
|
||||
|
||||
-- ========================================
|
||||
-- Snowflake ID alignment
|
||||
-- 1. Keep the original bootstrap create-table flow unchanged
|
||||
-- 2. Drop AUTO_INCREMENT after bootstrap so runtime IDs are fully application-generated
|
||||
-- 3. Keep the same behavior as 2026-03-30_messagehub_snowflake_id_alignment_patch.sql
|
||||
-- ========================================
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE sp_drop_auto_increment_on_id_if_exists(IN p_table_name VARCHAR(64))
|
||||
BEGIN
|
||||
DECLARE v_column_type VARCHAR(128) DEFAULT NULL;
|
||||
DECLARE v_column_comment VARCHAR(255) DEFAULT NULL;
|
||||
DECLARE v_extra VARCHAR(128) DEFAULT NULL;
|
||||
|
||||
SELECT COLUMN_TYPE, COLUMN_COMMENT, EXTRA
|
||||
INTO v_column_type, v_column_comment, v_extra
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND COLUMN_NAME = 'id'
|
||||
LIMIT 1;
|
||||
|
||||
IF v_column_type IS NOT NULL AND v_extra LIKE '%auto_increment%' THEN
|
||||
SET @alter_sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` MODIFY COLUMN `id` ',
|
||||
v_column_type,
|
||||
' NOT NULL'
|
||||
);
|
||||
|
||||
IF v_column_comment IS NOT NULL AND v_column_comment <> '' THEN
|
||||
SET @alter_sql = CONCAT(@alter_sql, ' COMMENT ', QUOTE(v_column_comment));
|
||||
END IF;
|
||||
|
||||
PREPARE stmt FROM @alter_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('push_template');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message_channel_config');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message_source_system');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message_source_system_channel');
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
-- MessageHub 雪花 ID 主键口径对齐补丁
|
||||
-- 适用场景:
|
||||
-- 1. 已经执行过旧版 MessageHub 初始化 SQL 的存量库
|
||||
-- 2. 需要将运行时已切到应用层雪花 ID 的表,去掉数据库侧 AUTO_INCREMENT
|
||||
--
|
||||
-- 注意:
|
||||
-- 1. 如果你的数据库名不是 messagehub,请先修改下面的 use 语句
|
||||
-- 2. 本补丁不会改动已有数据,只会调整 id 列定义
|
||||
|
||||
USE messagehub;
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE sp_drop_auto_increment_on_id_if_exists(IN p_table_name VARCHAR(64))
|
||||
BEGIN
|
||||
DECLARE v_column_type VARCHAR(128) DEFAULT NULL;
|
||||
DECLARE v_column_comment VARCHAR(255) DEFAULT NULL;
|
||||
DECLARE v_extra VARCHAR(128) DEFAULT NULL;
|
||||
|
||||
SELECT COLUMN_TYPE, COLUMN_COMMENT, EXTRA
|
||||
INTO v_column_type, v_column_comment, v_extra
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND COLUMN_NAME = 'id'
|
||||
LIMIT 1;
|
||||
|
||||
IF v_column_type IS NOT NULL AND v_extra LIKE '%auto_increment%' THEN
|
||||
SET @alter_sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` MODIFY COLUMN `id` ',
|
||||
v_column_type,
|
||||
' NOT NULL'
|
||||
);
|
||||
|
||||
IF v_column_comment IS NOT NULL AND v_column_comment <> '' THEN
|
||||
SET @alter_sql = CONCAT(@alter_sql, ' COMMENT ', QUOTE(v_column_comment));
|
||||
END IF;
|
||||
|
||||
PREPARE stmt FROM @alter_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('messageDO');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('push_template');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message_channel_config');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message_source_system');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('message_source_system_channel');
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
@@ -0,0 +1,74 @@
|
||||
-- YumiNail 雪花 ID 主键口径对齐补丁
|
||||
-- 适用场景:
|
||||
-- 1. 已经执行过旧版 YumiNail.sql 的存量库
|
||||
-- 2. 需要将运行时已切到应用层雪花 ID 的表,去掉数据库侧 AUTO_INCREMENT
|
||||
--
|
||||
-- 注意:
|
||||
-- 1. 如果你的数据库名不是 nailart,请先修改下面的 use 语句
|
||||
-- 2. 本补丁不会改动已有数据,只会调整 id 列定义
|
||||
|
||||
USE nailart;
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE sp_drop_auto_increment_on_id_if_exists(IN p_table_name VARCHAR(64))
|
||||
BEGIN
|
||||
DECLARE v_column_type VARCHAR(128) DEFAULT NULL;
|
||||
DECLARE v_column_comment VARCHAR(255) DEFAULT NULL;
|
||||
DECLARE v_extra VARCHAR(128) DEFAULT NULL;
|
||||
|
||||
SELECT COLUMN_TYPE, COLUMN_COMMENT, EXTRA
|
||||
INTO v_column_type, v_column_comment, v_extra
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND COLUMN_NAME = 'id'
|
||||
LIMIT 1;
|
||||
|
||||
IF v_column_type IS NOT NULL AND v_extra LIKE '%auto_increment%' THEN
|
||||
SET @alter_sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` MODIFY COLUMN `id` ',
|
||||
v_column_type,
|
||||
' NOT NULL'
|
||||
);
|
||||
|
||||
IF v_column_comment IS NOT NULL AND v_column_comment <> '' THEN
|
||||
SET @alter_sql = CONCAT(@alter_sql, ' COMMENT ', QUOTE(v_column_comment));
|
||||
END IF;
|
||||
|
||||
PREPARE stmt FROM @alter_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_balance');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_balance_change_detail');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_cart');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_comment');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_coupon');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_coupon_tag');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_effect_diagram');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_login_identity');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_nail_artist_info');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_object_storage_file');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_order');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_order_item');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_permission');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_product');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_product_like');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_shop');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_tag');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user_coupon');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user_permission');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_vip_benefit');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_vip_info');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_wishlist');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user_shop_binding');
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
@@ -1274,5 +1274,74 @@ WHERE u.is_delete = 0
|
||||
AND up.permission_id = p.id
|
||||
);
|
||||
|
||||
-- ========================================
|
||||
-- 雪花 ID 口径收口区:去掉运行时主键表的 AUTO_INCREMENT
|
||||
-- 说明:
|
||||
-- 1. 前面的建表与种子数据继续沿用旧链路,保证空库初始化过程不受影响
|
||||
-- 2. 本段在所有初始化数据完成后执行,将运行时已切到应用层雪花 ID 的表统一收口
|
||||
-- 3. 与 2026-03-30_snowflake_id_alignment_patch.sql 保持同口径
|
||||
-- ========================================
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE sp_drop_auto_increment_on_id_if_exists(IN p_table_name VARCHAR(64))
|
||||
BEGIN
|
||||
DECLARE v_column_type VARCHAR(128) DEFAULT NULL;
|
||||
DECLARE v_column_comment VARCHAR(255) DEFAULT NULL;
|
||||
DECLARE v_extra VARCHAR(128) DEFAULT NULL;
|
||||
|
||||
SELECT COLUMN_TYPE, COLUMN_COMMENT, EXTRA
|
||||
INTO v_column_type, v_column_comment, v_extra
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND COLUMN_NAME = 'id'
|
||||
LIMIT 1;
|
||||
|
||||
IF v_column_type IS NOT NULL AND v_extra LIKE '%auto_increment%' THEN
|
||||
SET @alter_sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` MODIFY COLUMN `id` ',
|
||||
v_column_type,
|
||||
' NOT NULL'
|
||||
);
|
||||
|
||||
IF v_column_comment IS NOT NULL AND v_column_comment <> '' THEN
|
||||
SET @alter_sql = CONCAT(@alter_sql, ' COMMENT ', QUOTE(v_column_comment));
|
||||
END IF;
|
||||
|
||||
PREPARE stmt FROM @alter_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_balance');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_balance_change_detail');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_cart');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_comment');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_coupon');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_coupon_tag');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_effect_diagram');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_login_identity');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_nail_artist_info');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_object_storage_file');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_order');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_order_item');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_permission');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_product');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_product_like');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_shop');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_tag');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user_coupon');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user_permission');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_vip_benefit');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_vip_info');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_wishlist');
|
||||
CALL sp_drop_auto_increment_on_id_if_exists('t_user_shop_binding');
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists;
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
# 2026-03-30 雪花 ID 数据库对齐补丁
|
||||
|
||||
## 背景
|
||||
|
||||
- 本轮代码已经将 `YumiNail-Core`、`MessageHub` 的主键生成统一切到应用层“时间戳高位雪花 ID”。
|
||||
- 旧版初始化 SQL 仍保留了数据库侧 `AUTO_INCREMENT`。
|
||||
- 这不会影响运行,但会造成“代码口径”和“库结构口径”不完全一致。
|
||||
|
||||
## 处理策略
|
||||
|
||||
这次没有直接改前半段建表与种子插入逻辑,而是采用“收口补丁”方案:
|
||||
|
||||
- 新部署:
|
||||
- 继续执行主初始化 SQL
|
||||
- 在主初始化 SQL 末尾自动追加去掉 `AUTO_INCREMENT` 的收口段
|
||||
- 已部署初版的存量库:
|
||||
- 手动执行本轮新增的增量补丁 SQL
|
||||
|
||||
这样做的原因:
|
||||
|
||||
- 旧版初始化 SQL 中有多处种子数据插入没有显式写 `id`
|
||||
- 如果直接把前面的建表定义全部改成“无自增”,会把空库初始化链路弄坏
|
||||
- 放到初始化末尾再统一去掉自增,可以同时兼容空库首装和存量库升级
|
||||
|
||||
## 新增文件
|
||||
|
||||
### YumiNail
|
||||
|
||||
- 增量补丁:
|
||||
- `YumiNail-Core/src/main/resources/files/2026-03-30_snowflake_id_alignment_patch.sql`
|
||||
- 主初始化 SQL 已同步:
|
||||
- `YumiNail-Core/src/main/resources/files/YumiNail.sql`
|
||||
|
||||
### MessageHub
|
||||
|
||||
- 增量补丁:
|
||||
- `MessageHub/src/main/resources/sql/2026-03-30_messagehub_snowflake_id_alignment_patch.sql`
|
||||
- 主初始化 SQL 已同步:
|
||||
- `MessageHub/src/main/resources/sql/2026-03-23_messagehub_complete_manual_init.sql`
|
||||
|
||||
## 你现在应该怎么执行
|
||||
|
||||
你现在已经执行过初版部署 SQL,所以按下面顺序执行增量补丁即可:
|
||||
|
||||
1. 在 `YumiNail` 数据库执行:
|
||||
- `YumiNail-Core/src/main/resources/files/2026-03-30_snowflake_id_alignment_patch.sql`
|
||||
2. 在 `MessageHub` 数据库执行:
|
||||
- `MessageHub/src/main/resources/sql/2026-03-30_messagehub_snowflake_id_alignment_patch.sql`
|
||||
|
||||
## 新环境部署口径
|
||||
|
||||
后续新部署推荐直接执行:
|
||||
|
||||
1. `YumiNail-Core/src/main/resources/files/YumiNail.sql`
|
||||
2. `MessageHub/src/main/resources/sql/2026-03-23_messagehub_complete_manual_init.sql`
|
||||
|
||||
不需要再额外补执行本轮增量 SQL。
|
||||
|
||||
## 补丁内容
|
||||
|
||||
两份增量补丁都只做一件事:
|
||||
|
||||
- 检查目标表的 `id` 列如果仍带 `AUTO_INCREMENT`
|
||||
- 动态执行 `ALTER TABLE ... MODIFY COLUMN id ... NOT NULL`
|
||||
- 保留原字段类型和原字段注释
|
||||
|
||||
## 影响表
|
||||
|
||||
### YumiNail
|
||||
|
||||
- `t_balance`
|
||||
- `t_balance_change_detail`
|
||||
- `t_cart`
|
||||
- `t_comment`
|
||||
- `t_coupon`
|
||||
- `t_coupon_tag`
|
||||
- `t_effect_diagram`
|
||||
- `t_login_identity`
|
||||
- `t_nail_artist_info`
|
||||
- `t_object_storage_file`
|
||||
- `t_order`
|
||||
- `t_order_item`
|
||||
- `t_permission`
|
||||
- `t_product`
|
||||
- `t_product_like`
|
||||
- `t_shop`
|
||||
- `t_tag`
|
||||
- `t_user`
|
||||
- `t_user_coupon`
|
||||
- `t_user_permission`
|
||||
- `t_vip_benefit`
|
||||
- `t_vip_info`
|
||||
- `t_wishlist`
|
||||
- `t_user_shop_binding`
|
||||
|
||||
### MessageHub
|
||||
|
||||
- `message`
|
||||
- `messageDO`
|
||||
- `push_template`
|
||||
- `message_channel_config`
|
||||
- `message_source_system`
|
||||
- `message_source_system_channel`
|
||||
|
||||
## 是否需要数据回填
|
||||
|
||||
不需要。
|
||||
|
||||
原因:
|
||||
|
||||
- 这次没有改 `id` 的字段类型,仍然是 `BIGINT` / `BIGINT UNSIGNED`
|
||||
- 只是去掉数据库自增属性
|
||||
- 已有记录的 `id` 不变
|
||||
- 业务号、订单号、消息号、对象名等也不需要额外回填
|
||||
|
||||
## 风险说明
|
||||
|
||||
- 本补丁不会删除数据,也不会改主键值
|
||||
- 如果你的数据库名不是默认的 `nailart` / `messagehub`,执行前需要先改补丁中的 `USE` 语句
|
||||
- `MessageHub/src/main/resources/sql/init.sql` 属于历史文件,不作为本轮推荐执行入口,未纳入本次主链路同步
|
||||
Reference in New Issue
Block a user