# Buggy code
async def _migrate_config(self, from_version: int, to_version: int):
    database_entries = []
    time_now = str(datetime.datetime.now(datetime.timezone.utc))
    if from_version == to_version:
        return
    elif from_version < to_version:
        all_guild_data = await self.config.all_guilds()
        all_playlist = {}
        for guild_id, guild_data in all_guild_data.items():
            temp_guild_playlist = guild_data.pop("playlists", None)
            if temp_guild_playlist:
                guild_playlist = {}
                for count, (name, data) in enumerate(temp_guild_playlist.items(), 1):
                    if not data or not name:
                        continue
                    playlist = {"id": count, "name": name, "guild": int(guild_id)}
                    playlist.update(data)
                    guild_playlist[str(count)] = playlist

                    tracks_in_playlist = data.get("tracks", []) or []
                    for t in tracks_in_playlist:
                        uri = t.get("info", {}).get("uri")
                        if uri:
                            t = {"loadType": "V2_COMPAT", "tracks": [t], "query": uri}
                            database_entries.append(
                                {
                                    "query": uri,
                                    "data": json.dumps(t),
                                    "last_updated": time_now,
                                    "last_fetched": time_now,
                                }
                            )
                if guild_playlist:
                    all_playlist[str(guild_id)] = guild_playlist
        await self.config.custom(PlaylistScope.GUILD.value).set(all_playlist)
        # new schema is now in place
        await self.config.schema_version.set(_SCHEMA_VERSION)

        # migration done, now let's delete all the old stuff
        for guild_id in all_guild_data:
            await self.config.guild(
                cast(discord.Guild, discord.Object(id=guild_id))
            ).clear_raw("playlists")
    if database_entries and HAS_SQL:
        await self.music_cache.insert("lavalink", database_entries)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "/home/martinebots/martinebotscogs/cogs_modified/audio/audio.py", line 3183, in play
await self._enqueue_tracks(ctx, query)
File "/home/martinebots/martinebotscogs/cogs_modified/audio/audio.py", line 3885, in _enqueue_tracks
result, called_api = await self.music_cache.lavalink_query(ctx, player, query)
File "/home/martinebots/martinebotscogs/cogs_modified/audio/apis.py", line 911, in lavalink_query
results = await player.load_tracks(query)
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/lavalink/rest_api.py", line 300, in load_tracks
self.__check_node_ready()
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/lavalink/rest_api.py", line 268, in __check_node_ready
raise RuntimeError("Cannot execute REST request when node not ready.")
RuntimeError: Cannot execute REST request when node not ready.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/martinebots/martinebotsvenv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: Cannot execute REST request when node not ready.```

# Fixed code
async def _migrate_config(self, from_version: int, to_version: int) -> None:
    database_entries = []
    time_now = str(datetime.datetime.now(datetime.timezone.utc))
    if from_version == to_version:
        return
    if from_version < 2 <= to_version:
        all_guild_data = await self.config.all_guilds()
        all_playlist = {}
        for guild_id, guild_data in all_guild_data.items():
            temp_guild_playlist = guild_data.pop("playlists", None)
            if temp_guild_playlist:
                guild_playlist = {}
                for count, (name, data) in enumerate(temp_guild_playlist.items(), 1):
                    if not data or not name:
                        continue
                    playlist = {"id": count, "name": name, "guild": int(guild_id)}
                    playlist.update(data)
                    guild_playlist[str(count)] = playlist

                    tracks_in_playlist = data.get("tracks", []) or []
                    for t in tracks_in_playlist:
                        uri = t.get("info", {}).get("uri")
                        if uri:
                            t = {"loadType": "V2_COMPACT", "tracks": [t], "query": uri}
                            database_entries.append(
                                {
                                    "query": uri,
                                    "data": json.dumps(t),
                                    "last_updated": time_now,
                                    "last_fetched": time_now,
                                }
                            )
                if guild_playlist:
                    all_playlist[str(guild_id)] = guild_playlist
        await self.config.custom(PlaylistScope.GUILD.value).set(all_playlist)
        # new schema is now in place
        await self.config.schema_version.set(_SCHEMA_VERSION)

        # migration done, now let's delete all the old stuff
        for guild_id in all_guild_data:
            await self.config.guild(
                cast(discord.Guild, discord.Object(id=guild_id))
            ).clear_raw("playlists")
    if from_version < 3 <= to_version:
        for scope in PlaylistScope.list():
            scope_playlist = await get_all_playlist_for_migration23(scope)
            for p in scope_playlist:
                await p.save()
            await self.config.custom(scope).clear()
        await self.config.schema_version.set(_SCHEMA_VERSION)

    if database_entries:
        await self.music_cache.database.insert("lavalink", database_entries)