Exemplo n.º 1
0
def api():
    """DB metadata API endpoint."""
    with get_db_connection() as db_conn:
        return jsonify(Version().dump(
            dict(code_db_schema_version=code_db_schema_version,
                 potential_whitespace_imsis_msisdns=False,
                 schema_version=utils.query_db_schema_version(db_conn))).data)
Exemplo n.º 2
0
def check(ctx):
    """
    Checks whether DB schema matches software DB version.

    :param ctx: current cli context obj
    """
    db_config = common.ensure_config(ctx).db_config

    logger = logging.getLogger('dirbs.db')
    logger.info('Querying DB schema version for DB %s on host %s',
                db_config.database, db_config.host)

    with utils.create_db_connection(db_config) as conn:
        version = utils.query_db_schema_version(conn)

    logger.info('Code schema version: %d', code_db_schema_version)
    if version is None:
        logger.error(
            'DB has not been clean installed. Maybe this DB pre-dates the version checking?'
        )
        logger.error('DB schema version unknown.')
        # Exit code is used to determine if schema has(exit code:0) or has not(exit code:1) been installed.
        # Non-zero exit code triggers installation of schema at entrypoint of processing container.
        sys.exit(1)
    else:
        logger.info('DB schema version: %s', str(version))
        if version < code_db_schema_version:
            logger.error('DB schema older than code.')
        elif version > code_db_schema_version:
            logger.error('DB schema newer than code.')
        else:
            logger.info('Schema versions match between code and DB.')
Exemplo n.º 3
0
def version() -> jsonify:
    """DB metadata API endpoint."""
    with get_db_connection() as db_conn:
        return jsonify(
            Version().dump(
                dict(code_db_schema_version=code_db_schema_version,
                     db_schema_version=utils.query_db_schema_version(db_conn))).data)
Exemplo n.º 4
0
def version() -> jsonify:
    """DB metadata API endpoint."""
    with get_db_connection() as db_conn:
        return jsonify(Version().dump(
            dict(source_code_version=dirbs_core_version,
                 code_db_schema_version=code_db_schema_version,
                 db_schema_version=utils.query_db_schema_version(db_conn),
                 report_schema_version=report_schema_version)).data)
Exemplo n.º 5
0
def upgrade(ctx):
    """
    Upgrades the current DB schema to the version supported by this code using migration scripts.

    :param ctx: current cli context obj
    """
    logger = logging.getLogger('dirbs.db')
    config = common.ensure_config(ctx)
    db_config = config.db_config
    needs_analyze = False
    with utils.create_db_connection(db_config) as conn:
        logger.info('Querying DB schema version for DB %s on host %s',
                    db_config.database, db_config.host)
        with conn.cursor() as cur:
            try:
                version = utils.query_db_schema_version(conn)
            except ProgrammingError:
                logger.warn(
                    'Could not determine current schema version. Assuming no version'
                )
                version = None

            if version is None:
                logger.error(
                    'DB currently not installed or version number could not be determined. Can\'t upgrade'
                )
                sys.exit(1)

            if version < min_schema_version:
                logger.error(
                    'Current DB schema is older than DIRBS 4.0.0. Can\'t upgrade'
                )
                sys.exit(1)

            if version > code_db_schema_version:
                logger.error('DB schema newer than code. Can\'t upgrade')
                sys.exit(1)

            if version != code_db_schema_version:
                logger.info('Upgrading DB schema from version %d to %d',
                            version, code_db_schema_version)

                # If we're upgrading, make sure we schedule a full ANALYZE outside the transaction later
                needs_analyze = True

                # Set our role here so that new objects get created with dirbs_core_power_user as owner by default
                with utils.db_role_setter(conn,
                                          role_name='dirbs_core_power_user'):
                    for old_version in range(version, code_db_schema_version):
                        new_version = old_version + 1
                        # Check if there is a special migration class, otherwise use standard SQL file
                        try:
                            module_name = 'dirbs.schema_migrators.v{0}_upgrade'.format(
                                new_version)
                            module = importlib.import_module(module_name)
                            logger.info('Running Python migration script: %s',
                                        module_name)
                            migrator = module.migrator()
                            migrator.upgrade(conn)
                        except ImportError as ex:
                            script_name = 'sql/migration_scripts/v{0:d}_upgrade.sql'.format(
                                new_version)
                            logger.info('Running SQL migration script: %s',
                                        script_name)
                            sql = pkgutil.get_data('dirbs', script_name)
                            cur.execute(sql)

                        # We commit after every version upgrade
                        utils.set_db_schema_version(conn, new_version)
                        conn.commit()

                logger.info(
                    'Successfully updated schema - DB schema version is now %d',
                    code_db_schema_version)
                # Can't do anything until we know the schema is the right version
                _store_job_metadata(config, 'upgrade')
            else:
                logger.info('DB schema is already latest version')

            # Schedule a full ANALYZE at the end of an upgrade
            if needs_analyze:
                logger.info(
                    'Running ANALYZE of entire database after upgrade...')
                cur.execute('ANALYZE')
                logger.info(
                    'Finished running ANALYZE of entire database after upgrade'
                )