Skip to content

Commit

Permalink
optimize meowid migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tnix100 authored Aug 13, 2024
1 parent 9f14ce6 commit a8dc5ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import secrets
from radix import Radix

from meoid import gen_id_injected, MEOWER_EPOCH
from meowid import gen_id_injected, MEOWER_EPOCH
from utils import log

CURRENT_DB_VERSION = 10
Expand Down Expand Up @@ -308,21 +308,29 @@ def get_total_pages(collection: str, query: dict, page_size: int = 25) -> int:
}})


log("[Migrator] Adding Meoid to posts")
for post in db.get_collection("posts").find({"meoid": {"$exists": False}}):
db.get_collection("posts").update_one({"_id": post["_id"]}, {"$set": {"meowid": gen_id_injected(post["t"]["e"])}})
log("[Migrator] Adding MeowID to posts")
updates: list[pymongo.UpdateOne] = []
for post in db.get_collection("posts").find({"meowid": {"$exists": False}}, projection={"_id": 1, "t.e": 1}):
updates.append(pymongo.UpdateOne({"_id": post["_id"]}, {"$set": {"meowid": gen_id_injected(post["t"]["e"])}}))
db.get_collection("posts").bulk_write(updates)

log("[Migrator] Adding Meoid to chats")
for chat in db.get_collection("chats").find({"meoid": {"$exists": False}}):
db.get_collection("chats").update_one({"_id": chat["_id"]}, {"$set": {"meowid": gen_id_injected(chat["created"])}})
log("[Migrator] Adding MeowID to chats")
updates: list[pymongo.UpdateOne] = []
for chat in db.get_collection("chats").find({"meowid": {"$exists": False}}, projection={"_id": 1, "created": 1}):
time = chat.get("created", 0)
if time is None:
time = MEOWER_EPOCH
updates.append(pymongo.UpdateOne({"_id": chat["_id"]}, {"$set": {"meowid": gen_id_injected(time)}}))
db.get_collection("chats").bulk_write(updates)

log("[Migrator] Adding Meoid to usersv0")
for user in db.get_collection("usersv0").find({"meoid": {"$exists": False}}):
log("[Migrator] Adding MeowID to usersv0")
updates: list[pymongo.UpdateOne] = []
for user in db.get_collection("usersv0").find({"meowid": {"$exists": False}}, projection={"_id": 1, "created": 1}):
time = user.get("created", 0)
if time is None:
time = MEOWER_EPOCH
db.get_collection("usersv0").update_one({"_id": user["_id"]}, {"$set": {"meowid": gen_id_injected(time)}})
db.get_collection("user_settings").update_one({"_id": user["_id"]}, {"$set": {"meowid": gen_id_injected(time)}})
updates.append(pymongo.UpdateOne({"_id": user["_id"]}, {"$set": {"meowid": gen_id_injected(time)}}))
db.get_collection("usersv0").bulk_write(updates)



Expand Down
File renamed without changes.

0 comments on commit a8dc5ab

Please sign in to comment.