From fc51fba1ea2eff381f0089e86c63b8738d3086c0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 1 Sep 2026 16:22:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(deploy):=20dmig=20=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=84=9F=E7=9F=A5=E8=BF=81=E7=A7=BB=E2=80=94=E2=80=94env?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E9=99=90=E5=AE=9Aprod/test=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - m0005 生产专用 workspace_base=/d/doit/workspaces(env=prod) - detect_env: DEPLOY_ENV优先,/doit/路径推断 - 防止生产专用参数在测试环境误执行 --- deploy/dmig.py | 21 ++++++++++++++++++- .../migrations/m0005_workspace_base_prod.json | 12 +++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 deploy/migrations/m0005_workspace_base_prod.json diff --git a/deploy/dmig.py b/deploy/dmig.py index be04980..ee1c0d6 100755 --- a/deploy/dmig.py +++ b/deploy/dmig.py @@ -119,16 +119,35 @@ def _esc(s): return str(s).replace("\\", "\\\\").replace("'", "''") +def detect_env(): + """当前环境:DEPLOY_ENV 环境变量优先;否则按应用根路径推断(/doit/ → prod)。""" + env = os.environ.get("DEPLOY_ENV", "").strip().lower() + if env in ("test", "prod", "dev"): + return env + if "/doit/" in APP_ROOT: + return "prod" + return "test" + + +def env_match(mig, env): + """迁移文件可选 "env": "prod"/"test"/"all"(缺省=all),环境不匹配的批次跳过。""" + want = (mig.get("env") or "all").lower() + return want in ("all", env) + + def load_migrations(): migs = {} if not os.path.isdir(MIG_DIR): return migs + env = detect_env() for f in sorted(os.listdir(MIG_DIR)): if not f.endswith(".json"): continue try: d = json.load(open(os.path.join(MIG_DIR, f))) d["_file"] = f + if not env_match(d, env): + continue # 环境不匹配:本环境不可见 migs[d["id"]] = d except Exception as e: die("迁移文件解析失败 %s: %s" % (f, e)) @@ -188,7 +207,7 @@ def cmd_status(conf): ensure_ledger(conf) applied = ledger_state(conf) migs = load_migrations() - print("=== 迁移台账 ===") + print("=== 迁移台账(当前环境: %s)===" % detect_env()) for mid in sorted(migs): m = migs[mid] if mid in applied: diff --git a/deploy/migrations/m0005_workspace_base_prod.json b/deploy/migrations/m0005_workspace_base_prod.json new file mode 100644 index 0000000..05b7449 --- /dev/null +++ b/deploy/migrations/m0005_workspace_base_prod.json @@ -0,0 +1,12 @@ +{ + "id": "m0005", + "env": "prod", + "title": "生产设 workspace_base=/d/doit/workspaces(代码兜底是测试路径,不设会导致工作空间写错位置)", + "backup_tables": [], + "up": [ + {"op": "params_set", "key": "workspace_base", "value": "/d/doit/workspaces"} + ], + "down": [ + {"op": "params_del", "key": "workspace_base"} + ] +}