""" Seed content_sections table from data/content.json. Run from the backend/ directory: python seed_content.py """ import asyncio import json from pathlib import Path from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession from app.config import settings from app.models import Base, ContentSection CONTENT_FILE = Path(__file__).parent / "data" / "content.json" def build_sections(content: dict) -> dict: pages = content.get("pages", {}) return { "siteSettings": content.get("siteSettings", {}), "navigation": content.get("navigation", {}), "footer": content.get("footer", {}), "testimonials": content.get("testimonials", []), "pages.home": pages.get("home", {}), "pages.packWalks": pages.get("packWalks", {}), "pages.oneOnOneWalks": pages.get("oneOnOneWalks", {}), "pages.puppyVisits": pages.get("puppyVisits", {}), "pages.pricing": pages.get("pricing", {}), "pages.about": pages.get("about", {}), "pages.contact": pages.get("contact", {}), } async def seed(): if not CONTENT_FILE.exists(): print(f"Content file not found: {CONTENT_FILE}") return with open(CONTENT_FILE, encoding="utf-8") as f: content = json.load(f) sections = build_sections(content) engine = create_async_engine(settings.DATABASE_URL, echo=False) async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) Session = async_sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False) async with Session() as session: for key, data in sections.items(): from sqlalchemy import select result = await session.execute( select(ContentSection).where(ContentSection.key == key) ) row = result.scalar_one_or_none() if row: row.data = data print(f" updated: {key}") else: session.add(ContentSection(key=key, data=data)) print(f" inserted: {key}") await session.commit() await engine.dispose() print(f"\nSeeded {len(sections)} sections from {CONTENT_FILE}") if __name__ == "__main__": asyncio.run(seed())