Skip to content

Commit 7cb4236

Browse files
Do not use deprecated functions in Flask-SQLAlchemy 3.0
1 parent f6607fb commit 7cb4236

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

src/flask_migrate/templates/aioflask-multidb/env.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@
2020
fileConfig(config.config_file_name)
2121
logger = logging.getLogger('alembic.env')
2222

23+
24+
def get_engine(bind_key=None):
25+
try:
26+
# this works with Flask-SQLAlchemy<3 and Alchemical
27+
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
28+
except TypeError:
29+
# this works with Flask-SQLAlchemy>=3
30+
return current_app.extensions['migrate'].db.engines.get(bind_key)
31+
32+
2333
# add your model's MetaData object here
2434
# for 'autogenerate' support
2535
# from myapp import mymodel
2636
# target_metadata = mymodel.Base.metadata
2737
config.set_main_option(
28-
'sqlalchemy.url',
29-
str(current_app.extensions['migrate'].db.get_engine().url).replace(
30-
'%', '%%'))
38+
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
3139
bind_names = []
3240
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
3341
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
@@ -39,8 +47,7 @@
3947
for bind in bind_names:
4048
context.config.set_section_option(
4149
bind, "sqlalchemy.url",
42-
str(current_app.extensions['migrate'].db.get_engine(
43-
bind=bind).url).replace('%', '%%'))
50+
str(get_engine(bind_key=bind).url).replace('%', '%%'))
4451
target_db = current_app.extensions['migrate'].db
4552

4653

@@ -166,12 +173,11 @@ async def run_migrations_online():
166173
# for the direct-to-DB use case, start a transaction on all
167174
# engines, then run all migrations, then commit all transactions.
168175
engines = {
169-
'': {'engine': current_app.extensions['migrate'].db.get_engine()}
176+
'': {'engine': get_engine()}
170177
}
171178
for name in bind_names:
172179
engines[name] = rec = {}
173-
rec['engine'] = current_app.extensions['migrate'].db.get_engine(
174-
bind=name)
180+
rec['engine'] = get_engine(bind_key=name)
175181

176182
for name, rec in engines.items():
177183
engine = rec['engine']

src/flask_migrate/templates/aioflask/env.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@
1717
fileConfig(config.config_file_name)
1818
logger = logging.getLogger('alembic.env')
1919

20+
21+
def get_engine():
22+
try:
23+
# this works with Flask-SQLAlchemy<3 and Alchemical
24+
return current_app.extensions['migrate'].db.get_engine()
25+
except TypeError:
26+
# this works with Flask-SQLAlchemy>=3
27+
return current_app.extensions['migrate'].db.engine
28+
29+
2030
# add your model's MetaData object here
2131
# for 'autogenerate' support
2232
# from myapp import mymodel
2333
# target_metadata = mymodel.Base.metadata
2434
config.set_main_option(
25-
'sqlalchemy.url',
26-
str(current_app.extensions['migrate'].db.get_engine().url).replace(
27-
'%', '%%'))
35+
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
2836
target_db = current_app.extensions['migrate'].db
2937

3038
# other values from the config, defined by the needs of env.py,
@@ -90,7 +98,7 @@ async def run_migrations_online():
9098
9199
"""
92100

93-
connectable = current_app.extensions['migrate'].db.get_engine()
101+
connectable = get_engine()
94102

95103
async with connectable.connect() as connection:
96104
await connection.run_sync(do_run_migrations)

src/flask_migrate/templates/flask-multidb/env.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@
2222

2323
def get_engine(bind_key=None):
2424
try:
25-
# this works with Flask-SQLAlchemy>=3
26-
return current_app.extensions['migrate'].db.get_engine(
27-
bind_key=bind_key)
28-
except TypeError:
29-
# this works with Flask-SQLAlchemy<3
25+
# this works with Flask-SQLAlchemy<3 and Alchemical
3026
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
27+
except TypeError:
28+
# this works with Flask-SQLAlchemy>=3
29+
return current_app.extensions['migrate'].db.engines.get(bind_key)
3130

3231

3332
# add your model's MetaData object here
3433
# for 'autogenerate' support
3534
# from myapp import mymodel
3635
# target_metadata = mymodel.Base.metadata
3736
config.set_main_option(
38-
'sqlalchemy.url',
39-
str(current_app.extensions['migrate'].db.get_engine().url).replace(
40-
'%', '%%'))
37+
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
4138
bind_names = []
4239
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
4340
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
@@ -138,7 +135,7 @@ def process_revision_directives(context, revision, directives):
138135
# for the direct-to-DB use case, start a transaction on all
139136
# engines, then run all migrations, then commit all transactions.
140137
engines = {
141-
'': {'engine': current_app.extensions['migrate'].db.get_engine()}
138+
'': {'engine': get_engine()}
142139
}
143140
for name in bind_names:
144141
engines[name] = rec = {}

src/flask_migrate/templates/flask/env.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616
fileConfig(config.config_file_name)
1717
logger = logging.getLogger('alembic.env')
1818

19+
20+
def get_engine():
21+
try:
22+
# this works with Flask-SQLAlchemy<3 and Alchemical
23+
return current_app.extensions['migrate'].db.get_engine()
24+
except TypeError:
25+
# this works with Flask-SQLAlchemy>=3
26+
return current_app.extensions['migrate'].db.engine
27+
28+
1929
# add your model's MetaData object here
2030
# for 'autogenerate' support
2131
# from myapp import mymodel
2232
# target_metadata = mymodel.Base.metadata
2333
config.set_main_option(
24-
'sqlalchemy.url',
25-
str(current_app.extensions['migrate'].db.get_engine().url).replace(
26-
'%', '%%'))
34+
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
2735
target_db = current_app.extensions['migrate'].db
2836

2937
# other values from the config, defined by the needs of env.py,
@@ -77,7 +85,7 @@ def process_revision_directives(context, revision, directives):
7785
directives[:] = []
7886
logger.info('No changes in schema detected.')
7987

80-
connectable = current_app.extensions['migrate'].db.get_engine()
88+
connectable = get_engine()
8189

8290
with connectable.connect() as connection:
8391
context.configure(

0 commit comments

Comments
 (0)