1- import os
1+ import asyncio
22from logging .config import fileConfig
33
4+ from sqlalchemy import pool
5+ from sqlalchemy .engine import Connection
6+ from sqlalchemy .ext .asyncio import async_engine_from_config
7+
48from alembic import context
5- from sqlalchemy import engine_from_config , pool
9+
10+ from app .models import SQLModel # noqa
11+ from app .core .config import settings # noqa
612
713# this is the Alembic Config object, which provides
814# access to the values within the .ini file in use.
915config = context .config
16+ # Override the placeholder URL in alembic.ini with the value from settings.
17+ config .set_main_option ("sqlalchemy.url" , str (settings .SQLALCHEMY_DATABASE_URI ))
1018
1119# Interpret the config file for Python logging.
1220# This line sets up loggers basically.
13- assert config .config_file_name is not None
14- fileConfig (config .config_file_name )
21+ if config .config_file_name is not None :
22+ fileConfig (config .config_file_name )
1523
1624# add your model's MetaData object here
1725# for 'autogenerate' support
1826# from myapp import mymodel
1927# target_metadata = mymodel.Base.metadata
20- # target_metadata = None
21-
22- from app .models import SQLModel # noqa
23- from app .core .config import settings # noqa
24-
2528target_metadata = SQLModel .metadata
2629
2730# other values from the config, defined by the needs of env.py,
3033# ... etc.
3134
3235
33- def get_url ():
34- return str (settings .SQLALCHEMY_DATABASE_URI )
35-
36-
37- def run_migrations_offline ():
36+ def run_migrations_offline () -> None :
3837 """Run migrations in 'offline' mode.
3938
4039 This configures the context with just a URL
@@ -46,38 +45,47 @@ def run_migrations_offline():
4645 script output.
4746
4847 """
49- url = get_url ( )
48+ url = config . get_main_option ( "sqlalchemy.url" )
5049 context .configure (
51- url = url , target_metadata = target_metadata , literal_binds = True , compare_type = True
50+ url = url ,
51+ target_metadata = target_metadata ,
52+ literal_binds = True ,
53+ dialect_opts = {"paramstyle" : "named" },
5254 )
5355
5456 with context .begin_transaction ():
5557 context .run_migrations ()
5658
5759
58- def run_migrations_online ():
59- """Run migrations in 'online' mode.
60+ def do_run_migrations (connection : Connection ) -> None :
61+ context .configure (connection = connection , target_metadata = target_metadata )
62+
63+ with context .begin_transaction ():
64+ context .run_migrations ()
65+
6066
61- In this scenario we need to create an Engine
67+ async def run_async_migrations () -> None :
68+ """In this scenario we need to create an Engine
6269 and associate a connection with the context.
6370
6471 """
65- configuration = config .get_section (config .config_ini_section )
66- assert configuration is not None
67- configuration ["sqlalchemy.url" ] = get_url ()
68- connectable = engine_from_config (
69- configuration ,
72+
73+ connectable = async_engine_from_config (
74+ config .get_section (config .config_ini_section , {}),
7075 prefix = "sqlalchemy." ,
7176 poolclass = pool .NullPool ,
7277 )
7378
74- with connectable .connect () as connection :
75- context .configure (
76- connection = connection , target_metadata = target_metadata , compare_type = True
77- )
79+ async with connectable .connect () as connection :
80+ await connection .run_sync (do_run_migrations )
81+
82+ await connectable .dispose ()
83+
84+
85+ def run_migrations_online () -> None :
86+ """Run migrations in 'online' mode."""
7887
79- with context .begin_transaction ():
80- context .run_migrations ()
88+ asyncio .run (run_async_migrations ())
8189
8290
8391if context .is_offline_mode ():
0 commit comments