from sqlalchemy import create_engine, text from sqlalchemy.orm import DeclarativeBase, sessionmaker from .config import settings engine = create_engine(settings.database_url, pool_pre_ping=True) SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) class Base(DeclarativeBase): pass def get_db(): """وابستگی FastAPI برای تزریق نشست دیتابیس.""" db = SessionLocal() try: yield db finally: db.close() # جدول‌های مجموعه‌ای که ستون position دارند POSITION_TABLES = [ "services", "projects", "articles", "testimonials", "faqs", "team", "timeline", "stats", "why_us", "investment_models", "process_steps", "offices", "gallery_images", "nav_items", ] def run_light_migrations() -> None: """مهاجرت‌های سبک idempotent — برای دیتابیس‌های موجود که create_all آن‌ها را به‌روز نمی‌کند اجرا می‌شود (seed و startup هر دو آن را صدا می‌زنند).""" if engine.dialect.name != "postgresql": return with engine.begin() as conn: for table in POSITION_TABLES: conn.execute( text(f"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS position INTEGER NOT NULL DEFAULT 0") )