src/dirbs/cli/db.py
def upgrade(ctx):  # noqa: C901
    """Upgrades the current DB schema to the version supported by this code using migration scripts.

    #TODO: fix suppressed C901 (upgrade is too complex)
    """
    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:
                            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')

            # battle for the whitelist database migrations begins here
            if config.operational_config.activate_whitelist:
                try:
                    wl_version = utils.query_wl_db_schema_version(conn)
                except ProgrammingError:
                    logger.warning('Could not determine current whitelist schema version. Assuming no version')
                    wl_version = None

                if wl_version is None:
                    logger.error('Whitelist DB currently not installed or version number could not be determined. '
                                 "Can\'t upgrade")
                    sys.exit(1)
                if wl_version > wl_db_schema_version:
                    logger.error("Whitelist DB schema newer than code. Can\'t upgrade")
                    sys.exit(1)
                if wl_version != wl_db_schema_version:
                    logger.info('Upgrading Whitelist DB schema version %d to %d', wl_version, wl_db_schema_version)
                    with utils.db_role_setter(conn, role_name='dirbs_core_power_user'):
                        for old_version in range(wl_version, wl_db_schema_version):
                            new_version = old_version + 1
                            try:
                                module_name = 'dirbs.schema_migrators.whitelist.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:
                                script_name = 'sql/migration_scripts/whitelist/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)

                            utils.set_wl_db_schema_version(conn, new_version)
                            conn.commit()
                    logger.info('Successfully updated whitelist schema - Whitelist DB schema version is now %d',
                                wl_db_schema_version)
                    # Can't do anything until we know the schema is the right version
                    _store_job_metadata(config, 'wl_upgrade')
                else:
                    logger.info('Whitelist 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')
Upgrades the current DB schema to the version supported by this code using migration scripts.

    #TODO: fix suppressed C901 (upgrade is too complex)
    
Upgrades the current DB schema to the version supported by this code using migration scripts.
TODO: fix suppressed C901 (upgrade is too complex)
Upgrades the current DB schema to the version supported by this code using migration scripts . TODO : fix suppressed C901 ( upgrade is too complex )
def upgrade(ctx):  
    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)
                needs_analyze = True
                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
                        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:
                            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)
                        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)
                _store_job_metadata(config, 'upgrade')
            else:
                logger.info('DB schema is already latest version')
            if config.operational_config.activate_whitelist:
                try:
                    wl_version = utils.query_wl_db_schema_version(conn)
                except ProgrammingError:
                    logger.warning('Could not determine current whitelist schema version. Assuming no version')
                    wl_version = None
                if wl_version is None:
                    logger.error('Whitelist DB currently not installed or version number could not be determined. '
                                 "Can\'t upgrade")
                    sys.exit(1)
                if wl_version > wl_db_schema_version:
                    logger.error("Whitelist DB schema newer than code. Can\'t upgrade")
                    sys.exit(1)
                if wl_version != wl_db_schema_version:
                    logger.info('Upgrading Whitelist DB schema version %d to %d', wl_version, wl_db_schema_version)
                    with utils.db_role_setter(conn, role_name='dirbs_core_power_user'):
                        for old_version in range(wl_version, wl_db_schema_version):
                            new_version = old_version + 1
                            try:
                                module_name = 'dirbs.schema_migrators.whitelist.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:
                                script_name = 'sql/migration_scripts/whitelist/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)
                            utils.set_wl_db_schema_version(conn, new_version)
                            conn.commit()
                    logger.info('Successfully updated whitelist schema - Whitelist DB schema version is now %d',
                                wl_db_schema_version)
                    _store_job_metadata(config, 'wl_upgrade')
                else:
                    logger.info('Whitelist DB schema is already latest version')
            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')
def upgrade ( ctx ) : 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 ) needs_analyze = True 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 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 : 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 ) 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 ) _store_job_metadata ( config , 'upgrade' ) else : logger . info ( 'DB schema is already latest version' ) if config . operational_config . activate_whitelist : try : wl_version = utils . query_wl_db_schema_version ( conn ) except ProgrammingError : logger . warning ( 'Could not determine current whitelist schema version. Assuming no version' ) wl_version = None if wl_version is None : logger . error ( 'Whitelist DB currently not installed or version number could not be determined. ' "Can\'t upgrade" ) sys . exit ( 1 ) if wl_version > wl_db_schema_version : logger . error ( "Whitelist DB schema newer than code. Can\'t upgrade" ) sys . exit ( 1 ) if wl_version != wl_db_schema_version : logger . info ( 'Upgrading Whitelist DB schema version %d to %d' , wl_version , wl_db_schema_version ) with utils . db_role_setter ( conn , role_name = 'dirbs_core_power_user' ) : for old_version in range ( wl_version , wl_db_schema_version ) : new_version = old_version + 1 try : module_name = 'dirbs.schema_migrators.whitelist.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 : script_name = 'sql/migration_scripts/whitelist/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 ) utils . set_wl_db_schema_version ( conn , new_version ) conn . commit ( ) logger . info ( 'Successfully updated whitelist schema - Whitelist DB schema version is now %d' , wl_db_schema_version ) _store_job_metadata ( config , 'wl_upgrade' ) else : logger . info ( 'Whitelist DB schema is already latest version' ) 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' )
Upgrades the current DB schema to the version supported by this code using migration scripts.
Upgrades the current DB schema to the version supported by this code using migration scripts .
# noqa: C901 """Upgrades the current DB schema to the version supported by this code using migration scripts.

    #TODO: fix suppressed C901 (upgrade is too complex)
    """ # If we're upgrading, make sure we schedule a full ANALYZE outside the transaction later # Set our role here so that new objects get created with dirbs_core_power_user as owner by default # Check if there is a special migration class, otherwise use standard SQL file # We commit after every version upgrade # Can't do anything until we know the schema is the right version # battle for the whitelist database migrations begins here # Can't do anything until we know the schema is the right version # Schedule a full ANALYZE at the end of an upgrade
ctx