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"} + ] +}