Skip to content

Commit

Permalink
improve migration implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
emrgnt-cmplxty committed Oct 22, 2024
1 parent 5932654 commit 084b53b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 105 deletions.
4 changes: 2 additions & 2 deletions py/alembic.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[alembic]
script_location = migrations
sqlalchemy.url = postgresql+asyncpg://postgres:postgres@localhost/postgres
sqlalchemy.url = postgresql://postgres:postgres@localhost:5432/postgres

[loggers]
keys = root,sqlalchemy,alembic
Expand Down Expand Up @@ -34,4 +34,4 @@ formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
datefmt = %H:%M:%
56 changes: 0 additions & 56 deletions py/env.py

This file was deleted.

64 changes: 42 additions & 22 deletions py/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import asyncio
from logging.config import fileConfig

from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config

from alembic import context
from sqlalchemy import engine_from_config, pool

# Import your models/metadata
from your_project.models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
target_metadata = Base.metadata

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
Expand All @@ -29,26 +47,28 @@ def run_migrations_offline() -> None:
with context.begin_transaction():
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 = async_engine_from_config(
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 = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)

await connectable.dispose()
with context.begin_transaction():
context.run_migrations()

def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())

if context.is_offline_mode():
run_migrations_offline()
Expand Down
2 changes: 1 addition & 1 deletion py/migrations/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
Expand Down
24 changes: 0 additions & 24 deletions py/migrations/versions/9f1f30b182ae_3_2_16.py

This file was deleted.

0 comments on commit 084b53b

Please sign in to comment.