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
selenium-screenshot-testing
Patrick Cunningham 2019-06-21 02:00:33 -04:00 committed by Kevin Chung
parent 420e4f4dc7
commit ff0f2c2a0b
3 changed files with 14 additions and 7 deletions

View File

@ -3,7 +3,7 @@ import os
from distutils.version import StrictVersion from distutils.version import StrictVersion
from flask import Flask, Request from flask import Flask, Request
from flask_migrate import upgrade, stamp from flask_migrate import upgrade
from werkzeug.utils import cached_property from werkzeug.utils import cached_property
from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.middleware.proxy_fix import ProxyFix
from jinja2 import FileSystemLoader from jinja2 import FileSystemLoader
@ -11,7 +11,7 @@ from jinja2.sandbox import SandboxedEnvironment
from six.moves import input from six.moves import input
from CTFd import utils 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.sessions import CachingSessionInterface
from CTFd.utils.updates import update_check from CTFd.utils.updates import update_check
from CTFd.utils.initialization import ( 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 # Alembic sqlite support is lacking so we should just create_all anyway
if url.drivername.startswith("sqlite"): if url.drivername.startswith("sqlite"):
db.create_all() db.create_all()
stamp() stamp_latest_revision()
else: else:
# This creates tables instead of db.create_all() # This creates tables instead of db.create_all()
# Allows migrations to happen properly # Allows migrations to happen properly

View File

@ -1,11 +1,11 @@
from CTFd.utils import get_app_config, set_config 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.utils.uploads import get_uploader
from CTFd.models import db from CTFd.models import db
from CTFd.cache import cache from CTFd.cache import cache
from datafreeze.format import SERIALIZERS from datafreeze.format import SERIALIZERS
from flask import current_app as app 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 datafreeze.format.fjson import JSONSerializer, JSONEncoder
from sqlalchemy.exc import OperationalError, ProgrammingError from sqlalchemy.exc import OperationalError, ProgrammingError
from alembic.util import CommandError from alembic.util import CommandError
@ -278,7 +278,7 @@ def import_ctf(backup, erase=True):
upgrade(revision="head") upgrade(revision="head")
except (CommandError, RuntimeError, SystemExit): except (CommandError, RuntimeError, SystemExit):
app.db.create_all() app.db.create_all()
stamp() stamp_latest_revision()
# Invalidate all cached data # Invalidate all cached data
cache.clear() cache.clear()

View File

@ -1,5 +1,5 @@
from flask import current_app as app from flask import current_app as app
from flask_migrate import Migrate from flask_migrate import Migrate, stamp
from alembic.migration import MigrationContext from alembic.migration import MigrationContext
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.engine.url import make_url from sqlalchemy.engine.url import make_url
@ -8,6 +8,7 @@ from sqlalchemy_utils import (
create_database as create_database_util, create_database as create_database_util,
drop_database as drop_database_util, drop_database as drop_database_util,
) )
import os
migrations = Migrate() migrations = Migrate()
@ -42,3 +43,9 @@ def get_current_revision():
context = MigrationContext.configure(conn) context = MigrationContext.configure(conn)
current_rev = context.get_current_revision() current_rev = context.get_current_revision()
return current_rev 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)