pipeline-opportunity/scripts/p1_add_project_id.py

48 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding:utf-8 -*-
"""幂等补列opp_mining_batches / opp_clusters 加 project_idinformation_schema 守卫,绝不 DROP"""
import asyncio, os, sys
W = "/d/pipeline/pipeline-app"; os.chdir(W); sys.path.insert(0, W)
from appPublic.folderUtils import ProgramPath
from appPublic.jsonConfig import getConfig
from appPublic.event_dispatcher import EventDispatcher
from sqlor.dbpools import DBPools
from ahserver.serverenv import ServerEnv
p = ProgramPath(); c = getConfig(W, {"workdir": W, "ProgramPath": p}); DBPools(c.databases)
se = ServerEnv(); se.event_dispatcher = EventDispatcher(); se.get_module_dbname = lambda m: "pipeline"
TARGETS = [("opp_mining_batches", "project_id"), ("opp_clusters", "project_id")]
async def main():
async with DBPools().sqlorContext("pipeline") as sor:
for table, col in TARGETS:
chk = await sor.sqlExe(
"SELECT COLUMN_NAME FROM information_schema.COLUMNS "
"WHERE table_schema=DATABASE() AND table_name=${t}$ AND column_name=${c}$",
{"t": table, "c": col})
await sor.sqlExe("COMMIT", {})
if chk:
print("SKIP(已存在): %s.%s" % (table, col))
continue
await sor.sqlExe(
"ALTER TABLE %s ADD COLUMN `%s` VARCHAR(32) DEFAULT '' "
"COMMENT '项目ID(空=平台级挖掘)'" % (table, col), {})
await sor.sqlExe("COMMIT", {})
print("ADDED: %s.%s" % (table, col))
# 索引:批次按 org+project 查询
try:
await sor.sqlExe("CREATE INDEX idx_opp_batches_orgproj ON opp_mining_batches (org_id, project_id)", {})
await sor.sqlExe("COMMIT", {})
print("index added")
except Exception as e:
print("index skip:", str(e)[:60])
# 验证
for table, col in TARGETS:
chk = await sor.sqlExe(
"SELECT COLUMN_NAME FROM information_schema.COLUMNS "
"WHERE table_schema=DATABASE() AND table_name=${t}$ AND column_name=${c}$",
{"t": table, "c": col})
await sor.sqlExe("COMMIT", {})
print("VERIFY %s.%s:" % (table, col), "OK" if chk else "MISSING")
asyncio.run(main())