袁聪merge:合并分支

This commit is contained in:
2026-09-11 17:26:18 +08:00
95 changed files with 18208 additions and 120 deletions
+49 -53
View File
@@ -13,73 +13,69 @@ from __future__ import annotations
import re
import sys
from pathlib import Path
from urllib.parse import unquote, urlparse
from sqlalchemy import create_engine, text
import pymysql
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from app.core.config import get_settings # noqa: E402
SQL_FILE = ROOT / "alembic" / "baseline_generated.sql"
# baseline_generated.sql 只覆盖基线表;以下平台增量表由 Alembic 迁移创建。
INCREMENTAL_TABLES = {
"agent_run",
"domain_event_outbox",
"request_idempotency",
"config_release",
"platform_config_item",
"model_endpoint_config",
"model_routing_rule",
"model_routing_fallback",
"prompt_template_version",
"outbox_delivery",
"svc_conversation_session",
"api_request_receipt",
}
VERSIONS_DIR = ROOT / "alembic" / "versions"
def expected_tables() -> set[str]:
content = SQL_FILE.read_text(encoding="utf-8")
return set(re.findall(r"CREATE TABLE `?([A-Za-z0-9_]+)`?", content))
text = SQL_FILE.read_text(encoding="utf-8")
tables = set(re.findall(
r"CREATE TABLE\s+(?:IF NOT EXISTS\s+)?`?([A-Za-z0-9_]+)`?",
text,
flags=re.IGNORECASE,
))
for path in VERSIONS_DIR.glob("*.py"):
tables.update(re.findall(
r"CREATE TABLE\s+(?:IF NOT EXISTS\s+)?`?([A-Za-z0-9_]+)`?",
path.read_text(encoding="utf-8"),
flags=re.IGNORECASE,
))
return tables
def main() -> int:
engine = create_engine(get_settings().mysql_dsn.replace("mysql+asyncmy", "mysql+pymysql"))
with engine.connect() as connection:
actual = set(
connection.execute(
text(
"SELECT TABLE_NAME FROM information_schema.TABLES "
"WHERE TABLE_SCHEMA = DATABASE()"
)
)
.scalars()
.all()
) - {"alembic_version"}
expected = expected_tables() | INCREMENTAL_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:
print(f"schema audit FAILED: missing={sorted(missing)} unexpected={sorted(unexpected)}")
return 1
rows = connection.execute(
text(
"SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.COLUMNS "
"WHERE TABLE_SCHEMA = DATABASE()"
)
).all()
engine.dispose()
column_counts: dict[str, int] = {}
for table, _column in rows:
column_counts[table] = column_counts.get(table, 0) + 1
without_columns = sorted(table for table in expected if not column_counts.get(table))
if without_columns:
print(f"schema audit FAILED: tables without columns: {without_columns}")
return 1
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")
return 0