This commit is contained in:
vipg
2025-11-14 15:51:36 +08:00
parent 9627c939f6
commit 143a87c3dd
2 changed files with 38 additions and 1 deletions

View File

@@ -30,7 +30,7 @@ BEGIN
FOR EACH ROW
EXECUTE FUNCTION update_open_modified_column();
RAISE NOTICE 'Created open table and trigger';
RAISE NOTICE 'created open table and trigger';
ELSE
RAISE NOTICE 'open table already exists';
END IF;

View File

@@ -0,0 +1,37 @@
-- 切换到目标数据库
\c postgres;
CREATE OR REPLACE FUNCTION update_close_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_close = 'close') THEN
CREATE TABLE close (
id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL,
record_id UUID NOT NULL,
year INT NOT NULL,
month INT NOT NULL,
day INT NOT NULL,
variety_id VARCHAR(50) NOT NULL, -- 品种id
contract VARCHAR(50) NOT NULL, -- 合约2401
direction VARCHAR(20) NOT NULL CHECK (direction IN ('', '')), -- 限制合法方向
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_close_updated_at
BEFORE UPDATE ON "close"
FOR EACH ROW
EXECUTE FUNCTION update_close_modified_column();
RAISE NOTICE 'created close table and trigger';
ELSE
RAISE NOTICE 'close table already exists';
END IF;
END $$;