|
1 | 1 | """Database initialization script. |
2 | 2 |
|
3 | | -Checks if an initial superuser should be created and creates it if needed. |
| 3 | +Checks if an initial superuser and a demo user should be created. |
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import asyncio |
|
19 | 19 | logger = logging.getLogger(__name__) |
20 | 20 |
|
21 | 21 |
|
22 | | -async def init_db() -> None: |
23 | | - """Create the initial superuser if configured.""" |
24 | | - if ( |
25 | | - not settings.first_superuser_username |
26 | | - or not settings.first_superuser_password |
27 | | - ): |
28 | | - logger.info( |
29 | | - "First superuser credentials not configured. Skipping initialization." |
| 22 | +async def create_user_if_not_exists( |
| 23 | + session: AsyncSession, |
| 24 | + username: str, |
| 25 | + password: str, |
| 26 | + is_demo: bool = False, |
| 27 | + description: str = "user", |
| 28 | +) -> None: |
| 29 | + """Create a user if they don't already exist in the database.""" |
| 30 | + statement = select(User).where(User.username == username) |
| 31 | + result = await session.exec(statement) |
| 32 | + user = result.first() |
| 33 | + |
| 34 | + if user: |
| 35 | + logger.info("%s '%s' already exists. Skipping.", description, username) |
| 36 | + else: |
| 37 | + logger.info("Creating %s '%s'.", description, username) |
| 38 | + user_in = UserIn( |
| 39 | + username=username, |
| 40 | + plain_password=password, |
30 | 41 | ) |
31 | | - return |
| 42 | + new_user = User( |
| 43 | + **user_in.model_dump(exclude={"plain_password"}), is_demo=is_demo |
| 44 | + ) |
| 45 | + new_user.hashed_password = get_password_hash(user_in.plain_password) |
| 46 | + session.add(new_user) |
| 47 | + await session.commit() |
| 48 | + logger.info("%s '%s' created successfully.", description, username) |
| 49 | + |
32 | 50 |
|
| 51 | +async def init_db() -> None: |
| 52 | + """Initialize the database with default users.""" |
33 | 53 | engine = create_async_engine(settings.database_url) |
34 | 54 |
|
35 | 55 | async with AsyncSession(engine) as session: |
36 | | - statement = select(User).where( |
37 | | - User.username == settings.first_superuser_username |
38 | | - ) |
39 | | - result = await session.exec(statement) |
40 | | - user = result.first() |
41 | | - |
42 | | - if user: |
43 | | - logger.info("Superuser already exists. Skipping creation.") |
44 | | - else: |
45 | | - logger.info("Creating initial superuser.") |
46 | | - user_in = UserIn( |
47 | | - username=settings.first_superuser_username, |
48 | | - plain_password=settings.first_superuser_password, |
| 56 | + if ( |
| 57 | + settings.first_superuser_username |
| 58 | + and settings.first_superuser_password |
| 59 | + ): |
| 60 | + await create_user_if_not_exists( |
| 61 | + session, |
| 62 | + settings.first_superuser_username, |
| 63 | + settings.first_superuser_password, |
| 64 | + is_demo=False, |
| 65 | + description="Superuser", |
49 | 66 | ) |
50 | | - new_user = User(**user_in.model_dump(exclude={"plain_password"})) |
51 | | - new_user.hashed_password = get_password_hash( |
52 | | - user_in.plain_password |
| 67 | + |
| 68 | + if settings.demo_user_username and settings.demo_user_password: |
| 69 | + await create_user_if_not_exists( |
| 70 | + session, |
| 71 | + settings.demo_user_username, |
| 72 | + settings.demo_user_password, |
| 73 | + is_demo=True, |
| 74 | + description="Demo user", |
53 | 75 | ) |
54 | | - session.add(new_user) |
55 | | - await session.commit() |
56 | | - logger.info("Initial superuser created successfully.") |
57 | 76 |
|
58 | 77 | await engine.dispose() |
59 | 78 |
|
|
0 commit comments