Skip to content

Commit

Permalink
remove missing channels' messages from db (follow-up to ff041be)
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick committed Apr 16, 2021
1 parent 83d7a63 commit 4c4e90c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions librarian/discord/cogs/background/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ async def sort_for_updates(self, pulls: typing.List[storage.models.pull.Pull]) -
async def handle_update_exception(self, exc: errors.LibrarianException, item: typing.Tuple[int, int]):
if isinstance(exc, errors.NoDiscordChannel):
await self.bot.settings.reset(exc.channel_id)
self.storage.discord.delete_channel_messages(exc.channel_id)

async def status(self) -> dict:
# FIXME: make something useful here
Expand Down
6 changes: 6 additions & 0 deletions librarian/storage/models/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def delete_message(self, message_id, channel_id, s):
DiscordMessage.channel_id == channel_id,
).delete()

@utils.optional_session
def delete_channel_messages(self, channel_id, s) -> int:
return s.query(DiscordMessage).filter(
DiscordMessage.channel_id == channel_id,
).delete()

@utils.optional_session
def messages_by_pull_numbers(self, *pull_numbers: typing.List[int], s: orm.Session = None):
""" Return all known messages that are tied to the specified pulls. """
Expand Down
2 changes: 2 additions & 0 deletions tests/discord/cogs/background/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ async def test__exception_handling_no_channel(
response = collections.namedtuple("Response", "status reason")(404, "testing stuff")
client.fetch_channel = mocker.AsyncMock(side_effect=discord_errors.NotFound(response, "error"))
client.settings.reset = mocker.AsyncMock()
client.storage.discord.delete_channel_messages = mocker.Mock()
await monitor.sort_for_updates([pp])

client.settings.reset.assert_called()
client.storage.discord.delete_channel_messages.assert_called()
args, _ = client.settings.reset.call_args
assert args == (123,)
16 changes: 16 additions & 0 deletions tests/storage/models/test_discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ def test__delete_message(self, storage, existing_pulls):
storage.discord.delete_message(123, 456)
assert not storage.discord.messages_by_pull_numbers(789)

def test__delete_channel_messages(self, storage, existing_pulls):
assert storage.discord.delete_channel_messages(123) == 0
storage.discord.save_messages(*(
stg.DiscordMessage(
id=i,
channel_id=123,
pull_number=1
)
for i in range(10)
))
storage.discord.save_messages(stg.DiscordMessage(id=15, channel_id=124, pull_number=1))

assert len(storage.discord.messages_by_pull_numbers(1)) == 11
assert storage.discord.delete_channel_messages(123) == 10
assert len(storage.discord.messages_by_pull_numbers(1)) == 1


class TestDiscordUsers:
def test__promote_one(self, storage):
Expand Down

0 comments on commit 4c4e90c

Please sign in to comment.