78 行
2.7 KiB
Python
78 行
2.7 KiB
Python
|
|
"""PostgreSQL migration smoke entrypoint.
|
||
|
|
|
||
|
|
The script intentionally prints only redacted configuration state. It can be
|
||
|
|
used as a dry-run plan in unit tests, or pointed at a real PG config for a live
|
||
|
|
migration and seed smoke.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||
|
|
if str(REPO_ROOT) not in sys.path:
|
||
|
|
sys.path.insert(0, str(REPO_ROOT))
|
||
|
|
|
||
|
|
from database.db_helper import Database, load_database_settings
|
||
|
|
from database.seed import seed_default_material_categories
|
||
|
|
|
||
|
|
|
||
|
|
def build_pg_smoke_plan(config_path: str | Path = "config.yaml") -> dict[str, Any]:
|
||
|
|
settings = load_database_settings(config_path)
|
||
|
|
pg = settings.postgresql
|
||
|
|
return {
|
||
|
|
"config_path": "***PATH***",
|
||
|
|
"selected_backend": settings.selected_backend,
|
||
|
|
"postgresql_configured": pg.is_configured,
|
||
|
|
"postgresql_enabled": pg.enabled,
|
||
|
|
"connection_mode": "dsn" if pg.dsn else "host",
|
||
|
|
"host_set": bool(pg.host),
|
||
|
|
"database_set": bool(pg.database),
|
||
|
|
"user_set": bool(pg.user),
|
||
|
|
"password_set": bool(pg.password),
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def run_pg_smoke(config_path: str | Path = "config.yaml", *, dry_run: bool = False) -> dict[str, Any]:
|
||
|
|
plan = build_pg_smoke_plan(config_path)
|
||
|
|
if dry_run:
|
||
|
|
return {"ok": True, "dry_run": True, "plan": plan}
|
||
|
|
if plan["selected_backend"] != "postgresql" or not plan["postgresql_configured"]:
|
||
|
|
raise RuntimeError("PostgreSQL smoke requires a configured PostgreSQL backend")
|
||
|
|
database = Database(load_database_settings(config_path))
|
||
|
|
database.initialize()
|
||
|
|
seed_result = seed_default_material_categories(database)
|
||
|
|
conn = database.connect()
|
||
|
|
try:
|
||
|
|
migrations = conn.execute("SELECT COUNT(*) AS total FROM schema_migrations").fetchone()["total"]
|
||
|
|
categories = conn.execute("SELECT COUNT(*) AS total FROM material_categories").fetchone()["total"]
|
||
|
|
finally:
|
||
|
|
conn.close()
|
||
|
|
return {
|
||
|
|
"ok": True,
|
||
|
|
"dry_run": False,
|
||
|
|
"plan": plan,
|
||
|
|
"migrations_applied": int(migrations),
|
||
|
|
"seed": seed_result,
|
||
|
|
"material_categories": int(categories),
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def main(argv: list[str] | None = None) -> int:
|
||
|
|
parser = argparse.ArgumentParser(description="Run ImageStudio PostgreSQL migration smoke.")
|
||
|
|
parser.add_argument("--config", default=os.environ.get("IMAGESTUDIO_CONFIG", "config.yaml"))
|
||
|
|
parser.add_argument("--dry-run", action="store_true")
|
||
|
|
args = parser.parse_args(argv)
|
||
|
|
result = run_pg_smoke(args.config, dry_run=args.dry_run)
|
||
|
|
print(json.dumps(result, ensure_ascii=False, sort_keys=True))
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
raise SystemExit(main())
|