1
1
import asyncio
2
2
from logging .config import fileConfig
3
3
4
- from sqlalchemy import engine_from_config
5
4
from sqlalchemy import pool
6
- from sqlalchemy .ext .asyncio import AsyncEngine
7
- from sqlmodel import SQLModel
5
+ from sqlalchemy .engine import Connection
6
+ from sqlalchemy .ext .asyncio import async_engine_from_config
7
+ from sqlmodel import SQLModel # NEW
8
+
8
9
9
10
from alembic import context
10
11
11
- from app .models import Song
12
+ from app .models import Song # NEW
12
13
13
14
# this is the Alembic Config object, which provides
14
15
# access to the values within the .ini file in use.
15
16
config = context .config
16
17
17
18
# Interpret the config file for Python logging.
18
19
# This line sets up loggers basically.
19
- fileConfig (config .config_file_name )
20
+ if config .config_file_name is not None :
21
+ fileConfig (config .config_file_name )
20
22
21
23
# add your model's MetaData object here
22
24
# for 'autogenerate' support
23
25
# from myapp import mymodel
24
26
# target_metadata = mymodel.Base.metadata
25
- target_metadata = SQLModel .metadata
27
+ target_metadata = SQLModel .metadata # UPDATED
26
28
27
29
# other values from the config, defined by the needs of env.py,
28
30
# can be acquired:
29
31
# my_important_option = config.get_main_option("my_important_option")
30
32
# ... etc.
31
33
32
34
33
- def run_migrations_offline ():
35
+ def run_migrations_offline () -> None :
34
36
"""Run migrations in 'offline' mode.
35
37
36
38
This configures the context with just a URL
@@ -54,34 +56,38 @@ def run_migrations_offline():
54
56
context .run_migrations ()
55
57
56
58
57
- def do_run_migrations (connection ) :
59
+ def do_run_migrations (connection : Connection ) -> None :
58
60
context .configure (connection = connection , target_metadata = target_metadata )
59
61
60
62
with context .begin_transaction ():
61
63
context .run_migrations ()
62
64
63
65
64
- async def run_migrations_online ():
65
- """Run migrations in 'online' mode.
66
-
67
- In this scenario we need to create an Engine
66
+ async def run_async_migrations () -> None :
67
+ """In this scenario we need to create an Engine
68
68
and associate a connection with the context.
69
69
70
70
"""
71
- connectable = AsyncEngine (
72
- engine_from_config (
73
- config .get_section (config .config_ini_section ),
74
- prefix = "sqlalchemy." ,
75
- poolclass = pool .NullPool ,
76
- future = True ,
77
- )
71
+
72
+ connectable = async_engine_from_config (
73
+ config .get_section (config .config_ini_section , {}),
74
+ prefix = "sqlalchemy." ,
75
+ poolclass = pool .NullPool ,
78
76
)
79
77
80
78
async with connectable .connect () as connection :
81
79
await connection .run_sync (do_run_migrations )
82
80
81
+ await connectable .dispose ()
82
+
83
+
84
+ def run_migrations_online () -> None :
85
+ """Run migrations in 'online' mode."""
86
+
87
+ asyncio .run (run_async_migrations ())
88
+
83
89
84
90
if context .is_offline_mode ():
85
91
run_migrations_offline ()
86
92
else :
87
- asyncio . run ( run_migrations_online () )
93
+ run_migrations_online ()
0 commit comments