Fixes #1916 - Add `created` field to Organization and OrgOut, set on org creation - Add migration to backfill `created` dates from first workflow `created` - Replace `datetime.now()` and `datetime.utcnow()` across app with consistent timezone-aware `utils.dt_now` helper function, which now uses `datetime.now(timezone.utc)`. This is in part to ensure consistency in how we handle datetimes, and also to get ahead of timezone naive datetime creation methods like `datetime.utcnow()` being deprecated in Python 3.12. For more, see: https://blog.miguelgrinberg.com/post/it-s-time-for-a-change-datetime-utcnow-is-now-deprecated
50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
"""
|
|
Migration 0031 - Organization created field
|
|
"""
|
|
|
|
from btrixcloud.migrations import BaseMigration
|
|
|
|
|
|
MIGRATION_VERSION = "0031"
|
|
|
|
|
|
class Migration(BaseMigration):
|
|
"""Migration class."""
|
|
|
|
# pylint: disable=unused-argument
|
|
def __init__(self, mdb, **kwargs):
|
|
super().__init__(mdb, migration_version=MIGRATION_VERSION)
|
|
|
|
async def migrate_up(self):
|
|
"""Perform migration up.
|
|
|
|
Add created field to orgs without one, based on first workflow creation date.
|
|
"""
|
|
# pylint: disable=duplicate-code, line-too-long
|
|
orgs_db = self.mdb["organizations"]
|
|
crawl_configs_db = self.mdb["crawl_configs"]
|
|
|
|
cursor = orgs_db.find({"created": None})
|
|
async for org_dict in cursor:
|
|
oid = org_dict.get("_id")
|
|
try:
|
|
cursor = crawl_configs_db.find({"oid": oid}).sort("created", 1).limit(1)
|
|
workflows = await cursor.to_list(length=1)
|
|
workflow_dict = workflows[0]
|
|
workflow_created = workflow_dict.get("created")
|
|
await orgs_db.find_one_and_update(
|
|
{"_id": oid}, {"$set": {"created": workflow_created}}
|
|
)
|
|
print(f"Created date set for org {oid}", flush=True)
|
|
except IndexError:
|
|
print(
|
|
f"Error setting created date for org {oid}, no workflows exist to set date from",
|
|
flush=True,
|
|
)
|
|
# pylint: disable=broad-exception-caught
|
|
except Exception as err:
|
|
print(
|
|
f"Error setting created date for org {oid} from first workflow: {err}",
|
|
flush=True,
|
|
)
|