add
This commit is contained in:
48
backend/trading_record/sql/01_uuid_v7_setup.sql
Normal file
48
backend/trading_record/sql/01_uuid_v7_setup.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
-- 切换到目标数据库
|
||||
\c postgres;
|
||||
|
||||
-- 检查并创建UUID扩展(如果不存在)
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
-- 定义检测UUID v7支持的函数
|
||||
CREATE OR REPLACE FUNCTION check_uuid_v7_support() RETURNS BOOLEAN AS $$
|
||||
DECLARE
|
||||
test_uuid UUID;
|
||||
BEGIN
|
||||
BEGIN
|
||||
SELECT gen_random_uuid_v7() INTO test_uuid;
|
||||
RETURN TRUE;
|
||||
EXCEPTION
|
||||
WHEN undefined_function THEN
|
||||
RETURN FALSE;
|
||||
END;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
-- 创建UUID v7兼容函数(修复UUID格式长度问题)
|
||||
CREATE OR REPLACE FUNCTION gen_random_uuid_v7() RETURNS uuid AS $$
|
||||
DECLARE
|
||||
unix_ts_ms BIGINT;
|
||||
rand_a BIGINT;
|
||||
rand_b BIGINT;
|
||||
hex_str TEXT;
|
||||
BEGIN
|
||||
-- 获取当前毫秒级Unix时间戳
|
||||
unix_ts_ms := (EXTRACT(EPOCH FROM NOW()) * 1000)::BIGINT;
|
||||
|
||||
-- 生成随机数(调整随机数范围以确保总长度正确)
|
||||
rand_a := (random() * (2^20 - 1))::BIGINT;
|
||||
rand_b := (random() * (2^44 - 1))::BIGINT; -- 从48位调整为44位,减少2个字节
|
||||
|
||||
-- 组合UUID v7格式(确保总长度为32个十六进制字符)
|
||||
hex_str :=
|
||||
lpad(to_hex(unix_ts_ms >> 12), 8, '0') ||
|
||||
lpad(to_hex((unix_ts_ms & 4095) << 4), 4, '0') ||
|
||||
'7' || lpad(to_hex(rand_a >> 18), 3, '0') ||
|
||||
lpad(to_hex(8 + (rand_a & 16383) >> 12), 2, '0') ||
|
||||
lpad(to_hex(rand_a & 4095), 3, '0') ||
|
||||
lpad(to_hex(rand_b), 11, '0'); -- 从12位调整为11位
|
||||
|
||||
RETURN hex_str::uuid;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
30
backend/trading_record/sql/02_create_country_table.sql
Normal file
30
backend/trading_record/sql/02_create_country_table.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
-- 切换到目标数据库
|
||||
\c postgres;
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_country_modified_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'country') THEN
|
||||
CREATE TABLE "country" ( -- country是关键字,用双引号包裹
|
||||
id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TRIGGER update_country_updated_at
|
||||
BEFORE UPDATE ON "country"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_country_modified_column();
|
||||
|
||||
RAISE NOTICE 'Created country table and trigger';
|
||||
ELSE
|
||||
RAISE NOTICE 'country table already exists';
|
||||
END IF;
|
||||
END $$;
|
||||
32
backend/trading_record/sql/03_create_name_table.sql
Normal file
32
backend/trading_record/sql/03_create_name_table.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- 切换到目标数据库
|
||||
\c postgres;
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_name_modified_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'name') THEN
|
||||
CREATE TABLE name (
|
||||
id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
|
||||
country_id UUID NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TRIGGER update_name_updated_at
|
||||
BEFORE UPDATE ON "name"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_name_modified_column();
|
||||
|
||||
RAISE NOTICE 'Created name table and trigger';
|
||||
ELSE
|
||||
RAISE NOTICE 'name table already exists';
|
||||
END IF;
|
||||
END $$;
|
||||
32
backend/trading_record/sql/04_create_code_table.sql
Normal file
32
backend/trading_record/sql/04_create_code_table.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- 切换到目标数据库
|
||||
\c postgres;
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_code_modified_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'code') THEN
|
||||
CREATE TABLE code (
|
||||
id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
|
||||
country_id UUID NOT NULL,
|
||||
code VARCHAR NOT NULL,
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TRIGGER update_code_updated_at
|
||||
BEFORE UPDATE ON "code"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_code_modified_column();
|
||||
|
||||
RAISE NOTICE 'Created code table and trigger';
|
||||
ELSE
|
||||
RAISE NOTICE 'code table already exists';
|
||||
END IF;
|
||||
END $$;
|
||||
38
backend/trading_record/sql/05_create_info_view.sql
Normal file
38
backend/trading_record/sql/05_create_info_view.sql
Normal file
@@ -0,0 +1,38 @@
|
||||
\c postgres;
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
view_exists BOOLEAN;
|
||||
BEGIN
|
||||
-- 检查视图是否已存在
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.views
|
||||
WHERE table_name = 'country_info_view'
|
||||
) INTO view_exists;
|
||||
|
||||
-- 创建或更新视图
|
||||
CREATE OR REPLACE VIEW country_info_view AS
|
||||
SELECT
|
||||
u.id AS country_id,
|
||||
n.name AS name,
|
||||
c.code AS code,
|
||||
u.deleted AS deleted
|
||||
FROM
|
||||
"country" u
|
||||
JOIN
|
||||
name n ON u.id = n.country_id
|
||||
JOIN
|
||||
code c ON u.id = c.country_id
|
||||
WHERE
|
||||
u.deleted = FALSE;
|
||||
|
||||
-- 根据视图是否已存在输出不同提示
|
||||
IF view_exists THEN
|
||||
RAISE NOTICE '视图 country_info_view 已更新';
|
||||
ELSE
|
||||
RAISE NOTICE '视图 country_info_view 已创建';
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN OTHERS THEN
|
||||
RAISE NOTICE '处理视图时发生错误: %', SQLERRM;
|
||||
END $$;
|
||||
Reference in New Issue
Block a user