Skip to content

Commit

Permalink
Do not use deprecated functions in Flask-SQLAlchemy 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Nov 17, 2022
1 parent f6607fb commit 7cb4236
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
22 changes: 14 additions & 8 deletions src/flask_migrate/templates/aioflask-multidb/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')


def get_engine(bind_key=None):
try:
# this works with Flask-SQLAlchemy<3 and Alchemical
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
except TypeError:
# this works with Flask-SQLAlchemy>=3
return current_app.extensions['migrate'].db.engines.get(bind_key)


# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
bind_names = []
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
Expand All @@ -39,8 +47,7 @@
for bind in bind_names:
context.config.set_section_option(
bind, "sqlalchemy.url",
str(current_app.extensions['migrate'].db.get_engine(
bind=bind).url).replace('%', '%%'))
str(get_engine(bind_key=bind).url).replace('%', '%%'))
target_db = current_app.extensions['migrate'].db


Expand Down Expand Up @@ -166,12 +173,11 @@ async def run_migrations_online():
# for the direct-to-DB use case, start a transaction on all
# engines, then run all migrations, then commit all transactions.
engines = {
'': {'engine': current_app.extensions['migrate'].db.get_engine()}
'': {'engine': get_engine()}
}
for name in bind_names:
engines[name] = rec = {}
rec['engine'] = current_app.extensions['migrate'].db.get_engine(
bind=name)
rec['engine'] = get_engine(bind_key=name)

for name, rec in engines.items():
engine = rec['engine']
Expand Down
16 changes: 12 additions & 4 deletions src/flask_migrate/templates/aioflask/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')


def get_engine():
try:
# this works with Flask-SQLAlchemy<3 and Alchemical
return current_app.extensions['migrate'].db.get_engine()
except TypeError:
# this works with Flask-SQLAlchemy>=3
return current_app.extensions['migrate'].db.engine


# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
target_db = current_app.extensions['migrate'].db

# other values from the config, defined by the needs of env.py,
Expand Down Expand Up @@ -90,7 +98,7 @@ async def run_migrations_online():
"""

connectable = current_app.extensions['migrate'].db.get_engine()
connectable = get_engine()

async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
Expand Down
15 changes: 6 additions & 9 deletions src/flask_migrate/templates/flask-multidb/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@

def get_engine(bind_key=None):
try:
# this works with Flask-SQLAlchemy>=3
return current_app.extensions['migrate'].db.get_engine(
bind_key=bind_key)
except TypeError:
# this works with Flask-SQLAlchemy<3
# this works with Flask-SQLAlchemy<3 and Alchemical
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
except TypeError:
# this works with Flask-SQLAlchemy>=3
return current_app.extensions['migrate'].db.engines.get(bind_key)


# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
bind_names = []
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
Expand Down Expand Up @@ -138,7 +135,7 @@ def process_revision_directives(context, revision, directives):
# for the direct-to-DB use case, start a transaction on all
# engines, then run all migrations, then commit all transactions.
engines = {
'': {'engine': current_app.extensions['migrate'].db.get_engine()}
'': {'engine': get_engine()}
}
for name in bind_names:
engines[name] = rec = {}
Expand Down
16 changes: 12 additions & 4 deletions src/flask_migrate/templates/flask/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')


def get_engine():
try:
# this works with Flask-SQLAlchemy<3 and Alchemical
return current_app.extensions['migrate'].db.get_engine()
except TypeError:
# this works with Flask-SQLAlchemy>=3
return current_app.extensions['migrate'].db.engine


# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
'sqlalchemy.url', str(get_engine().url).replace('%', '%%'))
target_db = current_app.extensions['migrate'].db

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

connectable = current_app.extensions['migrate'].db.get_engine()
connectable = get_engine()

with connectable.connect() as connection:
context.configure(
Expand Down

0 comments on commit 7cb4236

Please sign in to comment.