35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
|
|
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(email, password_hash, role, plan, email_verified)
|
||
|
|
VALUES (%s, %s, 'admin', 'free', true)
|
||
|
|
RETURNING id
|
||
|
|
""",
|
||
|
|
(email, hash_password(password)),
|
||
|
|
).fetchone()
|
||
|
|
connection.commit()
|
||
|
|
print(f"created admin {row[0]}")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|