1
1
#!/usr/bin/env python3
2
2
# -*- coding: utf-8 -*-
3
- # ruff: noqa: E402, F403 , I001, RUF100
3
+ # ruff: noqa: F403, F401 , I001, RUF100
4
4
import asyncio
5
5
import os
6
6
import sys
7
-
8
7
from logging .config import fileConfig
9
8
10
9
from alembic import context
11
- from sqlalchemy import engine_from_config , pool
12
- from sqlalchemy .ext .asyncio import AsyncEngine
10
+ from sqlalchemy import pool
11
+ from sqlalchemy .engine import Connection
12
+ from sqlalchemy .ext .asyncio import async_engine_from_config
13
13
14
14
sys .path .append ('../' )
15
15
16
+ from backend .common .model import MappedBase
16
17
from backend .core import path_conf
18
+ from backend .database .db_mysql import SQLALCHEMY_DATABASE_URL
19
+
20
+ # import your new model here
21
+ from backend .app .admin .model import * # noqa: F401
22
+ from backend .app .generator .model import * # noqa: F401
17
23
18
- if not os .path .exists (path_conf .ALEMBIC_Versions_DIR ):
19
- os .makedirs (path_conf .ALEMBIC_Versions_DIR )
24
+ if not os .path .exists (path_conf .ALEMBIC_VERSION_DIR ):
25
+ os .makedirs (path_conf .ALEMBIC_VERSION_DIR )
20
26
21
27
# this is the Alembic Config object, which provides
22
28
# access to the values within the .ini file in use.
23
- config = context .config
29
+ alembic_config = context .config
24
30
25
31
# Interpret the config file for Python logging.
26
32
# This line sets up loggers basically.
27
- fileConfig (config .config_file_name )
33
+ if alembic_config .config_file_name is not None :
34
+ fileConfig (alembic_config .config_file_name )
28
35
29
- # add your model's MetaData object here
36
+ # model's MetaData object
30
37
# for 'autogenerate' support
31
- from backend .common .model import MappedBase
32
-
33
- # if add new app, do like this
34
- from backend .app .admin .model import * # noqa: F401
35
- from backend .app .generator .model import * # noqa: F401
36
-
37
38
target_metadata = MappedBase .metadata
38
39
39
40
# other values from the config, defined by the needs of env.py,
40
- from backend .database .db_mysql import SQLALCHEMY_DATABASE_URL
41
-
42
- config .set_main_option ('sqlalchemy.url' , SQLALCHEMY_DATABASE_URL )
41
+ alembic_config .set_main_option ('sqlalchemy.url' , SQLALCHEMY_DATABASE_URL )
43
42
44
43
45
44
def run_migrations_offline ():
@@ -54,49 +53,68 @@ def run_migrations_offline():
54
53
script output.
55
54
56
55
"""
57
- url = config .get_main_option ('sqlalchemy.url' )
56
+ url = alembic_config .get_main_option ('sqlalchemy.url' )
58
57
context .configure (
59
58
url = url ,
60
59
target_metadata = target_metadata ,
61
60
literal_binds = True ,
62
61
dialect_opts = {'paramstyle' : 'named' },
62
+ compare_type = True ,
63
+ compare_server_default = True ,
64
+ transaction_per_migration = True ,
63
65
)
64
66
65
67
with context .begin_transaction ():
66
68
context .run_migrations ()
67
69
68
70
69
- def do_run_migrations (connection ):
71
+ def do_run_migrations (connection : Connection ) -> None :
72
+ # 当迁移无变化时,不生成迁移记录
73
+ def process_revision_directives (context , revision , directives ):
74
+ if alembic_config .cmd_opts .autogenerate :
75
+ script = directives [0 ]
76
+ if script .upgrade_ops .is_empty ():
77
+ directives [:] = []
78
+ print ('\n No changes in model detected' )
79
+
70
80
context .configure (
71
81
connection = connection ,
72
82
target_metadata = target_metadata ,
83
+ compare_type = True ,
84
+ compare_server_default = True ,
85
+ transaction_per_migration = True ,
86
+ process_revision_directives = process_revision_directives ,
73
87
)
74
88
75
89
with context .begin_transaction ():
76
90
context .run_migrations ()
77
91
78
92
79
- async def run_migrations_online ():
80
- """Run migrations in 'online' mode.
81
-
82
- In this scenario we need to create an Engine
93
+ async def run_async_migrations () -> None :
94
+ """In this scenario we need to create an Engine
83
95
and associate a connection with the context.
84
96
85
97
"""
86
- connectable = AsyncEngine (
87
- engine_from_config (
88
- config .get_section (config .config_ini_section ),
89
- prefix = 'sqlalchemy.' ,
90
- poolclass = pool .NullPool ,
91
- future = True ,
92
- )
98
+
99
+ connectable = async_engine_from_config (
100
+ alembic_config .get_section (alembic_config .config_ini_section , {}),
101
+ prefix = 'sqlalchemy.' ,
102
+ poolclass = pool .NullPool ,
93
103
)
94
104
95
105
async with connectable .connect () as connection :
96
106
await connection .run_sync (do_run_migrations )
97
107
108
+ await connectable .dispose ()
109
+
110
+
111
+ def run_migrations_online () -> None :
112
+ """Run migrations in 'online' mode."""
113
+
114
+ asyncio .run (run_async_migrations ())
115
+
98
116
99
117
if context .is_offline_mode ():
100
118
run_migrations_offline ()
101
119
else :
102
- asyncio . run ( run_migrations_online () )
120
+ run_migrations_online ()
0 commit comments