编辑文件 修改自增id.sql
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
This commit is contained in:
@@ -1,26 +1,49 @@
|
||||
select setval('base_sys_user_id_seq', 10000000, false);
|
||||
|
||||
|
||||
-- 1. 恢复原自增,解除依赖
|
||||
-- 清理旧函数
|
||||
ALTER TABLE base_sys_user
|
||||
ALTER COLUMN id SET DEFAULT nextval('base_sys_user_id_seq');
|
||||
|
||||
-- 2. 清理旧函数
|
||||
DROP FUNCTION IF EXISTS next_8digit_rand_id() CASCADE;
|
||||
DROP FUNCTION IF EXISTS shuffle_8digit() CASCADE;
|
||||
|
||||
-- 3. 【终极版】不重复 + 乱序 + 固定8位
|
||||
CREATE OR REPLACE FUNCTION next_8digit_rand_id()
|
||||
-- 8位自增 → 纯数字位置互换 → 依旧8位 → 100%不重复
|
||||
CREATE OR REPLACE FUNCTION shuffle_8digit()
|
||||
RETURNS bigint
|
||||
LANGUAGE plpgsql VOLATILE
|
||||
LANGUAGE plpgsql
|
||||
VOLATILE
|
||||
AS $$
|
||||
DECLARE
|
||||
seq_val bigint;
|
||||
seq bigint;
|
||||
d0 int; d1 int; d2 int; d3 int;
|
||||
d4 int; d5 int; d6 int; d7 int;
|
||||
BEGIN
|
||||
seq_val := nextval('base_sys_user_id_seq');
|
||||
RETURN 10000000 + ((seq_val * 1103515245 + 12345) # 58329) % 90000000;
|
||||
seq := nextval('base_sys_user_id_seq'); -- 原本就是8位
|
||||
|
||||
-- 把 8 位数字拆出来:d7 d6 d5 d4 d3 d2 d1 d0
|
||||
d7 := (seq / 10000000) % 10;
|
||||
d6 := (seq / 1000000) % 10;
|
||||
d5 := (seq / 100000) % 10;
|
||||
d4 := (seq / 10000) % 10;
|
||||
d3 := (seq / 1000) % 10;
|
||||
d2 := (seq / 100) % 10;
|
||||
d1 := (seq / 10) % 10;
|
||||
d0 := seq % 10;
|
||||
|
||||
-- 固定位置互换(一对一置换,绝对不重复)
|
||||
RETURN
|
||||
d3*10000000 +
|
||||
d7*1000000 +
|
||||
d1*100000 +
|
||||
d5*10000 +
|
||||
d2*1000 +
|
||||
d6*100 +
|
||||
d0*10 +
|
||||
d4;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- 4. 启用
|
||||
-- 启用
|
||||
ALTER TABLE base_sys_user
|
||||
ALTER COLUMN id SET DEFAULT next_8digit_rand_id();
|
||||
ALTER COLUMN id SET DEFAULT shuffle_8digit();
|
||||
|
||||
Reference in New Issue
Block a user