#!/usr/bin/env python3 """Seed an admin user and the initial system prompt. Run once on first deploy: docker compose exec api python scripts/seed_admin.py Creates admin@barclays.com / change_me_password (change immediately). """ import asyncio import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) async def main(): import bcrypt from datetime import datetime, timezone from sqlalchemy import select from app.database import AsyncSessionLocal from app.models.user import User from app.models.system_prompt import SystemPrompt system_prompt_text = (Path(__file__).parent.parent / "app" / "prompts" / "system_prompt.md").read_text() async with AsyncSessionLocal() as db: # Admin user result = await db.execute(select(User).where(User.email == "admin@barclays.com")) if not result.scalar_one_or_none(): hashed_pw = bcrypt.hashpw(b"change_me_password", bcrypt.gensalt()).decode() admin = User( email="admin@barclays.com", hashed_password=hashed_pw, role="admin", created_at=datetime.now(timezone.utc), ) db.add(admin) await db.flush() admin_id = admin.id print("Created admin user: admin@barclays.com / change_me_password") else: result = await db.execute(select(User).where(User.email == "admin@barclays.com")) admin_id = result.scalar_one().id print("Admin user already exists — skipping.") # Initial system prompt result = await db.execute(select(SystemPrompt).where(SystemPrompt.is_active == True)) # noqa: E712 if not result.scalar_one_or_none(): db.add(SystemPrompt( version=1, label="Barclays Display Banner — default", system_text=system_prompt_text, is_active=True, created_by_id=admin_id, created_at=datetime.now(timezone.utc), )) print("Created initial system prompt (active).") else: print("Active system prompt already exists — skipping.") await db.commit() print("Seed complete.") if __name__ == "__main__": asyncio.run(main())