Skip to content

Commit

Permalink
alembic: read wazo-auth config file when upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
pc-m authored and fblackburn1 committed Feb 2, 2018
1 parent 7ab5b90 commit 9d10e4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
3 changes: 0 additions & 3 deletions alembic.ini
Expand Up @@ -13,9 +13,6 @@ script_location = alembic
# the 'revision' command, regardless of autogenerate
# revision_environment = false

sqlalchemy.url = postgresql://asterisk:proformatique@localhost/asterisk


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
Expand Down
24 changes: 15 additions & 9 deletions alembic/env.py
Expand Up @@ -3,7 +3,8 @@
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from sqlalchemy import create_engine


# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -22,6 +23,16 @@
URI = os.getenv('ALEMBIC_DB_URI', None)


def get_url():
# The import should not be top level to allow the usage of the ALEMBIC_DB_URI
# environment variable when the DB is not hosted on the same host as wazo-auth.
# When building the docker image for the database for example.
from wazo_auth.config import get_config

wazo_config = get_config('')
return wazo_config.get('db_uri')


def run_migrations_offline():
"""Run migrations in 'offline' mode.
Expand All @@ -34,7 +45,7 @@ def run_migrations_offline():
script output.
"""
url = URI or config.get_main_option("sqlalchemy.url")
url = URI or get_url()
context.configure(url=url, version_table=VERSION_TABLE)

with context.begin_transaction():
Expand All @@ -48,13 +59,8 @@ def run_migrations_online():
and associate a connection with the context.
"""
if URI:
config.set_section_option('alembic', 'sqlalchemy.url', URI)

engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
url = URI or get_url()
engine = create_engine(url)

connection = engine.connect()
context.configure(connection=connection, version_table=VERSION_TABLE)
Expand Down
3 changes: 2 additions & 1 deletion contribs/docker/Dockerfile-db
Expand Up @@ -4,11 +4,12 @@ USER root

ADD . /usr/src/wazo-auth
WORKDIR /usr/src/wazo-auth
ENV ALEMBIC_DB_URI postgresql://asterisk:proformatique@localhost/asterisk

RUN true \
&& python3 setup.py install \
&& pg_start \
&& bin/wazo-auth-init-db --user postgres \
&& (cd /usr/src/wazo-auth && alembic -c alembic.ini upgrade head) \
&& pg_stop \
&& true
&& true

0 comments on commit 9d10e4d

Please sign in to comment.