|
13 | 13 | from sqlalchemy.exc import SQLAlchemyError |
14 | 14 |
|
15 | 15 | from app.database.db import async_session |
| 16 | +from app.database.helpers import is_database_initialized |
16 | 17 | from app.managers.user import UserManager |
17 | 18 | from app.models.enums import RoleType |
18 | 19 | from app.models.user import User |
|
21 | 22 | if TYPE_CHECKING: # pragma: no cover |
22 | 23 | from collections.abc import Sequence |
23 | 24 |
|
| 25 | + from sqlalchemy.ext.asyncio import AsyncSession |
| 26 | + |
24 | 27 |
|
25 | 28 | app = typer.Typer(no_args_is_help=True) |
26 | 29 |
|
27 | 30 |
|
| 31 | +async def check_db_initialized(session: AsyncSession) -> None: |
| 32 | + """Check if database is initialized and exit if not. |
| 33 | +
|
| 34 | + Args: |
| 35 | + session: The database session to check |
| 36 | +
|
| 37 | + Raises: |
| 38 | + typer.Exit: If database is not initialized |
| 39 | + """ |
| 40 | + if not await is_database_initialized(session): |
| 41 | + rprint( |
| 42 | + "\n[red]-> ERROR: Database has not been initialized.\n" |
| 43 | + "[yellow]Please run [bold]'api-admin db init'[/bold] " |
| 44 | + "to initialize the database first.\n" |
| 45 | + ) |
| 46 | + raise typer.Exit(1) |
| 47 | + |
| 48 | + |
28 | 49 | def show_table(title: str, user_list: Sequence[User]) -> None: |
29 | 50 | """Show User data in a tabulated format.""" |
30 | 51 | console = Console() |
@@ -111,6 +132,7 @@ async def _create_user(user_data: dict[str, str | RoleType]) -> None: |
111 | 132 | """Async function to create a new user.""" |
112 | 133 | try: |
113 | 134 | async with async_session() as session: |
| 135 | + await check_db_initialized(session) |
114 | 136 | await UserManager.register(user_data, session) |
115 | 137 | await session.commit() |
116 | 138 |
|
@@ -151,6 +173,7 @@ async def _list_users() -> Sequence[User]: |
151 | 173 | """Async function to list all users in the database.""" |
152 | 174 | try: |
153 | 175 | async with async_session() as session: |
| 176 | + await check_db_initialized(session) |
154 | 177 | user_list = await UserManager.get_all_users(session) |
155 | 178 |
|
156 | 179 | except SQLAlchemyError as exc: |
@@ -180,6 +203,7 @@ async def _show_user() -> User: |
180 | 203 | """Async function to show details for a single user.""" |
181 | 204 | try: |
182 | 205 | async with async_session() as session: |
| 206 | + await check_db_initialized(session) |
183 | 207 | user = await UserManager.get_user_by_id(user_id, session) |
184 | 208 | except HTTPException as exc: |
185 | 209 | rprint( |
@@ -208,6 +232,7 @@ async def _verify_user(user_id: int) -> User | None: |
208 | 232 | """Async function to verify a user by id.""" |
209 | 233 | try: |
210 | 234 | async with async_session() as session: |
| 235 | + await check_db_initialized(session) |
211 | 236 | user = await session.get(User, user_id) |
212 | 237 | if user: |
213 | 238 | user.verified = True |
@@ -249,6 +274,7 @@ async def _ban_user(user_id: int, *, unban: bool | None) -> User | None: |
249 | 274 | """Async function to ban or unban a user.""" |
250 | 275 | try: |
251 | 276 | async with async_session() as session: |
| 277 | + await check_db_initialized(session) |
252 | 278 | user = await session.get(User, user_id) |
253 | 279 | if user: |
254 | 280 | user.banned = not unban |
@@ -296,6 +322,7 @@ async def _toggle_admin( |
296 | 322 | """Async function to toggle admin status for a user.""" |
297 | 323 | try: |
298 | 324 | async with async_session() as session: |
| 325 | + await check_db_initialized(session) |
299 | 326 | user = await session.get(User, user_id) |
300 | 327 | if user: |
301 | 328 | user.role = RoleType.user if remove else RoleType.admin |
@@ -333,6 +360,7 @@ async def _delete_user(user_id: int) -> User | None: |
333 | 360 | """Async function to delete a user.""" |
334 | 361 | try: |
335 | 362 | async with async_session() as session: |
| 363 | + await check_db_initialized(session) |
336 | 364 | user = await session.get(User, user_id) |
337 | 365 | if user: |
338 | 366 | await session.delete(user) |
@@ -389,6 +417,7 @@ async def _search_users() -> list[User]: |
389 | 417 | """Async function to search for users.""" |
390 | 418 | try: |
391 | 419 | async with async_session() as session: |
| 420 | + await check_db_initialized(session) |
392 | 421 | query = await UserManager.search_users( |
393 | 422 | search_term, field_enum, exact_match=exact |
394 | 423 | ) |
|
0 commit comments