Skip to content

Commit

Permalink
Merge pull request #37 from romanchyla/alembic
Browse files Browse the repository at this point in the history
Making alembic read config from the local_config.py
  • Loading branch information
spacemansteve authored Jul 13, 2017
2 parents 7dbad7f + 28ebd1f commit 134740d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions adsdata/metricsUpdater/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import absolute_import, unicode_literals

import sys, os
sys.path.append("..")
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import re
from kombu import Queue
from celery.signals import worker_process_init, worker_process_shutdown, user_preload_options
Expand All @@ -11,7 +11,7 @@
from metrics import Metrics
from utils import process_rows

app = app_module.MetricsUpdaterCelery('metrics-updater')
app = app_module.MetricsUpdaterCelery('metrics-updater', proj_home=os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
logger = app.logger
metrics_rds = None

Expand Down
3 changes: 2 additions & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ script_location = alembic
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = postgresql://postgres:pwd@localhost:5432/postgres
use_flask_db_url = true
sqlalchemy.url = 'intentionally wrong'


# Logging configuration
Expand Down
46 changes: 32 additions & 14 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
import os
import sys

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -22,7 +24,6 @@
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.
Expand All @@ -35,13 +36,21 @@ def run_migrations_offline():
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True)
url = get_app_config('SQLALCHEMY_URL', config.get_main_option("sqlalchemy.url"))
context.configure(url=url, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()

def get_app_config(key):
opath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if opath not in sys.path:
sys.path.insert(0, opath)

from adsdata.metricsUpdater.tasks import app

print 'Getting actual config for', key, app.conf.get(key)
return app.conf.get(key)

def run_migrations_online():
"""Run migrations in 'online' mode.
Expand All @@ -50,21 +59,30 @@ def run_migrations_online():
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata
)
cfg = config.get_section(config.config_ini_section)
if 'use_flask_db_url' in cfg and cfg['use_flask_db_url'] == 'true':
cfg['sqlalchemy.url'] = get_app_config('SQLALCHEMY_URL')


engine = engine_from_config(
cfg,
prefix='sqlalchemy.',
poolclass=pool.NullPool)

connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata
)

try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()

if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

0 comments on commit 134740d

Please sign in to comment.