44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import configparser
|
|
import pyodbc
|
|
|
|
# 读取 git-ignored 的本地机构库配置(不输出任何凭证)
|
|
cfg = configparser.ConfigParser()
|
|
cfg.read("fms-api/config/dbconfigs/G3HD.properties")
|
|
|
|
url = cfg.get("DEFAULT", "url", fallback="")
|
|
user = cfg.get("DEFAULT", "username", fallback="")
|
|
pwd = cfg.get("DEFAULT", "password", fallback="")
|
|
|
|
# url 形如 jdbc:sqlserver://host:port;databaseName=xxx
|
|
host_part = url.split("//", 1)[1].split(";", 1)[0]
|
|
if ":" in host_part:
|
|
host, port = host_part.split(":", 1)
|
|
else:
|
|
host, port = host_part, "1433"
|
|
|
|
print(f"Connecting to {host}:{port} as {user} ...")
|
|
|
|
conn = pyodbc.connect(
|
|
driver="{ODBC Driver 17 for SQL Server}",
|
|
server=f"{host},{port}",
|
|
uid=user,
|
|
pwd=pwd,
|
|
autocommit=False,
|
|
)
|
|
print("Connected OK")
|
|
|
|
with open("fms-api/config/migrations/001_add_i18n_type.sql", encoding="utf-8") as f:
|
|
sql = f.read()
|
|
|
|
# 按 GO 切分批量执行(SQL Server 约定)
|
|
batches = [b.strip() for b in sql.split("\nGO\n") if b.strip()]
|
|
cur = conn.cursor()
|
|
n = 0
|
|
for batch in batches:
|
|
cur.execute(batch)
|
|
n += 1
|
|
conn.commit()
|
|
cur.close()
|
|
conn.close()
|
|
print(f"Migration applied: {n} batch(es) executed on G3HD.")
|