browsertrix/backend/btrixcloud/migrations/migration_0007_colls_and_config_update.py
Ilya Kreymer 7eac0fdf95
optimization: convert all uses of 'async for' to use iterator directly (#1229)
- optimization: convert all uses of 'async for' to use iterator directly instead of converting to list to avoid
unbounded size lists
- additional cursor.to_list() to async for conversions for stats computation, simply crawlconfigs stats computation

---------
Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
2023-09-28 12:31:08 -07:00

44 lines
1.5 KiB
Python

"""
Migration 0007 - Workflows changes
- Rename colls to autoAddCollections
- Re-calculate workflow crawl stats to populate crawlSuccessfulCount
"""
from btrixcloud.crawlconfigs import stats_recompute_all
from btrixcloud.migrations import BaseMigration
MIGRATION_VERSION = "0007"
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."""
# pylint: disable=duplicate-code
crawl_configs = self.mdb["crawl_configs"]
crawls = self.mdb["crawls"]
# Update workflows crawl stats to populate crawlSuccessfulCount
async for config in crawl_configs.find({"inactive": {"$ne": True}}):
config_id = config["_id"]
try:
await stats_recompute_all(crawl_configs, crawls, config_id)
# pylint: disable=broad-exception-caught
except Exception as err:
print(f"Unable to update workflow {config_id}: {err}", flush=True)
# Make sure crawls have collections array
await crawls.update_many({"collections": None}, {"$set": {"collections": []}})
# Rename colls to autoAddCollections
await crawl_configs.update_many({}, {"$unset": {"autoAddCollections": 1}})
await crawl_configs.update_many(
{}, {"$rename": {"colls": "autoAddCollections"}}
)
await crawl_configs.update_many({}, {"$unset": {"colls": 1}})