feat(deploy): dmig 环境感知迁移——env字段限定prod/test执行范围

- m0005 生产专用 workspace_base=/d/doit/workspaces(env=prod)
- detect_env: DEPLOY_ENV优先,/doit/路径推断
- 防止生产专用参数在测试环境误执行
This commit is contained in:
yumoqing 2026-09-01 16:22:14 +08:00
parent 27b6a7f543
commit fc51fba1ea
2 changed files with 32 additions and 1 deletions

View File

@ -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:

View File

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