Skip to content

Commit

Permalink
Add Alembic health checks that validates the DB is at the last version
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valsecchi committed Jun 8, 2017
1 parent b01b630 commit 1ec56e2
Show file tree
Hide file tree
Showing 22 changed files with 303 additions and 27 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__pycache__
*.pyc
/.venv/
/acceptance_tests/app/c2cwsgiutils
/acceptance_tests/app/c2cwsgiutils_coverage_report.py
Expand All @@ -13,5 +15,5 @@
/c2cwsgiutils.egg-info/
/dist/
/reports/
.idea/dbnavigator.xml
.idea/inspectionProfiles
/.idea/dbnavigator.xml
/.idea/inspectionProfiles
6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,17 @@ health_check = HealthCheck(config)
health_check.add_db_session_check(models.DBSession, at_least_one_model=models.Hello)
health_check.add_url_check('http://localhost/api/hello')
health_check.add_custom_check('custom', custom_check, 2)
health_check.add_alembic_check(models.DBSession, '/app/alembic.ini', 3)
```

Then, the URL `{C2C_BASE_PATH}/health_check?max_level=2` can be used to run the health checks and get a report
Then, the URL `{C2C_BASE_PATH}/health_check?max_level=3` can be used to run the health checks and get a report
looking like that (in case of error):

```json
{
"status": 500,
"successes": ["db_engine_sqlalchemy", "db_engine_sqlalchemy_slave", "http://localhost/api/hello"],
"successes": ["db_engine_sqlalchemy", "db_engine_sqlalchemy_slave", "http://localhost/api/hello",
"alembic_app_alembic.ini"],
"failures": {
"custom": {
"message": "I'm not happy"
Expand All @@ -199,6 +201,8 @@ looking like that (in case of error):
}
```

Look at the documentation of the `c2cwsgiutils.health_check.HealthCheck` class for more information.


SQLAlchemy models graph
-----------------------
Expand Down
32 changes: 32 additions & 0 deletions acceptance_tests/app/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = app_alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = %(SQLALCHEMY_URL)s
Empty file.
76 changes: 76 additions & 0 deletions acceptance_tests/app/app_alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
import os


# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option('sqlalchemy.url', os.environ["SQLALCHEMY_URL"])

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig('production.ini', defaults=os.environ)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None

# 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.


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True)

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


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
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,
# To allow to run "UPDATE" in a migration and do an "ALTER" in another one, later.
transaction_per_migration=True
)

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


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions acceptance_tests/app/app_alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Initial version
Revision ID: 4a8c1bb4e775
Revises:
Create Date: 2016-09-14 09:23:27.466418
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '4a8c1bb4e775'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
op.execute("CREATE EXTENSION IF NOT EXISTS postgis;")
op.execute("""
CREATE TABLE hello (
id SERIAL PRIMARY KEY,
value TEXT UNIQUE INITIALLY DEFERRED
)
""")


def downgrade():
pass
Empty file.
1 change: 1 addition & 0 deletions acceptance_tests/app/c2cwsgiutils_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ def main(_, **settings):
health_check.add_db_session_check(models.DBSession, at_least_one_model=models.Hello)
health_check.add_url_check('http://localhost/api/hello')
health_check.add_custom_check('fail', _failure, 2)
health_check.add_alembic_check(models.DBSession, '/app/alembic.ini', 1)

return config.make_wsgi_app()
1 change: 1 addition & 0 deletions acceptance_tests/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
-r rel_requirements.txt
alembic==0.9.2
flake8==3.3.0
19 changes: 19 additions & 0 deletions acceptance_tests/app/run_alembic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# Upgrade the DB
set -e

# wait for the DB to be UP
while ! echo "import sqlalchemy; sqlalchemy.create_engine('$SQLALCHEMY_URL').connect()" | python 2> /dev/null
do
echo "Waiting for the DB to be reachable"
sleep 1;
done

for ini in *alembic*.ini
do
if [[ -f $ini ]]
then
echo "$ini ==========================="
alembic -c $ini upgrade head
fi
done
18 changes: 17 additions & 1 deletion acceptance_tests/tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ app:
SQL_PROFILER_SECRET: changeme
DEBUG_VIEW_SECRET: changeme
LOG_HOST: 172.17.0.1
LOG_TYPE: 'json,logstash'
LOG_TYPE: 'console,logstash'
SQL_LOG_LEVEL: DEBUG
OTHER_LOG_LEVEL: DEBUG
DEVELOPMENT: 1
Expand All @@ -21,6 +21,22 @@ app:
ports:
- 8480:80

alembic_master:
image: camptocamp/c2cwsgiutils_test_app:${DOCKER_TAG}
environment:
SQLALCHEMY_URL: postgresql://www-data:www-data@db:5432/test
links:
- db
command: /bin/true # will use execute with another script from the tests to actually do it

alembic_slave:
image: camptocamp/c2cwsgiutils_test_app:${DOCKER_TAG}
environment:
SQLALCHEMY_URL: postgresql://www-data:www-data@db:5432/test
links:
- db_slave:db
command: /bin/true # will use execute with another script from the tests to actually do it

db:
image: camptocamp/postgresql:pg9.5-latest
environment:
Expand Down
15 changes: 5 additions & 10 deletions acceptance_tests/tests/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,20 @@ def app_connection(composition):

@pytest.fixture(scope="session")
def master_db_setup(composition):
return _create_table(master=True)
return _create_table(composition, master=True)


@pytest.fixture(scope="session")
def slave_db_setup(composition):
return _create_table(master=False)
return _create_table(composition, master=False)


def _create_table(master):
def _create_table(composition, master):
name = 'master' if master else 'slave'
composition.run('alembic_' + name, '/app/run_alembic.sh')
connection = _connect(master)
with connection.cursor() as curs:
LOG.info("Creating table for " + name)
curs.execute("""
CREATE TABLE hello (
id SERIAL PRIMARY KEY,
value TEXT UNIQUE INITIALLY DEFERRED
)
""")
LOG.info("Creating data for " + name)
curs.execute("INSERT INTO hello (value) VALUES ('%s')" % (name))
connection.commit()
return connection
Expand Down
6 changes: 4 additions & 2 deletions acceptance_tests/tests/tests/test_health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ def test_ok(app_connection):
print('response=' + json.dumps(response))
assert response == {
'status': 200,
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello'],
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello',
'alembic_app_alembic.ini'],
'failures': {}
}

Expand All @@ -16,7 +17,8 @@ def test_failure(app_connection):
print('response=' + json.dumps(response))
assert response == {
'status': 500,
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello'],
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello',
'alembic_app_alembic.ini'],
'failures': {
'fail': {
'message': 'failing check',
Expand Down
4 changes: 2 additions & 2 deletions c2cwsgiutils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def setup_session(config, master_prefix, slave_prefix=None, force_master=None, f
path includes the route_prefix.
:param config: The pyramid Configuration object
:param master_prefix: The prefix for the master connection configuration entries in the application
:param master_prefix: The prefix for the master connection configuration entries in the application \
settings
:param slave_prefix: The prefix for the slave connection configuration entries in the application
:param slave_prefix: The prefix for the slave connection configuration entries in the application \
settings
:param force_master: The method/paths that needs to use the master
:param force_slave: The method/paths that needs to use the slave
Expand Down
Loading

0 comments on commit 1ec56e2

Please sign in to comment.