diff --git a/apiserver/mongo/migrations/0.17.1.py b/apiserver/mongo/migrations/0.17.1.py new file mode 100644 index 0000000..09edea5 --- /dev/null +++ b/apiserver/mongo/migrations/0.17.1.py @@ -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)