59 行
2.7 KiB
Python
59 行
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Explicit DM schema installer. Private connection JSON must name a dedicated DataGov schema."""
|
|
import argparse
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import stat
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--config",required=True,type=Path)
|
|
mode=parser.add_mutually_exclusive_group()
|
|
mode.add_argument("--apply",action="store_true")
|
|
mode.add_argument("--verify",action="store_true")
|
|
parser.add_argument("--reset-empty-test-installation",action="store_true")
|
|
parser.add_argument("--reset-empty-test-table")
|
|
args=parser.parse_args()
|
|
path=args.config.resolve()
|
|
if not path.is_relative_to(ROOT / "runtime/dev-private") or not path.is_file() or stat.S_IMODE(path.stat().st_mode)&0o077:
|
|
parser.error("configuration must be a 0600 private file under runtime/dev-private")
|
|
values=json.loads(path.read_text())
|
|
environment=os.environ.copy()
|
|
for key in ("host","port","username","password","schema"):
|
|
if not values.get(key):parser.error("missing private field: "+key)
|
|
environment["DATAGOV_DM_"+key.upper()]=str(values[key])
|
|
if not re.fullmatch(r"[A-Za-z0-9.-]+",str(values['host'])) or not 1<=int(values['port'])<=65535:
|
|
parser.error("invalid private endpoint")
|
|
source=(ROOT / "db/dameng/schema.sql").read_text()
|
|
rows=[]
|
|
for part in source.split("-- @statement ")[1:]:
|
|
label,sql=part.split("\n",1)
|
|
sql=sql.strip()
|
|
if sql.endswith("/"):sql=sql[:-1].rstrip()
|
|
elif sql.endswith(";"):sql=sql[:-1]
|
|
head=re.match(r"CREATE\s+(?:UNIQUE\s+)?(?:TABLE|INDEX|TRIGGER|FUNCTION)\s+(\w+)",sql,re.I)
|
|
if not head:raise ValueError("unrecognized schema statement")
|
|
key=label+":"+head.group(1)
|
|
digest=hashlib.sha256(sql.encode()).hexdigest()
|
|
rows.append(key+"\t"+digest+"\t"+base64.b64encode(sql.encode()).decode())
|
|
if not args.apply and not args.verify:
|
|
print("PLAN_ONLY statements="+str(len(rows))+" database_connection=NOT_RUN")
|
|
return 0
|
|
driver=ROOT / "script/datagov/m2/seatunnel/local/artifacts/DmJdbcDriver18-8.1.3.62.jar"
|
|
process=subprocess.run(["java","--class-path",str(driver),str(ROOT / "script/database/DamengSchema.java"),
|
|
*(["--verify"] if args.verify else ["--reset-empty-test-installation"] if args.reset_empty_test_installation else ["--reset-empty-test-table",args.reset_empty_test_table] if args.reset_empty_test_table else [])],
|
|
input="\n".join(rows)+"\n",text=True,env=environment)
|
|
return process.returncode
|
|
|
|
|
|
if __name__=="__main__":
|
|
raise SystemExit(main())
|