From c8cd02c5d3d43bbab462b863db5417b5d69228bb Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Thu, 2 Feb 2023 23:15:01 +0000 Subject: [PATCH] Correctly obtain database URL with SQLAlchemy 2.0 (Fixes #505) --- .../templates/aioflask-multidb/env.py | 14 ++++++++++---- src/flask_migrate/templates/aioflask/env.py | 11 +++++++++-- src/flask_migrate/templates/flask-multidb/env.py | 14 ++++++++++---- src/flask_migrate/templates/flask/env.py | 11 +++++++++-- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/flask_migrate/templates/aioflask-multidb/env.py b/src/flask_migrate/templates/aioflask-multidb/env.py index 891ca93..299f544 100644 --- a/src/flask_migrate/templates/aioflask-multidb/env.py +++ b/src/flask_migrate/templates/aioflask-multidb/env.py @@ -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()) @@ -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 diff --git a/src/flask_migrate/templates/aioflask/env.py b/src/flask_migrate/templates/aioflask/env.py index e4d060f..e33496b 100644 --- a/src/flask_migrate/templates/aioflask/env.py +++ b/src/flask_migrate/templates/aioflask/env.py @@ -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, diff --git a/src/flask_migrate/templates/flask-multidb/env.py b/src/flask_migrate/templates/flask-multidb/env.py index 2556d6d..8f3101f 100644 --- a/src/flask_migrate/templates/flask-multidb/env.py +++ b/src/flask_migrate/templates/flask-multidb/env.py @@ -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()) @@ -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, diff --git a/src/flask_migrate/templates/flask/env.py b/src/flask_migrate/templates/flask/env.py index 0694bc4..89f80b2 100644 --- a/src/flask_migrate/templates/flask/env.py +++ b/src/flask_migrate/templates/flask/env.py @@ -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,