feat(deploy): dmig 环境感知迁移——env字段限定prod/test执行范围
- m0005 生产专用 workspace_base=/d/doit/workspaces(env=prod) - detect_env: DEPLOY_ENV优先,/doit/路径推断 - 防止生产专用参数在测试环境误执行
This commit is contained in:
parent
27b6a7f543
commit
fc51fba1ea
@ -119,16 +119,35 @@ def _esc(s):
|
|||||||
return str(s).replace("\\", "\\\\").replace("'", "''")
|
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():
|
def load_migrations():
|
||||||
migs = {}
|
migs = {}
|
||||||
if not os.path.isdir(MIG_DIR):
|
if not os.path.isdir(MIG_DIR):
|
||||||
return migs
|
return migs
|
||||||
|
env = detect_env()
|
||||||
for f in sorted(os.listdir(MIG_DIR)):
|
for f in sorted(os.listdir(MIG_DIR)):
|
||||||
if not f.endswith(".json"):
|
if not f.endswith(".json"):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
d = json.load(open(os.path.join(MIG_DIR, f)))
|
d = json.load(open(os.path.join(MIG_DIR, f)))
|
||||||
d["_file"] = f
|
d["_file"] = f
|
||||||
|
if not env_match(d, env):
|
||||||
|
continue # 环境不匹配:本环境不可见
|
||||||
migs[d["id"]] = d
|
migs[d["id"]] = d
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
die("迁移文件解析失败 %s: %s" % (f, e))
|
die("迁移文件解析失败 %s: %s" % (f, e))
|
||||||
@ -188,7 +207,7 @@ def cmd_status(conf):
|
|||||||
ensure_ledger(conf)
|
ensure_ledger(conf)
|
||||||
applied = ledger_state(conf)
|
applied = ledger_state(conf)
|
||||||
migs = load_migrations()
|
migs = load_migrations()
|
||||||
print("=== 迁移台账 ===")
|
print("=== 迁移台账(当前环境: %s)===" % detect_env())
|
||||||
for mid in sorted(migs):
|
for mid in sorted(migs):
|
||||||
m = migs[mid]
|
m = migs[mid]
|
||||||
if mid in applied:
|
if mid in applied:
|
||||||
|
|||||||
12
deploy/migrations/m0005_workspace_base_prod.json
Normal file
12
deploy/migrations/m0005_workspace_base_prod.json
Normal 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"}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user