杂务:对齐 SQL 与雪花 ID 策略
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user