File size: 3,208 Bytes
2d0d464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Seed a fresh dev database with a usable set of users.

Pre-prod data is fake; after the JSONB→relational cutover the schema is built from
the SQLAlchemy models on startup (Base.metadata.create_all). This script inserts a
handful of approved users so the app is exercisable without going through Supabase
sign-up for every role.

NOTE: these users have RANDOM uuids (not real Supabase identities). They're for
backend smoke tests / local poking. Real users are provisioned just-in-time on
their first authenticated request (auth.py), keyed on their Supabase auth.uid().

Usage (from backend/, with env loaded):
    python scripts/seed_dev.py
    python scripts/seed_dev.py --wipe    # delete existing seed rows first
"""

import argparse
import asyncio
import sys
import uuid
from datetime import UTC, datetime
from pathlib import Path

BACKEND_ROOT = Path(__file__).resolve().parent.parent
if str(BACKEND_ROOT) not in sys.path:
    sys.path.insert(0, str(BACKEND_ROOT))

from sqlalchemy import delete, select

from app.database import close_db, connect_db, get_session
from app.models import User

SEED_USERS = [
    {"email": "admin@accujuris.dev", "name": "Dev Admin", "role": "admin"},
    {
        "email": "translator@accujuris.dev",
        "name": "Dev Translator",
        "role": "translator",
    },
    {
        "email": "legal@accujuris.dev",
        "name": "Dev Legal Expert",
        "role": "legal_expert",
    },
    {"email": "submitter@accujuris.dev", "name": "Dev Submitter", "role": "submitter"},
]


async def _run(wipe: bool) -> int:
    await connect_db()
    try:
        async with get_session() as session:
            if wipe:
                emails = [u["email"] for u in SEED_USERS]
                await session.execute(delete(User).where(User.email.in_(emails)))

            for spec in SEED_USERS:
                existing = (
                    await session.execute(
                        select(User).where(User.email == spec["email"])
                    )
                ).scalar_one_or_none()
                if existing:
                    print(f"  exists: {spec['email']} ({existing.role})")
                    continue
                uid = uuid.uuid4()
                session.add(
                    User(
                        id=uid,
                        supabase_uid=str(uid),
                        email=spec["email"],
                        name=spec["name"],
                        role=spec["role"],
                        is_approved=True,
                        is_permanent=(spec["role"] != "submitter"),
                        free_submissions_left=2,
                        created_at=datetime.now(UTC),
                    )
                )
                print(f"  seeded: {spec['email']} ({spec['role']}) id={uid}")
        print("Seed complete.")
        return 0
    finally:
        await close_db()


def main() -> None:
    parser = argparse.ArgumentParser(description="Seed dev users.")
    parser.add_argument(
        "--wipe", action="store_true", help="delete existing seed rows first"
    )
    args = parser.parse_args()
    sys.exit(asyncio.run(_run(args.wipe)))


if __name__ == "__main__":
    main()