Fix 0015 migration (#1154)

* migration: fix 0015 migration to ensure it reads the correct mongo collection, avoid variable overwrites and and uses org _id field. fixes #1153
This commit is contained in:
Ilya Kreymer 2023-09-08 08:17:40 -07:00 committed by GitHub
parent 2967f1e320
commit e75b207f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ from btrixcloud.migrations import BaseMigration
MIGRATION_VERSION = "0015"
# pylint: disable=too-many-locals
class Migration(BaseMigration):
"""Migration class."""
@ -18,29 +19,29 @@ class Migration(BaseMigration):
Calculate and store org storage usage
"""
orgs = self.mdb["orgs"]
crawls = self.mdb["crawls"]
profiles = self.mdb["profiles"]
mdb_orgs = self.mdb["organizations"]
mdb_crawls = self.mdb["crawls"]
mdb_profiles = self.mdb["profiles"]
orgs = [res async for res in orgs.find({})]
orgs = [res async for res in mdb_orgs.find({})]
for org in orgs:
oid = org.get("id")
oid = org.get("_id")
bytes_stored = 0
crawls = [res async for res in crawls.find({"oid": oid})]
crawls = [res async for res in mdb_crawls.find({"oid": oid})]
for crawl in crawls:
for crawl_file in crawl.get("files", []):
bytes_stored += crawl_file.get("size", 0)
profiles = [res async for res in profiles.find({"oid": org["id"]})]
profiles = [res async for res in mdb_profiles.find({"oid": oid})]
for profile in profiles:
profile_file = profile.get("resource")
if profile_file:
bytes_stored += profile_file.get("size", 0)
try:
await orgs.find_one_and_update(
res = await mdb_orgs.find_one_and_update(
{"_id": oid}, {"$set": {"bytesStored": bytes_stored}}
)
# pylint: disable=broad-exception-caught