add
This commit is contained in:
48
backend/futures_trade_record/sql/01_uuid_v7_setup.sql
Normal file
48
backend/futures_trade_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/futures_trade_record/sql/02_create_country_table.sql
Normal file
30
backend/futures_trade_record/sql/02_create_country_table.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
-- 切换到目标数据库
|
||||
\c postgres;
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_futures_trading_record_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 = 'futures_trading_record') THEN
|
||||
CREATE TABLE "futures_trading_record" ( -- futures_trading_record是关键字,用双引号包裹
|
||||
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_futures_trading_record_updated_at
|
||||
BEFORE UPDATE ON "futures_trading_record"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_futures_trading_record_modified_column();
|
||||
|
||||
RAISE NOTICE 'Created futures_trading_record table and trigger';
|
||||
ELSE
|
||||
RAISE NOTICE 'futures_trading_record table already exists';
|
||||
END IF;
|
||||
END $$;
|
||||
60
backend/futures_trade_record/sql/03_create_name_table.sql
Normal file
60
backend/futures_trade_record/sql/03_create_name_table.sql
Normal file
@@ -0,0 +1,60 @@
|
||||
-- 切换到目标数据库
|
||||
\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 = 'trade_records') THEN
|
||||
-- 创建交易记录表(根据业务场景命名为 trade_records,可按需修改)
|
||||
CREATE TABLE trade_records (
|
||||
-- 主键 UUID,默认使用 PostgreSQL 内置函数生成唯一值
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
|
||||
-- 开仓日期相关(整数类型,限制合理范围避免无效数据)
|
||||
open_year INT NOT NULL CHECK (open_year > 1900 AND open_year <= EXTRACT(YEAR FROM CURRENT_DATE) + 1),
|
||||
open_month INT NOT NULL CHECK (open_month BETWEEN 1 AND 12),
|
||||
open_day INT NOT NULL CHECK (open_day BETWEEN 1 AND 31),
|
||||
|
||||
-- 品种、合约、方向(字符串类型,根据业务长度调整 varchar 长度)
|
||||
variety VARCHAR(50) NOT NULL, -- 品种(如:黄金、原油)
|
||||
contract VARCHAR(50) NOT NULL, -- 合约(如:AU2406、CL2407)
|
||||
direction VARCHAR(20) NOT NULL CHECK (direction IN ('多头', '空头', '买入', '卖出')), -- 限制合法方向值
|
||||
|
||||
-- 开仓相关数值(使用 numeric 类型替代 double,避免浮点数精度丢失,适合金融数据)
|
||||
open_price NUMERIC(12, 6) NOT NULL, -- 开仓价格(精度:整数位6位+小数位6位,可按需调整)
|
||||
open_commission NUMERIC(10, 6) NOT NULL DEFAULT 0.00, -- 开仓手续费(默认0)
|
||||
|
||||
-- 平仓日期相关(允许 NULL,未平仓时为空)
|
||||
close_year INT CHECK (close_year IS NULL OR (close_year > 1900 AND close_year <= EXTRACT(YEAR FROM CURRENT_DATE) + 1)),
|
||||
close_month INT CHECK (close_month IS NULL OR close_month BETWEEN 1 AND 12),
|
||||
close_day INT CHECK (close_day IS NULL OR close_day BETWEEN 1 AND 31),
|
||||
|
||||
-- 平仓相关数值(允许 NULL,未平仓时为空;同样使用 numeric 保证精度)
|
||||
close_price NUMERIC(12, 6), -- 平仓价格
|
||||
close_commission NUMERIC(10, 6) DEFAULT 0.00, -- 平仓手续费(默认0)
|
||||
close_price_diff NUMERIC(12, 6), -- 平仓差价
|
||||
tick_price NUMERIC(12, 6), -- 跳点价格
|
||||
price_diff_profit NUMERIC(12, 6), -- 差价盈亏
|
||||
total_commission NUMERIC(10, 6) DEFAULT 0.00, -- 总手续费(开仓+平仓,可通过触发器自动计算)
|
||||
close_profit NUMERIC(12, 6) -- 平仓盈亏
|
||||
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/futures_trade_record/sql/04_create_code_table.sql
Normal file
32
backend/futures_trade_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,
|
||||
futures_trading_record_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/futures_trade_record/sql/05_create_info_view.sql
Normal file
38
backend/futures_trade_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 = 'futures_trading_record_info_view'
|
||||
) INTO view_exists;
|
||||
|
||||
-- 创建或更新视图
|
||||
CREATE OR REPLACE VIEW futures_trading_record_info_view AS
|
||||
SELECT
|
||||
u.id AS futures_trading_record_id,
|
||||
n.name AS name,
|
||||
c.code AS code,
|
||||
u.deleted AS deleted
|
||||
FROM
|
||||
"futures_trading_record" u
|
||||
JOIN
|
||||
name n ON u.id = n.futures_trading_record_id
|
||||
JOIN
|
||||
code c ON u.id = c.futures_trading_record_id
|
||||
WHERE
|
||||
u.deleted = FALSE;
|
||||
|
||||
-- 根据视图是否已存在输出不同提示
|
||||
IF view_exists THEN
|
||||
RAISE NOTICE '视图 futures_trading_record_info_view 已更新';
|
||||
ELSE
|
||||
RAISE NOTICE '视图 futures_trading_record_info_view 已创建';
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN OTHERS THEN
|
||||
RAISE NOTICE '处理视图时发生错误: %', SQLERRM;
|
||||
END $$;
|
||||
Reference in New Issue
Block a user