Skip to content

Commit

Permalink
Correctly obtain database URL with SQLAlchemy 2.0 (Fixes #505)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Feb 2, 2023
1 parent 828c65f commit c8cd02c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/flask_migrate/templates/aioflask-multidb/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ def get_engine(bind_key=None):
return current_app.extensions['migrate'].db.engines.get(bind_key)


def get_engine_url(bind_key=None):
try:
return get_engine(bind_key).url.render_as_string(
hide_password=False).replace('%', '%%')
except AttributeError:
return str(get_engine(bind_key).url).replace('%', '%%')


# 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(get_engine().url).replace('%', '%%'))
config.set_main_option('sqlalchemy.url', get_engine_url())
bind_names = []
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
Expand All @@ -44,8 +51,7 @@ def get_engine(bind_key=None):
bind_names = get_bind_names()
for bind in bind_names:
context.config.set_section_option(
bind, "sqlalchemy.url",
str(get_engine(bind_key=bind).url).replace('%', '%%'))
bind, "sqlalchemy.url", get_engine_url(bind_key=bind))
target_db = current_app.extensions['migrate'].db


Expand Down
11 changes: 9 additions & 2 deletions src/flask_migrate/templates/aioflask/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ def get_engine():
return current_app.extensions['migrate'].db.engine


def get_engine_url():
try:
return get_engine().url.render_as_string(hide_password=False).replace(
'%', '%%')
except AttributeError:
return str(get_engine().url).replace('%', '%%')


# 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(get_engine().url).replace('%', '%%'))
config.set_main_option('sqlalchemy.url', get_engine_url())
target_db = current_app.extensions['migrate'].db

# other values from the config, defined by the needs of env.py,
Expand Down
14 changes: 10 additions & 4 deletions src/flask_migrate/templates/flask-multidb/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ def get_engine(bind_key=None):
return current_app.extensions['migrate'].db.engines.get(bind_key)


def get_engine_url(bind_key=None):
try:
return get_engine(bind_key).url.render_as_string(
hide_password=False).replace('%', '%%')
except AttributeError:
return str(get_engine(bind_key).url).replace('%', '%%')


# 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(get_engine().url).replace('%', '%%'))
config.set_main_option('sqlalchemy.url', get_engine_url())
bind_names = []
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
Expand All @@ -43,8 +50,7 @@ def get_engine(bind_key=None):
bind_names = get_bind_names()
for bind in bind_names:
context.config.set_section_option(
bind, "sqlalchemy.url",
str(get_engine(bind_key=bind).url).replace('%', '%%'))
bind, "sqlalchemy.url", get_engine_url(bind_key=bind))
target_db = current_app.extensions['migrate'].db

# other values from the config, defined by the needs of env.py,
Expand Down
11 changes: 9 additions & 2 deletions src/flask_migrate/templates/flask/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ def get_engine():
return current_app.extensions['migrate'].db.engine


def get_engine_url():
try:
return get_engine().url.render_as_string(hide_password=False).replace(
'%', '%%')
except AttributeError:
return str(get_engine().url).replace('%', '%%')


# 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(get_engine().url).replace('%', '%%'))
config.set_main_option('sqlalchemy.url', get_engine_url())
target_db = current_app.extensions['migrate'].db

# other values from the config, defined by the needs of env.py,
Expand Down

0 comments on commit c8cd02c

Please sign in to comment.