You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
895 B
22 lines
895 B
-- 补 product.serialcursor(产品级授权码序号游标,新代码 allocSerialRange 依赖它)
|
|
-- 初值 = 该产品现有授权码的 max(number),让新批次序号接在老数据之后。
|
|
\set ON_ERROR_STOP on
|
|
BEGIN;
|
|
|
|
ALTER TABLE product ADD COLUMN IF NOT EXISTS serialcursor bigint DEFAULT 0;
|
|
UPDATE product SET serialcursor = 0 WHERE serialcursor IS NULL;
|
|
|
|
DO $$
|
|
DECLARE p record; tbl text; mx bigint;
|
|
BEGIN
|
|
FOR p IN SELECT id FROM product ORDER BY id LOOP
|
|
tbl := format('license_%s', to_hex(p.id));
|
|
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name = tbl) THEN
|
|
EXECUTE format('SELECT COALESCE(MAX(number), 0) FROM %I', tbl) INTO mx;
|
|
-- 只抬高不降低,重复执行安全
|
|
UPDATE product SET serialcursor = GREATEST(COALESCE(serialcursor, 0), mx) WHERE id = p.id;
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|
|
|
|
COMMIT;
|
|
|