diff --git a/backend/futures_trade_record/sql/03_create_open_table.sql b/backend/futures_trade_record/sql/03_create_open_table.sql index cb1905a..3d39a38 100644 --- a/backend/futures_trade_record/sql/03_create_open_table.sql +++ b/backend/futures_trade_record/sql/03_create_open_table.sql @@ -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; diff --git a/backend/futures_trade_record/sql/04_create_close_table.sql b/backend/futures_trade_record/sql/04_create_close_table.sql new file mode 100644 index 0000000..065a05e --- /dev/null +++ b/backend/futures_trade_record/sql/04_create_close_table.sql @@ -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 $$; \ No newline at end of file