add
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
-- 切换到目标数据库
|
||||
\c postgres;
|
||||
|
||||
CREATE OR REPLACE FUNCTION update_country_modified_column()
|
||||
CREATE OR REPLACE FUNCTION update_futures_trading_record_modified_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
@@ -11,20 +11,20 @@ $$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'country') THEN
|
||||
CREATE TABLE "country" ( -- country是关键字,用双引号包裹
|
||||
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_country_updated_at
|
||||
BEFORE UPDATE ON "country"
|
||||
CREATE TRIGGER update_futures_trading_record_updated_at
|
||||
BEFORE UPDATE ON "futures_trading_record"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_country_modified_column();
|
||||
EXECUTE FUNCTION update_futures_trading_record_modified_column();
|
||||
|
||||
RAISE NOTICE 'Created country table and trigger';
|
||||
RAISE NOTICE 'Created futures_trading_record table and trigger';
|
||||
ELSE
|
||||
RAISE NOTICE 'country table already exists';
|
||||
RAISE NOTICE 'futures_trading_record table already exists';
|
||||
END IF;
|
||||
END $$;
|
||||
@@ -14,7 +14,7 @@ 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,
|
||||
futures_trading_record_id UUID NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
@@ -14,7 +14,7 @@ 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,
|
||||
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,
|
||||
|
||||
@@ -7,30 +7,30 @@ BEGIN
|
||||
-- 检查视图是否已存在
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.views
|
||||
WHERE table_name = 'country_info_view'
|
||||
WHERE table_name = 'futures_trading_record_info_view'
|
||||
) INTO view_exists;
|
||||
|
||||
-- 创建或更新视图
|
||||
CREATE OR REPLACE VIEW country_info_view AS
|
||||
CREATE OR REPLACE VIEW futures_trading_record_info_view AS
|
||||
SELECT
|
||||
u.id AS country_id,
|
||||
u.id AS futures_trading_record_id,
|
||||
n.name AS name,
|
||||
c.code AS code,
|
||||
u.deleted AS deleted
|
||||
FROM
|
||||
"country" u
|
||||
"futures_trading_record" u
|
||||
JOIN
|
||||
name n ON u.id = n.country_id
|
||||
name n ON u.id = n.futures_trading_record_id
|
||||
JOIN
|
||||
code c ON u.id = c.country_id
|
||||
code c ON u.id = c.futures_trading_record_id
|
||||
WHERE
|
||||
u.deleted = FALSE;
|
||||
|
||||
-- 根据视图是否已存在输出不同提示
|
||||
IF view_exists THEN
|
||||
RAISE NOTICE '视图 country_info_view 已更新';
|
||||
RAISE NOTICE '视图 futures_trading_record_info_view 已更新';
|
||||
ELSE
|
||||
RAISE NOTICE '视图 country_info_view 已创建';
|
||||
RAISE NOTICE '视图 futures_trading_record_info_view 已创建';
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN OTHERS THEN
|
||||
|
||||
Reference in New Issue
Block a user