def create_db():
    """
    Run database initialization
    :return:
    """
    from flask_migrate import init, migrate, upgrade

    # initialize migrations
    migrations_dir = os.path.join(app.config['ROOT_DIR'], 'migrations')
    if not os.path.exists(migrations_dir):
        init()

    # perform database migrations
    migrate()

    # migrate database to latest revision
    upgrade()

    print("Migrations completed" + "." * 10)

    # initialize database with default records
    from app.utils import InitDatabase
    init_db = InitDatabase()

    init_db.add_stories()

    print("Database records added" + "." * 10)
Beispiel #2
0
def database_upgrade():
    u"""升级数据库"""
    from flask.ext.migrate import upgrade, migrate

    migrate()
    upgrade()
    print u"数据库升级成功"
def db_refresh(short=False):
    """Refresh the database.

    :param short: Short version
    """
    create_local_context = not has_app_context()
    if create_local_context:
        # When this command is run from the command line, there is no app context, so let's create one
        from dontforget.app import create_app

        app_ = create_app(TestConfig)
        Migrate(app_, db, os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "..", "..", "migrations"))
        context = app_.app_context()
        context.push()

    tmp_handlers = current_app.logger.handlers
    current_app.logger.handlers = []
    tmp_stderr = sys.stderr
    if short:
        sys.stderr = None

    db.reflect()
    drop_everything()
    upgrade()

    if short:
        sys.stderr = tmp_stderr

    current_app.logger.handlers = tmp_handlers

    if create_local_context:
        # Remove the context after use
        db.session.remove()
        context.pop()
Beispiel #4
0
def setup_database(app):
    """Prepare the database. Create tables, run migrations etc."""

    def _pre_alembic_db():
        """ Checks if we are migrating from a pre-alembic ihatemoney
        """
        con = db.engine.connect()
        tables_exist = db.engine.dialect.has_table(con, 'project')
        alembic_setup = db.engine.dialect.has_table(con, 'alembic_version')
        return tables_exist and not alembic_setup

    sqlalchemy_url = app.config.get('SQLALCHEMY_DATABASE_URI')
    if sqlalchemy_url.startswith('sqlite:////tmp'):
        warnings.warn(
            'The database is currently stored in /tmp and might be lost at '
            'next reboot.'
        )

    db.init_app(app)
    db.app = app

    Migrate(app, db)
    migrations_path = os.path.join(app.root_path, 'migrations')

    if _pre_alembic_db():
        with app.app_context():
            # fake the first migration
            stamp(migrations_path, revision='b9a10d5d63ce')

    # auto-execute migrations on runtime
    with app.app_context():
        upgrade(migrations_path)
Beispiel #5
0
def init_app(app):
    db.init_app(app)

    # Check every table starts with app_name
    for table_name in db.metadata.tables.keys():
        if not table_name.startswith(app.name):
            raise Exception("DB table {} should start with {}".format(table_name, app.name))  # noqa

    # Setup migrations
    migrations_dir = os.path.abspath(os.path.join(app.root_path, "..", "migrations"))

    # Setup migrations
    with app.app_context():
        options = {
            # Use a separate alembic_version table per app
            "version_table": "{}_alembic_version".format(app.name)
        }
        migrate.init_app(app, directory=migrations_dir, **options)
        logger.info("Starting migrations", app=app.name)
        if os.path.isdir(migrations_dir):
            try:
                flask_migrate.upgrade()
                logger.info("Completed migrations", app=app.name)
            except Exception as e:
                logger.error("Migrations failure", app=app.name, error=e)
        else:
            flask_migrate.init()

    @app.before_request
    def setup_request():
        flask.g.db = app.db

    return db
Beispiel #6
0
def deploy():
    from flask_migrate import upgrade
    from app.models import Role, User

    upgrade()
    Role.insert_roles()
    User.add_self_follows()
 def setUp(self):
     super(TestBotSearch, self).setUp()
     # suppress logging
     logging.disable(logging.CRITICAL)
     # run the database migrations
     Migrate(self.app, self.db)
     upgrade()
def deploy():
    """
    Run deployment tasks.
    """

    # migrate database to latest revision
    upgrade()

    # pre-populate
    list(
        map(
            lambda x: x.populate(),
            (
                Roles,
                Agencies,
                Reasons,
                Users,
                LetterTemplates,
                EnvelopeTemplates,
                CustomRequestForms,
            ),
        )
    )

    es_recreate()
Beispiel #9
0
def deploy():
    """Run deployment tasks."""
    from flask_migrate import upgrade
    from app.models import Role, User

    # migrate database to latest revision
    upgrade()
Beispiel #10
0
def deploy():
    """Run deployment tasks."""
    from flask_migrate import upgrade
    from app.models import Role

    upgrade()

    Role.insert_roles()
Beispiel #11
0
def deploy():
    from flask_migrate import upgrade
    upgrade()

    # 创建用户角色
    Role.insert_roles()
    # 让所有用户都关注此用户
    User.add_self_follows()    
Beispiel #12
0
def deploy():
    """Run the deployment tasks ."""
    from flask_migrate import upgrade
    from app.models import Role,User

    upgrade()
    Role.insert_roles()
    User.add_self_follows()
Beispiel #13
0
def initdb(default_settings=True):
    """Creates the database."""
    upgrade()

    if default_settings:
        print("Creating default data...")
        create_default_groups()
        create_default_settings()
Beispiel #14
0
def deploy():
    """ Run deployment tasks. """
    from flask_migrate import upgrade
    from app.models import Role
    # migrate database to the latest revision.    
    upgrade()

    # create user roles
    Role.insert_roles()
Beispiel #15
0
    def setUp(self):
        super(DbTest, self).setUp()

        # Cleaning DB. It useful in case of tests failure
        directory = os.path.join(os.path.dirname(__file__),
                                 '..', 'api', 'db', 'migrations')
        with app.app_context():
            flask_migrate.downgrade(directory=directory)
            flask_migrate.upgrade(directory=directory)
def _init_db_tables(config):
    print 'Setting up a Flask app'
    setup_flask_app(
        manager_ip=config['postgresql_host'],
        hash_salt=config['hash_salt'],
        secret_key=config['secret_key']
    )

    print 'Creating tables in the DB'
    upgrade(directory=config['db_migrate_dir'])
Beispiel #17
0
def deploy():
    """Run deployment tasks."""
    from flask_migrate import upgrade
    from app.models import Role, User, Category
    # migrate database to latest revision
    upgrade()
    # create user roles
    Role.insert_roles()
    # create self-follows for all users
    User.add_self_follows()
Beispiel #18
0
def deploy():
    """Run deployment tasks."""
    from flask_migrate import upgrade
    from app.models import Role,User
    # 把数据库迁移到最新修订版本
    upgrade()
    # 创建用户角色
    Role.insert_roles()
    #让所有用户都关注此用户
    User.add_self_follows()
Beispiel #19
0
def deploy():
    from flask_migrate import upgrade
    from app.models import User, ArticleTypeSetting, ArticleType

    # upgrade database to the latest version
    upgrade()

    User.insert_admin(email='*****@*****.**', username='******', password='******')
    ArticleTypeSetting.insert_system_setting()
    ArticleType.insert_system_articleType()
def init_db(dbname, db):
    sqlalchemy.orm.configure_mappers()

    try:
        teardown_db(db)

    except sqlalchemy.exc.OperationalError as e:
        if 'does not exist' in str(e):
            create_db(dbname)
            init_db(dbname, db)

    upgrade()
Beispiel #21
0
def initialize():
    from flask_migrate import upgrade
    from app.models import Role, Ad
    upgrade()
    db.create_all()  # Create the materialized view

    Role.insert_roles()
    Canton.insert()
    District.insert()
    Location.insert()
    Ad.insert_initial_xml()
    User.insert_default_user()
Beispiel #22
0
def deploy():
    """Run deployment task."""
    from flask_migrate import upgrade
    from app.models import Role, User

    upgrade()

    # create new role
    Role.insert_roles()

    # let everybody follow the uper role
    User.add_self_follows()
Beispiel #23
0
def deploy():
    """Deployment tasks"""
    #migrate database to latest revision
    upgrade()
    #create user roles
    Role.insert_roles()
    #insert matches from the data file
    Team.insert_teams()
    #insert modules
    PredictionModule.insert_modules()
    #insert matches from the data file
    Match.update_all_matches()
Beispiel #24
0
def deploy():
    """部署到生产环境"""
    from flask_migrate import upgrade
    from app.models import Role, User

    # 把数据库迁移到最新修订版本
    upgrade()

    # 创建用户角色
    Role.insert_roles()

    # 让所有的用户都关注自己
    User.add_self_follows()
Beispiel #25
0
def deploy():
    from flask_migrate import upgrade
    from app.models import Role, User

    upgrade()

    Role.insert_roles()

    User.insert_admin()

    User.insert_auto()

    User.insert_user("*****@*****.**", "lyy51", "Lym")
    def setUp(self):
        """Set up a blank test database before each test"""
        self.db_name = name_from_uri(os.environ['TEST_DATABASE_URL'])
        app.app.config.update(
            TESTING=True,
            SQLALCHEMY_DATABASE_URI=os.environ['TEST_DATABASE_URL']

        )
        upgrade(directory='migrations')

        self.app = app.app.test_client()
        app.init_db(self.db_name)
        self.cursor = app.connect_db()
Beispiel #27
0
def populate(dropdb=False, createdb=False):
    """Creates the database with some default data.
    To drop or create the databse use the '-d' or '-c' options.
    """
    if dropdb:
        print("Dropping database...")
        db.drop_all()

    if createdb:
        print("Creating database...")
        upgrade()

    print("Creating test data...")
    create_test_data()
Beispiel #28
0
def init_all(app, migrate=True):
    init_logging(app)
    # === Important notice to the maintainer ===
    # This line was use to database = init_db(app)
    # But we found session can not be cleaned among different
    # Unit tests, so we add this to avoid issue
    # sqlalchemy-object-already-attached-to-session
    # http://stackoverflow.com/questions/24291933/sqlalchemy-object-already-attached-to-session
    # A similar issue was captured on
    # https://github.com/jarus/flask-testing/issues/32
    # Please don't try to modify the follow four lines.
    # Please don't try to modify the follow four lines.
    # Please don't try to modify the follow four lines.
    if Info.get_db() is None:
        database = init_db(app)
    else:
        database = Info.get_db()
        database.init_app(app)
    security = init_flask_security(app, database)
    init_migrate(app, database)
    if migrate:
        with app.app_context():
            upgrade(directory=MIGRATION_DIR)

    init_https(app)
    init_admin_views(app, database)
    babel = init_babel(app)
    api = init_flask_restful(app)
    init_reports(app, api)
    init_jinja2_functions(app)
    # init_debug_toolbar(app)
    init_image_service(app)
    socket_io = init_socket_io(app)
    define_route_context(app, database, babel)

    # define a context processor for merging flask-admin's template context
    # into the flask-security views.
    @security.context_processor
    def security_context_processor():
        from flask import url_for
        return dict(
            get_url=url_for
        )

    @app.teardown_appcontext
    def shutdown_session(exception=None):
        database = Info.get_db()
        database.session.remove()

    return socket_io
Beispiel #29
0
def load_dump(filename):
    """Load a database dump and upgrades it to the latest schema.

    `filename` is path to the SQL dump to load, relative to `pg_dump_dir`.

    The SQL in that file will be executed, and then migration scripts will
    be run to bring it up to date.
    """
    with open(path.join(pg_dump_dir, filename)) as f:
        sql = f.read()
    with app.app_context():
        db.session.execute(sql)
        db.session.commit()
    upgrade(revision='heads')
Beispiel #30
0
    def setUp(self):
        super(DbTest, self).setUp()

        # Cleaning all changes from the previous test
        db.session.rollback()

        directory = self.get_migrations_dir()
        with app.app_context():
            try:
                flask_migrate.downgrade(directory=directory,
                                        revision='base')
            except CommandError as e:
                app.logger.debug("DB migration downgrade failed: %s", e)
                self.clean_db()
            flask_migrate.upgrade(directory=directory)
Beispiel #31
0
def initdb():
    """Creates the database."""

    upgrade()
Beispiel #32
0
def deploy():
    """运行部署任务"""
    upgrade()
    Role.insert_roles()
    User.add_self_follows()
Beispiel #33
0
def deploy():
    """ run deployment tasks."""
    from flask_migrate import upgrade
    upgrade()
Beispiel #34
0
def _migrate_database():
    upgrade(directory=paths.MIGRATIONS_DIR)
Beispiel #35
0
def deploy():
    from flask_migrate import upgrade
    from blog.app.db_models import Role
    upgrade()
    Role.insert_roles()
Beispiel #36
0
def deploy():
    '''Выполняет операции связанные с развёртыванием'''
    from flask_migrate import upgrade

    # обновляет базу данных до последней версии
    upgrade()
Beispiel #37
0
def create_app(config="CTFd.config.Config"):
    app = CTFdFlask(__name__)
    with app.app_context():
        app.config.from_object(config)

        app.theme_loader = ThemeLoader(os.path.join(app.root_path, "themes"),
                                       followlinks=True)
        # Weird nested solution for accessing plugin templates
        app.plugin_loader = jinja2.PrefixLoader({
            "plugins":
            jinja2.FileSystemLoader(searchpath=os.path.join(
                app.root_path, "plugins"),
                                    followlinks=True)
        })
        # Load from themes first but fallback to loading from the plugin folder
        app.jinja_loader = jinja2.ChoiceLoader(
            [app.theme_loader, app.plugin_loader])

        from CTFd.models import (  # noqa: F401
            db, Teams, Solves, Challenges, Fails, Flags, Tags, Files, Tracking,
        )

        url = create_database()

        # This allows any changes to the SQLALCHEMY_DATABASE_URI to get pushed back in
        # This is mostly so we can force MySQL's charset
        app.config["SQLALCHEMY_DATABASE_URI"] = str(url)

        # Register database
        db.init_app(app)

        # Register Flask-Migrate
        migrations.init_app(app, db)

        # Alembic sqlite support is lacking so we should just create_all anyway
        if url.drivername.startswith("sqlite"):
            # Enable foreign keys for SQLite. This must be before the
            # db.create_all call because tests use the in-memory SQLite
            # database (each connection, including db creation, is a new db).
            # https://docs.sqlalchemy.org/en/13/dialects/sqlite.html#foreign-key-support
            from sqlalchemy.engine import Engine
            from sqlalchemy import event

            @event.listens_for(Engine, "connect")
            def set_sqlite_pragma(dbapi_connection, connection_record):
                cursor = dbapi_connection.cursor()
                cursor.execute("PRAGMA foreign_keys=ON")
                cursor.close()

            db.create_all()
            stamp_latest_revision()
        else:
            # This creates tables instead of db.create_all()
            # Allows migrations to happen properly
            upgrade()

        from CTFd.models import ma

        ma.init_app(app)

        app.db = db
        app.VERSION = __version__
        app.CHANNEL = __channel__

        from CTFd.cache import cache

        cache.init_app(app)
        app.cache = cache

        reverse_proxy = app.config.get("REVERSE_PROXY")
        if reverse_proxy:
            if type(reverse_proxy) is str and "," in reverse_proxy:
                proxyfix_args = [int(i) for i in reverse_proxy.split(",")]
                app.wsgi_app = ProxyFix(app.wsgi_app, *proxyfix_args)
            else:
                app.wsgi_app = ProxyFix(app.wsgi_app,
                                        x_for=1,
                                        x_proto=1,
                                        x_host=1,
                                        x_port=1,
                                        x_prefix=1)

        version = utils.get_config("ctf_version")

        # Upgrading from an older version of CTFd
        if version and (StrictVersion(version) < StrictVersion(__version__)):
            if confirm_upgrade():
                run_upgrade()
            else:
                exit()

        if not version:
            utils.set_config("ctf_version", __version__)

        if not utils.get_config("ctf_theme"):
            utils.set_config("ctf_theme", "core")

        update_check(force=True)

        init_request_processors(app)
        init_template_filters(app)
        init_template_globals(app)

        # Importing here allows tests to use sensible names (e.g. api instead of api_bp)
        from CTFd.views import views
        from CTFd.teams import teams
        from CTFd.users import users
        from CTFd.challenges import challenges
        from CTFd.scoreboard import scoreboard
        from CTFd.auth import auth
        from CTFd.admin import admin
        from CTFd.api import api
        from CTFd.events import events
        from CTFd.errors import page_not_found, forbidden, general_error, gateway_error

        app.register_blueprint(views)
        app.register_blueprint(teams)
        app.register_blueprint(users)
        app.register_blueprint(challenges)
        app.register_blueprint(scoreboard)
        app.register_blueprint(auth)
        app.register_blueprint(api)
        app.register_blueprint(events)

        app.register_blueprint(admin)

        app.register_error_handler(404, page_not_found)
        app.register_error_handler(403, forbidden)
        app.register_error_handler(500, general_error)
        app.register_error_handler(502, gateway_error)

        init_logs(app)
        init_events(app)
        init_plugins(app)

        return app
Beispiel #38
0
def init_db():
    # migrate database to the latest version
    upgrade()
Beispiel #39
0
def deploy():
    from app.models import Role
    upgrade()
    Role.seed()
Beispiel #40
0
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, upgrade
from flask_login import LoginManager

app = Flask(__name__)
app.secret_key = 'any random string'

app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('URL_BANCO_DADOS')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)

login_manager = LoginManager()
login_manager.init_app(app)

from conteudo_tech_negro import routes
from conteudo_tech_negro.store import models
from conteudo_tech_negro import admin_configuracao

if not os.getenv('TESTES'):
    # flask migrate command to be used on GAE
    with app.app_context():
        upgrade()

    admin_configuracao.criar_usuaria_admin()
Beispiel #41
0
def upgrade_db():
    migrate()
    upgrade()
Beispiel #42
0
def run_upgrade():
    upgrade()
    utils.set_config("ctf_version", __version__)
Beispiel #43
0
 def run(self):
     upgrade()
Beispiel #44
0
def migrate_handler(event, context):
    from sls-flask.app import app

    with app.app_context():
        flask_migrate.upgrade()
    return "Migrated"
Beispiel #45
0
def init_db():
    upgrade(directory='./migrations')
Beispiel #46
0
def update_db():
    app = create_app(config_file='config/testing.py')
    with app.app_context():
        upgrade()
# project/server/__init__.py

import os

from flask import Flask
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
import flask_migrate

app = Flask(__name__)

app_settings = os.getenv('APP_SETTINGS',
                         'project.server.config.DevelopmentConfig')
app.config.from_object(app_settings)

bcrypt = Bcrypt(app)
db = SQLAlchemy(app)

migrate = flask_migrate.Migrate(db=db,
                                directory="migrations",
                                render_as_batch=False)

migrate.init_app(app)

with app.app_context():
    flask_migrate.upgrade()

from project.server.auth.views import auth_blueprint
app.register_blueprint(auth_blueprint)
Beispiel #48
0
def upgrade_db():
    log.info("Beginning database migration...")
    flask_migrate.upgrade()
    create_system_roles()
Beispiel #49
0
def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    load_dotenv()
    app.config.update(os.environ)

    @app.before_request
    def start_session():
        try:
            session["sid"]
        except KeyError:
            session["sid"] = urllib.parse.quote_plus(b64encode(os.urandom(10)))
            print("Starting with sid {}".format(session["sid"]))

    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    images = UploadSet("images", IMAGES)
    patch_request_class(app, 2 * 1024 * 1024)
    configure_uploads(app, images)

    from . import db
    from . import auth
    from . import views

    app.register_blueprint(auth.bp)
    app.register_blueprint(views.bp)
    from .blueprints.admin import admin_theme

    app.register_blueprint(admin_theme, url_prefix="/admin")
    app.add_url_rule("/", "index", views.__getattribute__("choose"))

    # the signals
    from .signals import journey_complete

    # Set custom modules path
    sys.path.append(app.config["MODULES_PATH"])

    with app.app_context():

        # Migrate database
        database.init_app(app)
        migrate = Migrate(app, database)
        upgrade('./migrations')

        if test_config is not None:
            seed_db()
            app.config.update(test_config)

        load_theme(app)

        # Register yml pages as routes
        pages = Page.query.all()
        for page in pages:
            page_path = page.path
            template_file = page.template_file
            view_func_name = page.page_name
            # Generate view function
            generate_view_func = """def %s_view_func():
            return render_template('%s')""" % (
                view_func_name,
                template_file,
            )
            exec(generate_view_func) in globals(), locals()
            method_name = view_func_name + "_view_func"
            possibles = globals().copy()
            possibles.update(locals())
            view_func = possibles.get(method_name)
            app.add_url_rule("/" + page_path, view_func_name + "_view_func",
                             view_func)

        # Import any custom modules
        modules = Module.query.all()
        print("sys.path contains: {}".format(sys.path))
        for module in modules:
            # Assume standard python module
            try:
                print("Attempting to importing module: {}".format(module.name))
                importlib.import_module(module.name)
            except ModuleNotFoundError:
                # Attempt to load module from src
                dest = Path(app.config["MODULES_PATH"], module.name)
                print("Cloning module into: {}".format(dest))
                os.makedirs(str(dest), exist_ok=True)
                try:
                    git.Repo.clone_from(module.src, dest)
                except git.exc.GitCommandError:
                    pass
                # Now re-try import
                try:
                    import site

                    reload(site)
                    importlib.import_module(module.name)
                except ModuleNotFoundError:
                    print("Error: Could not import module: {}".format(
                        module.name))
            # Register modules as blueprint (if it is one)
            try:
                importedModule = importlib.import_module(module.name)
                if isinstance(getattr(importedModule, module.name), Blueprint):
                    # Load any config the Blueprint declares
                    blueprint = getattr(importedModule, module.name)
                    blueprintConfig = "".join(
                        [blueprint.root_path, "/", "config.py"])
                    app.config.from_pyfile(blueprintConfig, silent=True)
                    # Register the Blueprint
                    app.register_blueprint(getattr(importedModule,
                                                   module.name))
                    print("Imported as flask Blueprint")
                    # Run Blueprint migrations if any
                    modulePath = Path(importedModule.__file__).parents[0]
                    moduleMigrationsPath = Path(modulePath, 'migrations')
                    if moduleMigrationsPath.is_dir():
                        # Run migrations
                        for migration in moduleMigrationsPath.iterdir():
                            print("Running module migration {}".format(
                                migration))
                            # Run subscribie_cli database migrations
                            db_full_path = app.config['DB_FULL_PATH']
                            subprocess.call("python " + str(migration) +
                                            ' -up -db ' + db_full_path,
                                            shell=True)

            except (ModuleNotFoundError, AttributeError):
                print("Error: Could not import module as blueprint: {}".format(
                    module["name"]))

    # Handling Errors Gracefully
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template("errors/404.html"), 404

    @app.errorhandler(500)
    def page_not_found(e):
        return render_template("errors/500.html"), 500

    @app.cli.command()
    def initdb():
        """Initialize the database."""
        click.echo('Init the db')
        with open("seed.sql") as fp:
            con = sqlite3.connect(app.config["DB_FULL_PATH"])
            cur = con.cursor()
            cur.executescript(fp.read())
            con.close()

    return app
Beispiel #50
0
def database(app):
    flask_migrate.upgrade(revision='head')
    yield
    flask_migrate.downgrade(revision='base')
def dbupgrade_handler(event, context):
    revision = os.getenv('DB_REVISION', 'head')
    with app.app_context():
        upgrade(revision=revision)
Beispiel #52
0
def setup_db(feature):
    logging.getLogger('alembic').setLevel(logging.ERROR)
    with chaos.app.app_context():
        flask_migrate.upgrade(directory=migration_dir)
Beispiel #53
0
def deploy():
    """
    migrate database to latest revision
    """
    upgrade()
Beispiel #54
0
def deploy():
    from flask_migrate import upgrade
    db.create_all()
    upgrade()
Beispiel #55
0
def create_app(config="CTFd.config.Config"):
    app = CTFdFlask(__name__)
    with app.app_context():
        app.config.from_object(config)

        theme_loader = ThemeLoader(os.path.join(app.root_path, "themes"),
                                   followlinks=True)
        app.jinja_loader = theme_loader

        from CTFd.models import (  # noqa: F401
            db, Teams, Solves, Challenges, Fails, Flags, Tags, Files, Tracking,
        )

        url = create_database()

        # This allows any changes to the SQLALCHEMY_DATABASE_URI to get pushed back in
        # This is mostly so we can force MySQL's charset
        app.config["SQLALCHEMY_DATABASE_URI"] = str(url)

        # Register database
        db.init_app(app)

        # Register Flask-Migrate
        migrations.init_app(app, db)

        # Alembic sqlite support is lacking so we should just create_all anyway
        if url.drivername.startswith("sqlite"):
            db.create_all()
            stamp_latest_revision()
        else:
            # This creates tables instead of db.create_all()
            # Allows migrations to happen properly
            upgrade()

        from CTFd.models import ma

        ma.init_app(app)

        app.db = db
        app.VERSION = __version__

        from CTFd.cache import cache

        cache.init_app(app)
        app.cache = cache

        reverse_proxy = app.config.get("REVERSE_PROXY")
        if reverse_proxy:
            if "," in reverse_proxy:
                proxyfix_args = [int(i) for i in reverse_proxy.split(",")]
                app.wsgi_app = ProxyFix(app.wsgi_app, None, *proxyfix_args)
            else:
                app.wsgi_app = ProxyFix(
                    app.wsgi_app,
                    num_proxies=None,
                    x_for=1,
                    x_proto=1,
                    x_host=1,
                    x_port=1,
                    x_prefix=1,
                )

        version = utils.get_config("ctf_version")

        # Upgrading from an older version of CTFd
        if version and (StrictVersion(version) < StrictVersion(__version__)):
            if confirm_upgrade():
                run_upgrade()
            else:
                exit()

        if not version:
            utils.set_config("ctf_version", __version__)

        if not utils.get_config("ctf_theme"):
            utils.set_config("ctf_theme", "core")

        update_check(force=True)

        init_request_processors(app)
        init_template_filters(app)
        init_template_globals(app)

        # Importing here allows tests to use sensible names (e.g. api instead of api_bp)
        from CTFd.views import views
        from CTFd.teams import teams
        from CTFd.users import users
        from CTFd.challenges import challenges
        from CTFd.scoreboard import scoreboard
        from CTFd.auth import auth
        from CTFd.admin import admin
        from CTFd.api import api
        from CTFd.events import events
        from CTFd.errors import page_not_found, forbidden, general_error, gateway_error

        app.register_blueprint(views)
        app.register_blueprint(teams)
        app.register_blueprint(users)
        app.register_blueprint(challenges)
        app.register_blueprint(scoreboard)
        app.register_blueprint(auth)
        app.register_blueprint(api)
        app.register_blueprint(events)

        app.register_blueprint(admin)

        app.register_error_handler(404, page_not_found)
        app.register_error_handler(403, forbidden)
        app.register_error_handler(500, general_error)
        app.register_error_handler(502, gateway_error)

        init_logs(app)
        init_events(app)
        init_plugins(app)

        return app
Beispiel #56
0
def deploy():
    """Run deployment tasks."""
    upgrade()
    Role.insert_roles()
    User.add_self_follows()
Beispiel #57
0
def deploy():
    """Run deployment tasks."""
    # migrate database to latest revision
    upgrade()
Beispiel #58
0
def check_db_version(install_path, db_file):
    """
    Check if db exists and is up to date.
    If it doesn't exist create it.  If it's out of date update it.
    """
    from alembic.script import ScriptDirectory
    from alembic.config import Config
    import sqlite3
    import flask_migrate

    # db_file = job.config.DBFILE
    mig_dir = os.path.join(install_path, "arm/migrations")

    config = Config()
    config.set_main_option("script_location", mig_dir)
    script = ScriptDirectory.from_config(config)

    # create db file if it doesn't exist
    if not os.path.isfile(db_file):
        logging.info("No database found.  Initializing arm.db...")
        make_dir(os.path.dirname(db_file))
        with app.app_context():
            flask_migrate.upgrade(mig_dir)

        if not os.path.isfile(db_file):
            logging.error(
                "Can't create database file.  This could be a permissions issue.  Exiting..."
            )
            sys.exit()

    # check to see if db is at current revision
    head_revision = script.get_current_head()
    logging.debug("Head is: " + head_revision)

    conn = sqlite3.connect(db_file)
    c = conn.cursor()

    c.execute("SELECT {cn} FROM {tn}".format(cn="version_num",
                                             tn="alembic_version"))
    db_version = c.fetchone()[0]
    logging.debug("Database version is: " + db_version)
    if head_revision == db_version:
        logging.info("Database is up to date")
    else:
        logging.info("Database out of date. Head is " + head_revision +
                     " and database is " + db_version +
                     ".  Upgrading database...")
        with app.app_context():
            ts = round(time.time() * 100)
            logging.info("Backuping up database '" + db_file + "' to '" +
                         db_file + str(ts) + "'.")
            shutil.copy(db_file, db_file + "_" + str(ts))
            flask_migrate.upgrade(mig_dir)
        logging.info("Upgrade complete.  Validating version level...")

        c.execute("SELECT {cn} FROM {tn}".format(tn="alembic_version",
                                                 cn="version_num"))
        db_version = c.fetchone()[0]
        logging.debug("Database version is: " + db_version)
        if head_revision == db_version:
            logging.info("Database is now up to date")
        else:
            logging.error("Database is still out of date. Head is " +
                          head_revision + " and database is " + db_version +
                          ".  Exiting arm.")
            sys.exit()
Beispiel #59
0
def deploy():
    # Migrate db to latest revision
    upgrade()
Beispiel #60
0
#MIGRATE_DIR=os.path.join(BASEDIR, "migrations")


def auto_constraint_name(constraint, table):
    if constraint.name is None or constraint.name == "_unnamed_":
        return "sa_autoname_%s" % str(uuid.uuid4())[0:5]
    else:
        return constraint.name


convention = {
    "auto_constraint_name": auto_constraint_name,
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(auto_constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s"
}

metadata = MetaData(naming_convention=convention)

db = SQLAlchemy(app, metadata=metadata)

from wikidata_models import *

migrate = Migrate(app, db, render_as_batch=True)  #, directory=MIGRATE_DIR

if AUTO_MIGRATE:
    with app.app_context():
        upgrade(directory=os.path.join(BASEDIR, 'migrations'))