diff --git a/create/README.md b/create/README.md index 01e3d93..a0ca0ab 100644 --- a/create/README.md +++ b/create/README.md @@ -202,6 +202,4 @@ DO $$ BEGIN RAISE NOTICE '✅============ 数据库表部署完成 ============✅'; END $$; - -``` - +``` \ No newline at end of file diff --git a/create/create.sh b/create/create.sh index 6c3c1e9..9c40b02 100644 --- a/create/create.sh +++ b/create/create.sh @@ -3,7 +3,11 @@ set -e # 遇到错误立即退出 +# 定义表名变量,可以根据需要修改 +TABLE_NAME="cn_pmi_234_aaarecords" + echo "🚀 启动Python容器执行create_table.py..." +echo "📋 目标表名: ${TABLE_NAME}" # 获取脚本所在目录的绝对路径 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -24,6 +28,7 @@ docker run --rm \ --name "${CONTAINER_NAME}" \ -v "${SCRIPT_DIR}:/app/create" \ -v "${PROJECT_ROOT}/infra/postgres/sql:/app/infra/postgres/sql" \ + -e TABLE_NAME="${TABLE_NAME}" \ -w /app \ python:3.13.7-alpine3.22 \ sh -c " diff --git a/create/create_table.py b/create/create_table.py index a45af0e..4f76d84 100644 --- a/create/create_table.py +++ b/create/create_table.py @@ -7,8 +7,8 @@ create.py - 动态生成PostgreSQL建表SQL语句 import os import re -# 1. 定义表名称变量,方便调整 -table_name = "cn_pmi_234_aaarecords" # 可以根据需要修改表名 +# 从环境变量获取表名,如果没有设置则使用默认值 +table_name = os.environ.get('TABLE_NAME', 'cn_pmi_234_aaarecords') # 2. 定义SQL模板 sql_template = f"""DO $$ @@ -69,7 +69,7 @@ def update_sql_file(): with open(sql_file_path, 'r', encoding='utf-8') as f: content = f.read() - # 查找插入位置(在\"部署完成\"日志前) + # 查找插入位置(在"部署完成"日志前) insert_pattern = r'(DO \$\$.*?RAISE NOTICE \'🚀============ 数据库表部署开始 ============🚀\'.*?END \$\$;)(.*?)(DO \$\$.*?RAISE NOTICE \'============ 数据库表部署完成 ============\'.*?END \$\$;)' match = re.search(insert_pattern, content, re.DOTALL)