Spaces:
Sleeping
Sleeping
File size: 1,374 Bytes
cdc55f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | """
Create or update an admin user in the database.
Usage:
py scripts/seed_admin.py --email admin@example.com --password MyPass!
If a user with that email already exists their password and role are updated.
If no user exists a new one is created with role=admin and is_active=True.
Requires DATABASE_URL to be set in .env (loaded automatically).
"""
import argparse
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.config import settings
from app.models.db import User, UserRole, get_engine
from app.security import hash_password
from sqlmodel import Session, select
def seed(email: str, password: str):
engine = get_engine()
with Session(engine) as db:
existing = db.exec(select(User).where(User.email == email)).first()
if existing:
print(f"User {email} already exists with role {existing.role}")
return
user = User(
email=email,
hashed_password=hash_password(password),
role=UserRole.admin,
)
db.add(user)
db.commit()
print(f"Admin user created: {email}")
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("--email", required=True)
p.add_argument("--password", required=True)
args = p.parse_args()
seed(args.email, args.password)
|