[Mutes] Fix data conversion to shema_version 1 and add a note about it (#4934)

* [Mutes] Add a note about conversion to schema_version 1

* Only start the conversion if all_channels

* Actually do what the previous commit said

* okay this time is the right time

* Update mutes.py

* Move conversion to its own method

* Update mutes.py
This commit is contained in:
PredaaA 2021-04-04 02:43:36 +02:00 committed by GitHub
parent 6c338f175b
commit 847444eab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -148,21 +148,30 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass):
schema_version = await self.config.schema_version() schema_version = await self.config.schema_version()
if schema_version == 0: if schema_version == 0:
start = datetime.now() await self._schema_0_to_1()
log.info("Config conversion to schema_version 1 started.") schema_version += 1
await self.config.schema_version.set(schema_version)
async def _schema_0_to_1(self):
"""This contains conversion that adds guild ID to channel mutes data."""
all_channels = await self.config.all_channels() all_channels = await self.config.all_channels()
if not all_channels:
return
start = datetime.now()
log.info(
"Config conversion to schema_version 1 started. This may take a while to proceed..."
)
async for channel_id in AsyncIter(all_channels.keys()): async for channel_id in AsyncIter(all_channels.keys()):
try: try:
if (channel := self.bot.get_channel(channel_id)) is None: if (channel := self.bot.get_channel(channel_id)) is None:
channel = await self.bot.fetch_channel(channel_id) channel = await self.bot.fetch_channel(channel_id)
async with self.config.channel_from_id(channel_id) as muted_users: async with self.config.channel_from_id(channel_id).muted_users() as muted_users:
for user_id, mute_data in muted_users.items(): for mute_id, mute_data in muted_users.items():
mute_data["guild"] = channel.guild.id mute_data["guild"] = channel.guild.id
except (discord.NotFound, discord.Forbidden): except (discord.NotFound, discord.Forbidden):
await self.config.channel_from_id(channel_id).clear() await self.config.channel_from_id(channel_id).clear()
schema_version += 1
await self.config.schema_version.set(schema_version)
log.info( log.info(
"Config conversion to schema_version 1 done. It took %s to proceed.", "Config conversion to schema_version 1 done. It took %s to proceed.",
datetime.now() - start, datetime.now() - start,