袁聪的第一次提交,包含nl2sql,行情数据,场外申购
This commit is contained in:
+31
-12
@@ -1,38 +1,57 @@
|
||||
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")
|
||||
return set(re.findall(r"CREATE TABLE `?([A-Za-z0-9_]+)`?", text))
|
||||
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 = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456", database="jr")
|
||||
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", ("jr",))
|
||||
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() | {
|
||||
"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",
|
||||
}
|
||||
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", ("jr",))
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user