From 950844dc9233f373a365900b36cfe87493dc69ac Mon Sep 17 00:00:00 2001 From: Tessa Walsh Date: Thu, 18 Jan 2024 16:59:40 -0500 Subject: [PATCH] Add migration to fix issues with previous migrations (#1480) Fixes #1479 - Update null crawlTimeouts in db from null to 0 - Update crawlerChannel in configmaps --- backend/btrixcloud/db.py | 2 +- ...ration_0025_workflow_db_configmap_fixes.py | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 backend/btrixcloud/migrations/migration_0025_workflow_db_configmap_fixes.py diff --git a/backend/btrixcloud/db.py b/backend/btrixcloud/db.py index eb70a8c8..d91c6d2a 100644 --- a/backend/btrixcloud/db.py +++ b/backend/btrixcloud/db.py @@ -16,7 +16,7 @@ from pymongo.errors import InvalidName from .migrations import BaseMigration -CURR_DB_VERSION = "0024" +CURR_DB_VERSION = "0025" # ============================================================================ diff --git a/backend/btrixcloud/migrations/migration_0025_workflow_db_configmap_fixes.py b/backend/btrixcloud/migrations/migration_0025_workflow_db_configmap_fixes.py new file mode 100644 index 00000000..6c159e69 --- /dev/null +++ b/backend/btrixcloud/migrations/migration_0025_workflow_db_configmap_fixes.py @@ -0,0 +1,49 @@ +""" +Migration 0025 -- fix workflow database and configmap issues. +""" +from btrixcloud.crawlmanager import CrawlManager +from btrixcloud.migrations import BaseMigration +from btrixcloud.models import CrawlConfig, UpdateCrawlConfig + + +MIGRATION_VERSION = "0025" + + +class Migration(BaseMigration): + """Migration class.""" + + def __init__(self, mdb, migration_version=MIGRATION_VERSION): + super().__init__(mdb, migration_version) + + async def migrate_up(self): + """Perform migration up. + + Set crawlTimeout to 0 in any workflows where it is not set, and + update configmap for each workflow to include crawlerChannel. + """ + mdb_crawl_configs = self.mdb["crawl_configs"] + try: + await mdb_crawl_configs.update_many( + {"crawlTimeout": None}, + {"$set": {"crawlTimeout": 0}}, + ) + # pylint: disable=broad-except + except Exception: + print( + "Error updating null crawlconfig crawlTimeouts to 0", + flush=True, + ) + + crawl_manager = CrawlManager() + async for config_dict in mdb_crawl_configs.find({}): + config = CrawlConfig.from_dict(config_dict) + try: + await crawl_manager.update_crawl_config( + config, UpdateCrawlConfig(crawlerChannel=config.crawlerChannel) + ) + # pylint: disable=broad-except + except Exception as exc: + print( + f"Skipping configmap migration for config {config.id} due to error", + exc, + )