browsertrix/backend/btrixcloud/migrations/migration_0007_colls_and_config_update.py
Tessa Walsh 9c7a312a4c
Rework collections to track collections in Crawl (#878)
* Track collections in Crawl rather than crawls in Collection
* Add delete collection API endpoint and tests
* Precompute collection crawlCount, pageCount, and tags and add them to
GET collection responses
* Add modified field to Collection
* Update collection replay.json method
* Make add and remove crawls accept list of crawl ids
* Auto-add new workflow crawls to collections when they successfully
complete via CrawlConfig.autoAddCollections field
* Move long-running post-crawl operator tasks into asyncio task
* Make CrawlConfig.autoAddCollections updatable via /update API endpoint
2023-05-25 15:41:50 -04:00

48 lines
1.6 KiB
Python

"""
Migration 0007 - Workflows changes
- Rename colls to autoAddCollections
- Re-calculate workflow crawl stats to populate crawlSuccessfulCount
"""
from btrixcloud.crawlconfigs import update_config_crawl_stats
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
configs = [res async for res in crawl_configs.find({"inactive": {"$ne": True}})]
if not configs:
return
for config in configs:
config_id = config["_id"]
try:
await update_config_crawl_stats(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}})