Skip to content

Commit

Permalink
Unify v0.17 migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
allegroai committed Jan 5, 2021
1 parent a8d9088 commit be0cf0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 57 deletions.
48 changes: 48 additions & 0 deletions apiserver/mongo/migrations/0.17.0.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
from datetime import datetime
from typing import Optional

from pymongo.database import Database, Collection

from apiserver.bll.task.artifacts import get_artifact_id
from apiserver.utilities.dicts import nested_get


def _add_active_duration(db: Database):
active_duration_key = "active_duration"
query = {"$or": [{active_duration_key: {"$eq": None}}, {active_duration_key: {"$eq": 0}}]}
collection = db["task"]
for doc in collection.find(
filter=query, projection=[active_duration_key, "status", "started", "completed"]
):
started = doc.get("started")
completed = doc.get("completed")
running = doc.get("status") == "running"
active_duration_value = doc.get(active_duration_key)
if active_duration_value == 0:
collection.update_one(
{"_id": doc["_id"]},
{"$set": {active_duration_key: None}},
)
elif started and active_duration_value is None:
collection.update_one(
{"_id": doc["_id"]},
{"$set": {active_duration_key: _get_active_duration(completed, running, started)}},
)


def _get_active_duration(
completed: datetime, running: bool, started: datetime
) -> Optional[float]:
if running:
return (datetime.utcnow() - started).total_seconds()
elif completed:
return (completed - started).total_seconds()
else:
return None


def migrate_backend(db: Database):
collection: Collection = db["task"]
artifacts_field = "execution.artifacts"
Expand All @@ -17,3 +54,14 @@ def migrate_backend(db: Database):
collection.update_one(
{"_id": doc["_id"]}, {"$set": {artifacts_field: new_artifacts}}
)

_add_active_duration(db)


def migrate_auth(db: Database):
"""
Remove the old indices from the user collections
to enable building of the updated user email index
"""
collection: Collection = db["user"]
collection.drop_indexes()
45 changes: 0 additions & 45 deletions apiserver/mongo/migrations/0.17.2.py

This file was deleted.

12 changes: 0 additions & 12 deletions apiserver/mongo/migrations/0.17.3.py

This file was deleted.

0 comments on commit be0cf0c

Please sign in to comment.