67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
from urllib.parse import unquote, urlparse
|
|
|
|
import pymysql
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
SQL_FILE = ROOT / "alembic" / "baseline_generated.sql"
|
|
VERSIONS_DIR = ROOT / "alembic" / "versions"
|
|
|
|
|
|
def expected_tables() -> set[str]:
|
|
text = SQL_FILE.read_text(encoding="utf-8")
|
|
tables = set(re.findall(r"CREATE TABLE `?([A-Za-z0-9_]+)`?", text))
|
|
for path in VERSIONS_DIR.glob("*.py"):
|
|
tables.update(re.findall(r"CREATE TABLE `?([A-Za-z0-9_]+)`?", path.read_text(encoding="utf-8")))
|
|
return tables
|
|
|
|
|
|
def mysql_connection() -> pymysql.Connection:
|
|
from app.core.config import get_settings
|
|
|
|
parsed = urlparse(get_settings().mysql_dsn.replace("mysql+asyncmy://", "mysql+pymysql://"))
|
|
return pymysql.connect(
|
|
host=parsed.hostname or "127.0.0.1",
|
|
port=parsed.port or 3306,
|
|
user=unquote(parsed.username or ""),
|
|
password=unquote(parsed.password or ""),
|
|
database=(parsed.path or "/").lstrip("/"),
|
|
charset="utf8mb4",
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
connection = mysql_connection()
|
|
database = connection.db.decode() if isinstance(connection.db, bytes) else connection.db
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema=%s", (database,))
|
|
actual = {row[0] for row in cursor.fetchall()} - {"alembic_version"}
|
|
expected = expected_tables()
|
|
missing = expected - actual
|
|
unexpected = actual - expected
|
|
if missing or unexpected:
|
|
raise SystemExit(f"table mismatch missing={sorted(missing)} unexpected={sorted(unexpected)}")
|
|
cursor.execute(
|
|
"SELECT table_name, column_name FROM information_schema.columns WHERE table_schema=%s",
|
|
(database,),
|
|
)
|
|
actual_columns: dict[str, set[str]] = {}
|
|
for table, column in cursor.fetchall():
|
|
actual_columns.setdefault(table, set()).add(column)
|
|
for table in sorted(expected):
|
|
if not actual_columns.get(table):
|
|
raise SystemExit(f"missing columns for {table}")
|
|
connection.close()
|
|
print(f"schema audit passed: {len(expected)} business tables, no missing or unexpected tables")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|