import os import sys import psycopg from .config import settings from .security import hash_password def main() -> None: email = os.getenv("ADMIN_EMAIL", "").strip().lower() password = os.getenv("ADMIN_PASSWORD", "") if not email or not password: raise SystemExit("ADMIN_EMAIL and ADMIN_PASSWORD must be provided through the process environment") if len(password) < 8: raise SystemExit("ADMIN_PASSWORD must be at least 8 characters") with psycopg.connect(settings.database_url) as connection: existing = connection.execute("SELECT id FROM users WHERE email = %s", (email,)).fetchone() if existing: raise SystemExit("admin email already exists; refusing to overwrite") row = connection.execute( """ INSERT INTO users(username, email, password_hash, role, plan, email_verified) VALUES (%s, %s, 'admin', 'free', true) RETURNING id """, (f"admin_{email.split('@', 1)[0]}", email, hash_password(password)), ).fetchone() connection.commit() print(f"created admin {row[0]}") if __name__ == "__main__": main()