diff --git a/create/create.py b/create/create.py index 773cced..1128bd7 100644 --- a/create/create.py +++ b/create/create.py @@ -39,6 +39,19 @@ BEGIN END IF; END $$;""" +def normalize_blank_lines(text): + """规范化空行:确保END $$;和DO $$之间只有一个空行""" + # 将多个空行替换为单个空行 + text = re.sub(r'\n{3,}', '\n\n', text) + + # 确保END $$;后面有一个空行再接DO $$ + text = re.sub(r'END \$\$;(\s*)DO \$\$', r'END $$;\n\nDO $$', text) + + # 清理开头和结尾的多余空行 + text = text.strip() + '\n' + + return text + def update_sql_file(): """将生成的SQL语句追加到03_create_table.sql文件中""" @@ -67,8 +80,14 @@ def update_sql_file(): existing_middle = match.group(2) after_insert = match.group(3) + # 规范化空行 + existing_middle = normalize_blank_lines(existing_middle) + # 组合新内容 - new_content = f"""{before_insert}\n\n{sql_template}\n{existing_middle}\n{after_insert}""" + new_content = f"""{before_insert}\n\n{sql_template}\n\n{existing_middle}\n{after_insert}""" + + # 规范化整个内容的空行 + new_content = normalize_blank_lines(new_content) # 写回文件 with open(sql_file_path, 'w', encoding='utf-8') as f: diff --git a/infra/postgres/sql/03_create_table.sql b/infra/postgres/sql/03_create_table.sql index 0678a76..bcf4ad6 100644 --- a/infra/postgres/sql/03_create_table.sql +++ b/infra/postgres/sql/03_create_table.sql @@ -31,33 +31,7 @@ BEGIN END IF; END $$; -DO $$ -BEGIN - IF NOT EXISTS ( - SELECT 1 - FROM information_schema.tables - WHERE table_schema = 'public' - AND table_name = 'cn_futures_trading_records' - ) THEN - CREATE TABLE cn_futures_trading_records ( - 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_cn_futures_trading_records_at - BEFORE UPDATE ON cn_futures_trading_records - FOR EACH ROW - EXECUTE FUNCTION moddatetime(updated_at); - - RAISE NOTICE 'cn_futures_trading_records 表已创建'; - ELSE - RAISE NOTICE 'cn_futures_trading_records 表已存在,跳过'; - END IF; -END $$; DO $$ BEGIN