def migration_context(from_archive_config, to_archive_config, do_lock=True): """ Provides a context for upgrades including a lock on the db to ensure only one upgrade process at a time doing checks. Use a postgresql application lock to block schema updates and serialize checks :param lock_id: the lock id (int) for the lock to acquire :return: """ logger.info('Initializing source archive: {}'.format(from_archive_config)) from_archive = ArchiveManager(from_archive_config) logger.info('Initializing dest archive: {}'.format(to_archive_config)) to_archive = ArchiveManager(to_archive_config) if do_lock: engine = anchore_engine.db.entities.common.get_engine() with db_application_lock( engine, (application_lock_ids['archive_migration']['namespace'], application_lock_ids['archive_migration']['ids']['default'])): yield MigrationContext(from_archive=from_archive, to_archive=to_archive) else: yield MigrationContext(from_archive=from_archive, to_archive=to_archive)
def upgrade_context(lock_id): """ Provides a context for upgrades including a lock on the db to ensure only one upgrade process at a time doing checks. Use a postgresql application lock to block schema updates and serialize checks :param lock_id: the lock id (int) for the lock to acquire :return: """ engine = anchore_engine.db.entities.common.get_engine() from anchore_engine.db.db_locks import db_application_lock, application_lock_ids with db_application_lock(engine, (application_lock_ids['upgrade']['namespace'], lock_id)): versions = get_versions() yield versions