Files
ai_trading_api/create/README.md
2025-12-26 16:08:07 +08:00

100 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
以下是我的工程目录组织。
```
.
├── LICENSE
├── README.md
├── create
│ └── create.py
├── docker-compose.yaml
├── infra
│ └── postgres
│ ├── scripts
│ │ └── db-lanuch-entrypoint.sh
│ └── sql
│ ├── 01_uuid_v7_setup.sql
│ ├── 02_create_function.sql
│ └── 03_create_table.sql
└── services
└── cn_futures_trading_records
├── README.md
├── dev.sh
└── src
├── Dockerfile
├── crud
│ └── create.go
├── go.mod
├── go.sum
├── infra
│ ├── launch.go
│ ├── logger.go
│ └── postgres.go
├── main.go
└── model
└── payload.go
```
以下是 03_create_table.sql 的内容:
```sql
-- =========================================================
-- table.sql (PostgreSQL 17.4+)
-- =========================================================
\pset pager off
\timing on
\c postgres;
CREATE EXTENSION IF NOT EXISTS "moddatetime" SCHEMA public;
DO $$
BEGIN
RAISE NOTICE '🚀============ 数据库表部署开始 ============🚀';
END $$;
-- create table logic
DO $$
BEGIN
RAISE NOTICE '============ 数据库表部署完成 ============';
END $$;
```
我需要你帮我在/create/create.py 中实现一下逻辑:
1、定义一个变量参数方便我调整表名称。
2、通过我定义的表名称把一下的替换以下的 `tabe_name`关键字。
```sql
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'tabe_name'
) THEN
CREATE TABLE tabe_name (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY, -- id
payload JSONB NOT NULL, -- 数据
deleted BOOLEAN NOT NULL DEFAULT FALSE, -- 删除状态
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 记录创建时间
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP -- 记录修改时间
);
-- 3 触发器:自动刷新 updated_at
CREATE TRIGGER trg_tabe_name_at
BEFORE UPDATE ON tabe_name
FOR EACH ROW
EXECUTE FUNCTION moddatetime(updated_at);
RAISE NOTICE 'tabe_name 表已创建';
ELSE
RAISE NOTICE 'tabe_name 表已存在,跳过';
END IF;
END $$;
```
3、拿到2中的处理结果写入 03_create_table.sql 中,写入规则为则追加到""部署完成"日志上面。