Update migration to convert user preferences to JSON

This commit is contained in:
allegroai 2020-01-02 15:26:45 +02:00
parent f778837d4b
commit 72213dffa4

View File

@ -1,6 +1,20 @@
import json
from pymongo.database import Database, Collection from pymongo.database import Database, Collection
def migrate_auth(db: Database): def migrate_auth(db: Database):
collection: Collection = db["user"] collection: Collection = db["user"]
if "name_1_company_1" in [doc["name"] for doc in collection.list_indexes()]:
collection.drop_index("name_1_company_1") collection.drop_index("name_1_company_1")
def migrate_backend(db: Database):
collection: Collection = db["user"]
users = collection.find(
{"preferences": {"$exists": True, "$ne": None, "$type": "object"}}
)
for doc in users:
collection.update_one(
{"_id": doc["_id"]}, {"$set": {"preferences": json.dumps(doc["preferences"])}}
)