Skip to content

Commit

Permalink
chore: prevent create_all failures from stopping application startup (
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Apr 1, 2024
1 parent 64be0ba commit 18b0031
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 0 additions & 1 deletion advanced_alchemy/extensions/litestar/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def database_group() -> None:
@option("--verbose", type=bool, help="Enable verbose output.", default=False, is_flag=True)
def show_database_revision(app: Litestar, verbose: bool) -> None:
"""Show current database revision."""

from advanced_alchemy.extensions.litestar.alembic import AlembicCommands

console.rule("[yellow]Listing current revision[/]", align="left")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Callable, cast

from litestar.cli._utils import console
from litestar.constants import HTTP_RESPONSE_START
from sqlalchemy.exc import OperationalError
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession

from advanced_alchemy.config.asyncio import SQLAlchemyAsyncConfig as _SQLAlchemyAsyncConfig
Expand Down Expand Up @@ -211,7 +213,10 @@ async def create_all_metadata(self, app: Litestar) -> None:
app (Litestar): The ``Litestar`` instance
"""
async with self.get_engine().begin() as conn:
await conn.run_sync(self.alembic_config.target_metadata.create_all)
try:
await conn.run_sync(self.alembic_config.target_metadata.create_all)
except OperationalError as exc:
console.print(f"[bold red] * Could not create target metadata. Reason: {exc}")

def create_app_state_items(self) -> dict[str, Any]:
"""Key/value pairs to be stored in application state."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, AsyncGenerator, Callable, cast

from litestar.cli._utils import console
from litestar.constants import HTTP_RESPONSE_START
from sqlalchemy import Engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import Session

from advanced_alchemy.config.sync import SQLAlchemySyncConfig as _SQLAlchemySyncConfig
Expand Down Expand Up @@ -208,7 +210,10 @@ def create_all_metadata(self, app: Litestar) -> None:
app (Litestar): The ``Litestar`` instance
"""
with self.get_engine().begin() as conn:
self.alembic_config.target_metadata.create_all(bind=conn)
try:
self.alembic_config.target_metadata.create_all(bind=conn)
except OperationalError as exc:
console.print(f"[bold red] * Could not create target metadata. Reason: {exc}")

def create_app_state_items(self) -> dict[str, Any]:
"""Key/value pairs to be stored in application state."""
Expand Down

0 comments on commit 18b0031

Please sign in to comment.