Add task active duration migration

This commit is contained in:
allegroai 2021-01-05 17:53:44 +02:00
parent 91b1b34a6b
commit 563c451ac9

View File

@ -0,0 +1,24 @@
from pymongo.database import Database
def _add_active_duration(db: Database):
active_duration = "active_duration"
query = {active_duration: {"$eq": None}}
collection = db["task"]
for doc in collection.find(
filter=query, projection=[active_duration, "started", "last_update"]
):
started = doc.get("started")
last_update = doc.get("last_update")
if started and last_update and doc.get(active_duration) is None:
collection.update_one(
{"_id": doc["_id"]},
{"$set": {active_duration: (last_update - started).total_seconds()}},
)
def migrate_backend(db: Database):
"""
Add active_duration field to tasks
"""
_add_active_duration(db)