From 4e3ce93ef0df20c32138abaf6e26acffbf3dc7cd Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 16 2017 21:34:25 +0000 Subject: Make alembic use the DB url specified in the configuration file of pagure This avoids providing the URL twice, once for pagure, once for alembic but we remain backward compatible here by still allowing that URL to be present in the alembic.ini file (which then takes precedence). --- diff --git a/alembic/env.py b/alembic/env.py index 712b616..1c11bd2 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,6 +1,9 @@ from __future__ import with_statement + +import os + from alembic import context -from sqlalchemy import engine_from_config, pool +from sqlalchemy import create_engine, pool from logging.config import fileConfig # this is the Alembic Config object, which provides @@ -11,17 +14,37 @@ config = context.config # This line sets up loggers basically. fileConfig(config.config_file_name) + +if 'PAGURE_CONFIG' not in os.environ \ + and os.path.exists('/etc/pagure/pagure.cfg'): + print 'Using configuration file `/etc/pagure/pagure.cfg`' + os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg' + + +try: + import pagure + import pagure.lib.model +except ImportError: + import sys + sys.path.insert(0, '.') + import pagure + import pagure.lib.model + + # add your model's MetaData object here # for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -target_metadata = None +target_metadata = pagure.lib.model.BASE # other values from the config, defined by the needs of env.py, # can be acquired: # my_important_option = config.get_main_option("my_important_option") # ... etc. +DBURL = config.get_main_option("sqlalchemy.url") +if not DBURL: + DBURL = pagure.APP.config['DB_URL'] + + def run_migrations_offline(): """Run migrations in 'offline' mode. @@ -34,12 +57,13 @@ def run_migrations_offline(): script output. """ - url = config.get_main_option("sqlalchemy.url") - context.configure(url=url, target_metadata=target_metadata) + + context.configure(url=DBURL, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() + def run_migrations_online(): """Run migrations in 'online' mode. @@ -47,22 +71,18 @@ def run_migrations_online(): and associate a connection with the context. """ - engine = engine_from_config( - config.get_section(config.config_ini_section), - prefix='sqlalchemy.', - poolclass=pool.NullPool) - - connection = engine.connect() - context.configure( - connection=connection, - target_metadata=target_metadata - ) - - try: + connectable = create_engine(DBURL, poolclass=pool.NullPool) + + with connectable.connect() as connection: + + context.configure( + connection=connection, + target_metadata=target_metadata + ) + with context.begin_transaction(): context.run_migrations() - finally: - connection.close() + if context.is_offline_mode(): run_migrations_offline()