Skip to content

Commit

Permalink
Merge pull request EUDAT-B2SHARE#1594 from nharraud/fix-upgrade-context
Browse files Browse the repository at this point in the history
upgrade: fix upgrade context
  • Loading branch information
nharraud authored Dec 12, 2017
2 parents 0afd516 + 3443ff9 commit d90779b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions b2share/modules/upgrade/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import warnings
from collections import namedtuple
from queue import Queue
from urllib.parse import urlunsplit
from functools import wraps

import click
from flask import current_app
Expand All @@ -39,6 +41,20 @@
from .models import Migration


def with_request_context(f):
"""Runs the decorated function in a request context."""
@wraps(f)
def decorator(*args, **kwargs):
base_url = urlunsplit((
current_app.config.get('PREFERRED_URL_SCHEME', 'http'),
current_app.config['JSONSCHEMAS_HOST'],
current_app.config.get('APPLICATION_ROOT') or '', '', ''
))
with current_app.test_request_context('/', base_url=base_url):
f(*args, **kwargs)
return decorator


def upgrade_to_last_version(verbose):
"""Upgrade the database to the last version and reindex the records."""
alembic = current_app.extensions['invenio-db'].alembic
Expand Down Expand Up @@ -142,6 +158,7 @@ def load(cls):
cls.loaded = True


@with_request_context
def run(self, failed_migration=None, verbose=None):
"""Run the upgrade."""
if not self.loaded:
Expand Down
9 changes: 8 additions & 1 deletion tests/b2share_unit_tests/upgrade/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@

def upgrade_run(app):
"""Run the "b2share upgrade" command."""
# unset SERVER_NAME as we want to check that it is not needed.
server_name = app.config['SERVER_NAME']
app.config.update(dict(SERVER_NAME=None))
# Upgrade B2SHARE with `b2share upgrade run`
runner = CliRunner()
script_info = ScriptInfo(create_app=lambda info: app)
return runner.invoke(run, ['-v'], obj=script_info)
result = runner.invoke(run, ['-v'], obj=script_info)
## Uncomment this in order to test without Click. ##
# from b2share.modules.upgrade.api import upgrade_to_last_version
# with app.app_context():
# upgrade_to_last_version(False)

# Set the SERVER_NAME to its previous value
app.config.update(dict(SERVER_NAME=server_name))
return result


def repeat_upgrade(app, alembic, expected_migrations=1):
"""Repeat the upgrade just to make sure that everything is done well."""
Expand Down

0 comments on commit d90779b

Please sign in to comment.