From ff0f2c2a0bdc43c43adf7bcac9b9dd69476fb252 Mon Sep 17 00:00:00 2001 From: Patrick Cunningham Date: Fri, 21 Jun 2019 02:00:33 -0400 Subject: [PATCH] Fixed bug that caused apache2+wsgi deployment to break (#1030) * Create a `flask_migrate.stamp()` wrapper in `CTFd.utils.migrations` that always references the migrations folder regardless of how CTFd is run or deployed. * Closes #257 properly --- CTFd/__init__.py | 6 +++--- CTFd/utils/exports/__init__.py | 6 +++--- CTFd/utils/migrations/__init__.py | 9 ++++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CTFd/__init__.py b/CTFd/__init__.py index 1ef1de5..381fb86 100644 --- a/CTFd/__init__.py +++ b/CTFd/__init__.py @@ -3,7 +3,7 @@ import os from distutils.version import StrictVersion from flask import Flask, Request -from flask_migrate import upgrade, stamp +from flask_migrate import upgrade from werkzeug.utils import cached_property from werkzeug.middleware.proxy_fix import ProxyFix from jinja2 import FileSystemLoader @@ -11,7 +11,7 @@ from jinja2.sandbox import SandboxedEnvironment from six.moves import input from CTFd import utils -from CTFd.utils.migrations import migrations, create_database +from CTFd.utils.migrations import migrations, create_database, stamp_latest_revision from CTFd.utils.sessions import CachingSessionInterface from CTFd.utils.updates import update_check from CTFd.utils.initialization import ( @@ -149,7 +149,7 @@ def create_app(config="CTFd.config.Config"): # Alembic sqlite support is lacking so we should just create_all anyway if url.drivername.startswith("sqlite"): db.create_all() - stamp() + stamp_latest_revision() else: # This creates tables instead of db.create_all() # Allows migrations to happen properly diff --git a/CTFd/utils/exports/__init__.py b/CTFd/utils/exports/__init__.py index 6bfe5a7..d994a69 100644 --- a/CTFd/utils/exports/__init__.py +++ b/CTFd/utils/exports/__init__.py @@ -1,11 +1,11 @@ from CTFd.utils import get_app_config, set_config -from CTFd.utils.migrations import get_current_revision, create_database, drop_database +from CTFd.utils.migrations import get_current_revision, create_database, drop_database, stamp_latest_revision from CTFd.utils.uploads import get_uploader from CTFd.models import db from CTFd.cache import cache from datafreeze.format import SERIALIZERS from flask import current_app as app -from flask_migrate import upgrade, stamp +from flask_migrate import upgrade from datafreeze.format.fjson import JSONSerializer, JSONEncoder from sqlalchemy.exc import OperationalError, ProgrammingError from alembic.util import CommandError @@ -278,7 +278,7 @@ def import_ctf(backup, erase=True): upgrade(revision="head") except (CommandError, RuntimeError, SystemExit): app.db.create_all() - stamp() + stamp_latest_revision() # Invalidate all cached data cache.clear() diff --git a/CTFd/utils/migrations/__init__.py b/CTFd/utils/migrations/__init__.py index 3fa646d..68474bb 100644 --- a/CTFd/utils/migrations/__init__.py +++ b/CTFd/utils/migrations/__init__.py @@ -1,5 +1,5 @@ from flask import current_app as app -from flask_migrate import Migrate +from flask_migrate import Migrate, stamp from alembic.migration import MigrationContext from sqlalchemy import create_engine from sqlalchemy.engine.url import make_url @@ -8,6 +8,7 @@ from sqlalchemy_utils import ( create_database as create_database_util, drop_database as drop_database_util, ) +import os migrations = Migrate() @@ -42,3 +43,9 @@ def get_current_revision(): context = MigrationContext.configure(conn) current_rev = context.get_current_revision() return current_rev + + +def stamp_latest_revision(): + # Get proper migrations directory regardless of cwd + directory = os.path.join(os.path.dirname(app.root_path), 'migrations') + stamp(directory=directory)