Install versioning on the db.
763072
def enforce_versioning(force=False):
    """"""
    connect_str, repo_url = get_version_data()
    LOG.warning("Your database uses an unversioned benchbuild schema.")
    if not force and not ui.ask(
            "Should I enforce version control on your schema?"):
        LOG.error("User declined schema versioning.")
        return None
    repo_version = migrate.version(repo_url, url=connect_str)
    migrate.version_control(connect_str, repo_url, version=repo_version)
    return repo_version
1017448
def createdb():
    """"""
    manager.db.engine.echo = True
    manager.db.create_all()
    set_alembic_revision()
1112793
def install(**kwargs):
    """"""
    force = kwargs.get('force')
    preserve_legacy = kwargs.get('preserve_legacy')

    colorama.init(strip=kwargs.get('no_color'))

    stdout = subprocess.check_output('which therapist', shell=True)
    therapist_bin = stdout.decode('utf-8').split()[0]

    git_dir = current_git_dir()

    if git_dir is None:
        output(NOT_GIT_REPO_MSG)
        exit(1)

    hook_options = {
        'fix': '--fix' if kwargs.get('fix') else '',
        'stage_modified_files': '' if kwargs.get('no_stage_modified_files') else '--stage-modified-files',
        'therapist_bin': therapist_bin,
    }

    srchook_path = os.path.join(BASE_DIR, 'hooks', 'pre-commit-template')
    with open(srchook_path, 'r') as f:
        srchook = f.read()
    srchook_hash = hash_hook(srchook_path, hook_options)

    dsthook_path = os.path.join(git_dir, 'hooks', 'pre-commit')

    if os.path.isfile(dsthook_path):
        dsthook_hash = identify_hook(dsthook_path)
        if dsthook_hash:
            if dsthook_hash == srchook_hash:
                output(HOOK_ALREADY_INSTALLED_MSG)
                exit(0)
        else:
            if not force and not preserve_legacy:
                print(EXISTING_HOOK_MSG)
                preserve_legacy = click.confirm(CONFIRM_PRESERVE_LEGACY_HOOK_MSG, default=True)

            if preserve_legacy:
                output(COPYING_HOOK_MSG, end='')
                shutil.copy2(dsthook_path, '{}.legacy'.format(dsthook_path))
                output(DONE_COPYING_HOOK_MSG)
            elif not force:
                if not click.confirm(CONFIRM_REPLACE_HOOK_MSG, default=False):
                    output(INSTALL_ABORTED_MSG)
                    exit(1)

    output(INSTALLING_HOOK_MSG, end='')
    with open(dsthook_path, 'w+') as f:
        srchook = srchook.replace('%hash%', srchook_hash)
        for k, v in iteritems(hook_options):
            srchook = srchook.replace('%{}%'.format(k), v)
        f.write(srchook)
    os.chmod(dsthook_path, 0o775)
    output(DONE_INSTALLING_HOOK_MSG)
752254
def bundle(context, name):
    """"""
    if context.obj['db'].bundle(name):
        click.echo(click.style('bundle name already exists', fg='yellow'))
        context.abort()
    new_bundle = context.obj['db'].new_bundle(name)
    context.obj['db'].add_commit(new_bundle)

    # add default version
    new_version = context.obj['db'].new_version(created_at=new_bundle.created_at)
    new_version.bundle = new_bundle
    context.obj['db'].add_commit(new_version)

    click.echo(click.style(f"new bundle added: {new_bundle.name} ({new_bundle.id})", fg='green'))
301053
def iter_installable_versions(self):
        
        for name in self._pyenv('install', '--list').out.splitlines():
            try:
                version = Version.parse(name.strip())
            except ValueError:
                continue
            yield version
1169193
def init(ctx):
    ''''''

    log.info('Apply DB migrations if needed')
    ctx.invoke(migrate, record=True)

    ctx.invoke(index)

    if IS_TTY:
        text = _('Do you want to create a superadmin user?')
        if click.confirm(text, default=True):
            user = ctx.invoke(user_commands.create)
            ctx.invoke(user_commands.set_admin, email=user.email)

        text = _('Do you want to import some data-related license?')
        if click.confirm(text, default=True):
            ctx.invoke(licenses)

        text = _('Do you want to create some sample data?')
        if click.confirm(text, default=True):
            ctx.invoke(generate_fixtures)

    success(_('Your udata instance is ready!'))
888533
def rebuild():
    
    drop_db()
    create_db()
    if StrictVersion(django.get_version()) < StrictVersion('1.7'):
        local('python{} manage.py syncdb --all --noinput'.format(
            PYTHON_VERSION))
        local('python{} manage.py migrate --fake'.format(PYTHON_VERSION))
    else:
        local('python{} manage.py migrate'.format(PYTHON_VERSION))
435620
def upgrade():
    """"""
    op.create_table(
        'access_actionssystemroles',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('action', sa.String(length=80), nullable=True),
        sa.Column('exclude', sa.Boolean(name='exclude'), server_default='0',
                  nullable=False),
        sa.Column('argument', sa.String(length=255), nullable=True),
        sa.Column('role_name', sa.String(length=40), nullable=False),
        sa.PrimaryKeyConstraint('id',
                                name=op.f('pk_access_actionssystemroles')),
        sa.UniqueConstraint('action', 'exclude', 'argument', 'role_name',
                            name='access_actionssystemroles_unique')
    )
    op.create_index(op.f('ix_access_actionssystemroles_action'),
                    'access_actionssystemroles', ['action'], unique=False)
    op.create_index(op.f('ix_access_actionssystemroles_argument'),
                    'access_actionssystemroles', ['argument'], unique=False)
    op.create_index(op.f('ix_access_actionssystemroles_role_name'),
                    'access_actionssystemroles', ['role_name'], unique=False)
817349
def upgrade():
    """"""
    # Variant types:
    def created():
        """Return instance of a column."""
        return sa.Column(
            'created',
            sa.DateTime().with_variant(mysql.DATETIME(fsp=6), 'mysql'),
            nullable=False
        )

    def updated():
        """Return instance of a column."""
        return sa.Column(
            'updated',
            sa.DateTime().with_variant(mysql.DATETIME(fsp=6), 'mysql'),
            nullable=False
        )

    def uri():
        """Return instance of a column."""
        return sa.Column(
            'uri',
            sa.Text().with_variant(mysql.VARCHAR(255), 'mysql'),
            nullable=True
        )

    def key(nullable=True):
        """Return instance of a column."""
        return sa.Column(
            'key',
            sa.Text().with_variant(mysql.VARCHAR(255), 'mysql'),
            nullable=nullable
        )

    op.create_table(
        'files_files',
        created(),
        updated(),
        sa.Column(
            'id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        uri(),
        sa.Column('storage_class', sa.String(length=1), nullable=True),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('checksum', sa.String(length=255), nullable=True),
        sa.Column('readable', sa.Boolean(name='readable'), nullable=False),
        sa.Column('writable', sa.Boolean(name='writable'), nullable=False),
        sa.Column('last_check_at', sa.DateTime(), nullable=True),
        sa.Column('last_check', sa.Boolean(name='last_check'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('uri')
    )
    op.create_table(
        'files_location',
        created(),
        updated(),
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=20), nullable=False),
        sa.Column('uri', sa.String(length=255), nullable=False),
        sa.Column('default', sa.Boolean(name='default'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('name')
    )
    op.create_table(
        'files_bucket',
        created(),
        updated(),
        sa.Column(
            'id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        sa.Column('default_location', sa.Integer(), nullable=False),
        sa.Column(
            'default_storage_class',
            sa.String(length=1),
            nullable=False
        ),
        sa.Column('size', sa.BigInteger(), nullable=False),
        sa.Column('quota_size', sa.BigInteger(), nullable=True),
        sa.Column('max_file_size', sa.BigInteger(), nullable=True),
        sa.Column('locked', sa.Boolean(name='locked'), nullable=False),
        sa.Column('deleted', sa.Boolean(name='deleted'), nullable=False),
        sa.ForeignKeyConstraint(
            ['default_location'],
            [u'files_location.id'],
            ondelete='RESTRICT'),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_table(
        'files_buckettags',
        sa.Column(
            'bucket_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        sa.Column('key', sa.String(length=255), nullable=False),
        sa.Column('value', sa.Text(), nullable=False),
        sa.ForeignKeyConstraint(
            ['bucket_id'],
            [u'files_bucket.id'],
            ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('bucket_id', 'key')
    )
    op.create_table(
        'files_multipartobject',
        created(),
        updated(),
        sa.Column(
            'upload_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        sa.Column(
            'bucket_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=True),
        key(),
        sa.Column(
            'file_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        sa.Column('chunk_size', sa.Integer(), nullable=True),
        sa.Column('size', sa.BigInteger(), nullable=True),
        sa.Column('completed', sa.Boolean(name='completed'), nullable=False),
        sa.ForeignKeyConstraint(
            ['bucket_id'],
            [u'files_bucket.id'],
            ondelete='RESTRICT'),
        sa.ForeignKeyConstraint(
            ['file_id'],
            [u'files_files.id'],
            ondelete='RESTRICT'),
        sa.PrimaryKeyConstraint('upload_id'),
        sa.UniqueConstraint('upload_id', 'bucket_id', 'key', name='uix_item')
    )
    op.create_table(
        'files_object',
        created(),
        updated(),
        sa.Column(
            'bucket_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        key(nullable=False),
        sa.Column(
            'version_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        sa.Column(
            'file_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=True),
        sa.Column(
            '_mimetype', sa.String(
                length=255), nullable=True),
        sa.Column('is_head', sa.Boolean(name='is_head'), nullable=False),
        sa.ForeignKeyConstraint(
            ['bucket_id'],
            [u'files_bucket.id'],
            ondelete='RESTRICT'),
        sa.ForeignKeyConstraint(
            ['file_id'],
            [u'files_files.id'],
            ondelete='RESTRICT'),
        sa.PrimaryKeyConstraint('bucket_id', 'key', 'version_id')
    )
    op.create_index(
        op.f('ix_files_object__mimetype'),
        'files_object',
        ['_mimetype'],
        unique=False
    )
    op.create_table(
        'files_multipartobject_part',
        created(),
        updated(),
        sa.Column(
            'upload_id',
            sqlalchemy_utils.types.uuid.UUIDType(),
            nullable=False),
        sa.Column(
            'part_number',
            sa.Integer(),
            autoincrement=False,
            nullable=False),
        sa.Column('checksum', sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ['upload_id'],
            [u'files_multipartobject.upload_id'],
            ondelete='RESTRICT'),
        sa.PrimaryKeyConstraint('upload_id', 'part_number')
    )
374863
def upgrade(directory, sql, tag, x_arg, revision):
    """"""
    _upgrade(directory, revision, sql, tag, x_arg)
236671
def upgrade():
    """"""
    if op._proxy.migration_context.dialect.name == 'postgresql':
        op.alter_column(
            'records_metadata',
            'json',
            type_=sa.dialects.postgresql.JSONB,
            postgresql_using='json::text::jsonb'
        )
991413
private void dbUpgrade()
    {
        DbConn cnx = this.getConn();
        Map<String, Object> rs = null;
        int db_schema_version = 0;
        try
        {
            rs = cnx.runSelectSingleRow("version_select_latest");
            db_schema_version = (Integer) rs.get("VERSION_D1");
        }
        catch (Exception e)
        {
            // Database is to be created, so version 0 is OK.
        }
        cnx.rollback();

        if (SCHEMA_VERSION > db_schema_version)
        {
            jqmlogger.warn("Database is being upgraded from version {} to version {}", db_schema_version, SCHEMA_VERSION);

            // Upgrade scripts are named from_to.sql with 5 padding (e.g. 00000_00003.sql)
            // We try to find the fastest path (e.g. a direct 00000_00005.sql for creating a version 5 schema from nothing)
            // This is a simplistic and non-optimal algorithm as we try only a single path (no going back)

            int loop_from = db_schema_version;
            int to = db_schema_version;
            List<String> toApply = new ArrayList<>();
            toApply.addAll(adapter.preSchemaCreationScripts());

            while (to != SCHEMA_VERSION)
            {
                boolean progressed = false;
                for (int loop_to = SCHEMA_VERSION; loop_to > db_schema_version; loop_to--)
                {
                    String migrationFileName = String.format("/sql/%05d_%05d.sql", loop_from, loop_to);
                    jqmlogger.debug("Trying migration script {}", migrationFileName);
                    if (Db.class.getResource(migrationFileName) != null)
                    {
                        toApply.add(migrationFileName);
                        to = loop_to;
                        loop_from = loop_to;
                        progressed = true;
                        break;
                    }
                }

                if (!progressed)
                {
                    break;
                }
            }
            if (to != SCHEMA_VERSION)
            {
                throw new DatabaseException(
                        "There is no migration path from version " + db_schema_version + " to version " + SCHEMA_VERSION);
            }

            for (String s : toApply)
            {
                jqmlogger.info("Running migration script {}", s);
                ScriptRunner.run(cnx, s);
            }
            cnx.commit(); // Yes, really. For advanced DB!

            cnx.close(); // HSQLDB does not refresh its schema without this.
            cnx = getConn();

            cnx.runUpdate("version_insert", SCHEMA_VERSION, SCHEMA_COMPATIBLE_VERSION);
            cnx.commit();
            jqmlogger.info("Database is now up to date");
        }
        else
        {
            jqmlogger.info("Database is already up to date");
        }
        cnx.close();
    }
667596
def db_version(conn, pkg_name='openquake.server.db.schema.upgrades'):
    
    upgrader = UpgradeManager.instance(conn, pkg_name)
    return max(upgrader.get_db_versions(conn))
928059
def create_package_version(requirement):
    """"""
    try:
        PackageVersion(requirement=requirement).save()
        logger.info("Package '%s' added.", requirement.name)  # noqa
    except IntegrityError:
        logger.info("Package '%s' already exists.", requirement.name)
353109
def install (self):
        """"""
        outs = super(MyInstallLib, self).install()
        infile = self.create_conf_file()
        outfile = os.path.join(self.install_dir, os.path.basename(infile))
        self.copy_file(infile, outfile)
        outs.append(outfile)
        return outs
283016
def setup():
    
    try:
        db.bind(**config.database_config)
    except OSError:
        # Attempted to connect to a file-based database where the file didn't
        # exist
        db.bind(**config.database_config, create_db=True)

    rebuild = True

    try:
        db.generate_mapping(create_tables=True)
        with orm.db_session:
            version = GlobalConfig.get(key='schema_version')
            if version and version.int_value != SCHEMA_VERSION:
                logger.info("Existing database has schema version %d",
                            version.int_value)
            else:
                rebuild = False
    except:  # pylint:disable=bare-except
        logger.exception("Error mapping schema")

    if rebuild:
        logger.info("Rebuilding schema")
        try:
            db.drop_all_tables(with_all_data=True)
            db.create_tables()
        except:
            raise RuntimeError("Unable to upgrade schema automatically; please " +
                               "delete the existing database and try again.")

    with orm.db_session:
        if not GlobalConfig.get(key='schema_version'):
            logger.info("setting schema version to %d", SCHEMA_VERSION)
            GlobalConfig(key='schema_version',
                         int_value=SCHEMA_VERSION)
            orm.commit()
836693
def _setup(self):
        
        self._populate_local()
        try:
            self._populate_latest()
        except Exception as e:
            self.log.exception('Unable to retrieve latest %s version information', self.meta_name)
        self._sort()