Skip to content

Commit

Permalink
Merge bff2083 into c0742ae
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego committed Jul 7, 2020
2 parents c0742ae + bff2083 commit 9980f73
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -14,6 +14,9 @@ language: python
cache:
- pip

services:
- docker

matrix:
fast_finish: true
include:
Expand Down
3 changes: 3 additions & 0 deletions MANIFEST.in
Expand Up @@ -11,6 +11,9 @@ include *.sh
include *.yaml
include pytest.ini
recursive-include reana_db *.py
recursive-include reana_db *.mako
recursive-include reana_db .gitkeep
recursive-include reana_db *.ini
recursive-include docs *.py
recursive-include docs *.png
recursive-include docs *.rst
Expand Down
58 changes: 58 additions & 0 deletions reana_db/alembic.ini
@@ -0,0 +1,58 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = %(here)s/alembic

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

file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d_%%(rev)s_%%(slug)s

[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
hooks=black
black.type=console_scripts
black.entrypoint=black


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
106 changes: 106 additions & 0 deletions reana_db/alembic/env.py
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2020 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""REANA DB alembic's environment context."""

from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

from reana_db.config import SQLALCHEMY_DATABASE_URI
from reana_db.models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

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

# 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 _include_object(object_, name, *args):
# We ignore non-reana tables in migrations
schema = object_.schema if hasattr(object_, "schema") else object_.table.schema
if name == "alembic_version" or schema != "__reana":
return False
return True


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.
"""
context.configure(
url=SQLALCHEMY_DATABASE_URI,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
version_table_schema="__reana",
include_schemas=True,
include_object=_include_object,
)

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.
"""
conf = {
"script_location": "reana_db/alembic",
"sqlalchemy.url": SQLALCHEMY_DATABASE_URI,
}
connectable = engine_from_config(
conf, prefix="sqlalchemy.", poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
version_table_schema="__reana",
include_schemas=True,
include_object=_include_object,
)

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 reana_db/alembic/script.py.mako
@@ -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"}
Empty file.

0 comments on commit 9980f73

Please sign in to comment.