-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_admin.py
More file actions
47 lines (40 loc) · 1.57 KB
/
Copy pathseed_admin.py
File metadata and controls
47 lines (40 loc) · 1.57 KB
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
#!/usr/bin/env python
import asyncio
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
from passlib.context import CryptContext
# Password hashing context (matching backend)
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
async def seed_admin():
# Import models after ensuring DB connection
from app.models.user import User
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
print("ERROR: DATABASE_URL not set")
return
engine = create_async_engine(DATABASE_URL, echo=False)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
try:
async with async_session() as session:
# Check if admin exists
stmt = select(User).where(User.email == "admin@sentinel.ai")
result = await session.execute(stmt)
existing = result.scalar_one_or_none()
if existing:
print("✓ Admin user already exists")
else:
admin = User(
email="admin@sentinel.ai",
hashed_password=pwd_context.hash("admin123"),
is_active=True,
is_superuser=True
)
session.add(admin)
await session.commit()
print("✓ Admin user created: admin@sentinel.ai / admin123")
finally:
await engine.dispose()
if __name__ == "__main__":
asyncio.run(seed_admin())