76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
import re
|
|
from pathlib import Path
|
|
|
|
db = Path(r"c:\Users\wilso\Desktop\oms0414-back\db.sql").read_text(encoding="utf-8")
|
|
sql_dir = Path(r"c:\Users\wilso\Desktop\oms0414-back\sql")
|
|
missing = []
|
|
|
|
# Extract CREATE TABLE blocks from db.sql for per-table column checks
|
|
table_cols = {}
|
|
for m in re.finditer(r"CREATE TABLE `([^`]+)`\s*\((.*?)\)\s*(?:ENGINE|COMMENT|=|;)", db, re.S | re.I):
|
|
name = m.group(1)
|
|
body = m.group(2)
|
|
cols = re.findall(r"`([^`]+)`", body)
|
|
# filter index-only mentions roughly: keep all backtick names in body
|
|
table_cols[name] = set(cols)
|
|
|
|
for f in sorted(sql_dir.glob("*.sql")):
|
|
if "menu_notes" in f.name:
|
|
continue
|
|
text = f.read_text(encoding="utf-8")
|
|
creates = re.findall(r"CREATE TABLE(?: IF NOT EXISTS)?\s+`([^`]+)`", text, re.I)
|
|
# ALTER TABLE `x` ... ADD COLUMN `c`
|
|
alter_adds = re.findall(
|
|
r"ALTER TABLE\s+`([^`]+)`[\s\S]*?(?=ALTER TABLE|CREATE TABLE|\Z)",
|
|
text,
|
|
re.I,
|
|
)
|
|
# Better: find each ADD COLUMN with preceding table via scanning
|
|
lines_out = []
|
|
for t in creates:
|
|
ok = f"CREATE TABLE `{t}`" in db
|
|
lines_out.append(("OK" if ok else "MISS", "TABLE", t, None))
|
|
if not ok:
|
|
missing.append(f"{f.name}: TABLE `{t}`")
|
|
|
|
# Parse ADD COLUMN with associated table
|
|
pos = 0
|
|
current_table = None
|
|
for m in re.finditer(
|
|
r"ALTER TABLE\s+`([^`]+)`|ADD COLUMN(?: IF NOT EXISTS)?\s+`([^`]+)`|DROP COLUMN\s+`([^`]+)`|MODIFY COLUMN\s+`([^`]+)`",
|
|
text,
|
|
re.I,
|
|
):
|
|
if m.group(1):
|
|
current_table = m.group(1)
|
|
elif m.group(2):
|
|
col = m.group(2)
|
|
in_db = f"`{col}`" in db
|
|
in_table = current_table and col in table_cols.get(current_table, set())
|
|
ok = bool(in_table) if current_table in table_cols else in_db
|
|
lines_out.append(("OK" if ok else "MISS", "ADD", col, current_table))
|
|
if not ok:
|
|
missing.append(f"{f.name}: ADD `{current_table}`.`{col}`")
|
|
elif m.group(3):
|
|
col = m.group(3)
|
|
still = current_table and col in table_cols.get(current_table, set())
|
|
lines_out.append(("STILL" if still else "OK", "DROP", col, current_table))
|
|
if still:
|
|
missing.append(f"{f.name}: DROP `{current_table}`.`{col}` still in db.sql")
|
|
elif m.group(4):
|
|
col = m.group(4)
|
|
ok = current_table and col in table_cols.get(current_table, set())
|
|
# For MODIFY we just check presence; nullability needs manual review
|
|
lines_out.append(("OK" if ok else "MISS", "MODIFY", col, current_table))
|
|
if not ok:
|
|
missing.append(f"{f.name}: MODIFY `{current_table}`.`{col}` absent")
|
|
|
|
if lines_out:
|
|
print(f"==== {f.name} ====")
|
|
for st, kind, name, tbl in lines_out:
|
|
loc = f"{tbl}." if tbl else ""
|
|
print(f" [{st}] {kind} {loc}{name}")
|
|
|
|
print("\n==== SUMMARY (needs db.sql sync) ====")
|
|
print("\n".join(missing) if missing else "(none)")
|