Skip to content

Commit

Permalink
Merge pull request #51 from wafflestudio/alembic_async
Browse files Browse the repository at this point in the history
Alembic async
  • Loading branch information
morecleverer authored Feb 1, 2025
2 parents e3c1b68 + 1a3ccb7 commit b86178c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ python = "^3.11"
fastapi = "^0.115.0"
uvicorn = "^0.30.6"
sqlalchemy = "^2.0.35"
pymysql = "^1.1.1"
alembic = "^1.13.3"
email-validator = "^2.2.0"
pydantic-settings = "^2.5.2"
Expand Down
33 changes: 21 additions & 12 deletions snuvote/database/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from logging.config import fileConfig

from sqlalchemy import create_engine, engine_from_config
from sqlalchemy import pool
import asyncio
from sqlalchemy import Connection, pool
from sqlalchemy.ext.asyncio import create_async_engine


from alembic import context

from snuvote.database.common import Base
from snuvote.database.settings import DB_ALEMBIC_SETTINGS
from snuvote.database.settings import DB_SETTINGS

# TODO 왜 사용되지 않는 모듈을 import 하는 걸까요? 한 번 생각해보세요.
import snuvote.database.models # noqa: F401
Expand Down Expand Up @@ -45,7 +47,7 @@ def run_migrations_offline() -> None:
"""
context.configure(
url=DB_ALEMBIC_SETTINGS.url,
url=DB_SETTINGS.url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
Expand All @@ -55,22 +57,29 @@ def run_migrations_offline() -> None:
context.run_migrations()


def do_run_migrations(connection: Connection) -> None:
context.configure(
connection=connection,
target_metadata=target_metadata
)

with context.begin_transaction():
context.run_migrations()

async def run_async_migrations() -> None:
connectable = create_async_engine(DB_SETTINGS.url, poolclass=pool.NullPool)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)

def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = create_engine(DB_ALEMBIC_SETTINGS.url, poolclass=pool.NullPool)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)

with context.begin_transaction():
context.run_migrations()
asyncio.run(run_async_migrations())


if context.is_offline_mode():
Expand Down
8 changes: 0 additions & 8 deletions snuvote/database/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ class DatabaseSettings(BaseSettings):
user: str = ""
password: str = ""
database: str = ""
alembic_prefix: str | None = None

def __init__(self, prefix: str|None = None):
super().__init__()
self.alembic_prefix = prefix

@property
def url(self) -> str:
if self.alembic_prefix == "alembic":
return f"mysql+pymysql://admin:{self.password}@{self.host}:{self.port}/{self.database}"
return f"mysql+aiomysql://admin:{self.password}@{self.host}:{self.port}/{self.database}"

model_config = SettingsConfigDict(
Expand All @@ -31,4 +24,3 @@ def url(self) -> str:


DB_SETTINGS = DatabaseSettings()
DB_ALEMBIC_SETTINGS = DatabaseSettings('alembic')

0 comments on commit b86178c

Please sign in to comment.