From 899dfa6c536beda923aa4da0d3b35dcad4c0fc62 Mon Sep 17 00:00:00 2001 From: Jerrie-Aries Date: Sun, 10 Dec 2023 14:20:31 +0000 Subject: [PATCH] `invites` plugin v2.1.0: - Remove the temporary `migration.py` --- invites/core/migration.py | 89 --------------------------------------- invites/info.json | 2 +- invites/invites.py | 6 --- 3 files changed, 1 insertion(+), 96 deletions(-) delete mode 100644 invites/core/migration.py diff --git a/invites/core/migration.py b/invites/core/migration.py deleted file mode 100644 index 02c248c..0000000 --- a/invites/core/migration.py +++ /dev/null @@ -1,89 +0,0 @@ -from typing import Any, Dict - -from core.models import getLogger - - -logger = getLogger(__name__) - - -def is_multiline(data: Dict[str, Any]) -> bool: - fields = [ - str(data.get("invite_channel")), - str(data.get("invite_code")), - str(data.get("inviter", {}).get("id")), - ] - return any(("\n" in val for val in fields)) - - -def _resolve_invite_data(data: Dict[str, Any]) -> Dict[str, Any]: - invite = { - "code": data["invite_code"], - "inviter": {"id": str(data["inviter"]["id"])}, - "channel": {"id": data["invite_channel"].strip("<#>")}, - "created_at": None, - "expires_at": None, - "max_age": None, - "max_uses": None, - } - return invite - - -async def db_migration(cog) -> None: - """ - A helper to migrate documents in the database since our new data scheme is now a - bit different from the old one. - """ - db = cog.db - search_query = {"$and": [{"user_id": {"$exists": True}}, {"inviter": {"$exists": True}}]} - count = await db.count_documents(search_query) - if not count: - # no old user data, we consider it as migrated - cog.config.set("migrated", True) - await cog.config.update() - return - - to_insert = {} - logger.debug(f"Migrating database documents in {db.name}.") - async for old_doc in db.find(search_query): - if str(old_doc["user_id"]) in to_insert: - continue - - if old_doc.get("multi", False) or is_multiline(old_doc): - # we just treat these as broken - continue - - invite = _resolve_invite_data(old_doc) - userdoc = { - "_id": str(old_doc["user_id"]), - "guilds": {str(old_doc["guild_id"]): {"invite": invite}}, - } - # find dupe data for same user - find_filter = { - "user_id": old_doc["user_id"], - "guild_id": { - "$ne": old_doc["guild_id"], - }, - } - async for dupe_doc in db.find(find_filter): - # only do if it does not have multiple invites - if not is_multiline(dupe_doc): - ginvite = _resolve_invite_data(dupe_doc) - userdoc["guilds"][str(dupe_doc["guild_id"])] = {"invite": ginvite} - - to_insert[userdoc["_id"]] = userdoc - - try: - await db.delete_many({"_id": {"$ne": "config"}}) - except Exception as exc: - logger.error(f"{type(exc).__name__}: {str(exc)}") - raise exc - - try: - await db.insert_many(to_insert.values()) - except Exception as exc: - logger.error(f"{type(exc).__name__}: {str(exc)}") - raise exc - - cog.config.set("migrated", True) - await cog.config.update() - logger.debug("Migration is now complete.") diff --git a/invites/info.json b/invites/info.json index 518ad48..42defbc 100644 --- a/invites/info.json +++ b/invites/info.json @@ -9,7 +9,7 @@ "\n**Version:**\n`{0}`" ], "authors": ["Jerrie-Aries"], - "version": "2.0.2", + "version": "2.1.0", "bot_version": "4.0.0", "dpy_version": "2.0.0", "cogs_required": ["Extended Utils"] diff --git a/invites/invites.py b/invites/invites.py index 26104e0..767da2b 100644 --- a/invites/invites.py +++ b/invites/invites.py @@ -16,8 +16,6 @@ from .core.models import InviteTracker, PartialInvite -# temp for migration -from .core.migration import db_migration info_json = Path(__file__).parent.resolve() / "info.json" with open(info_json, encoding="utf-8") as f: @@ -87,10 +85,6 @@ async def initialize(self) -> None: await self.populate_config() await self.tracker.populate_invites() - # temp for migration - if not self.config.get("migrated", False): - await db_migration(self) - async def populate_config(self) -> None: """ Populates the config cache with data from database.