Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Add data migration to cleanup participants
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksellen committed Jul 25, 2023
1 parent dd20653 commit 2bf5db4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
@@ -0,0 +1,23 @@
from datetime import timedelta

from django.db import migrations
from django.db.models import Func, F

from karrot.conversations.models import ConversationParticipant


def cleanup_conversation_participants(apps, schema_editor):
ConversationParticipant = apps.get_model('conversations', 'ConversationParticipant')
ConversationThreadParticipant = apps.get_model('conversations', 'ConversationThreadParticipant')
ConversationParticipant.objects.filter(conversation__group__isnull=False).exclude(conversation__group__members=F('user')).delete()
ConversationThreadParticipant.objects.filter(thread__conversation__group__isnull=False).exclude(thread__conversation__group__members=F('user')).delete()


class Migration(migrations.Migration):
dependencies = [
('conversations', '0042_conversationmessageattachment'),
]

operations = [
migrations.RunPython(cleanup_conversation_participants, reverse_code=migrations.RunPython.noop, elidable=True),
]
73 changes: 73 additions & 0 deletions karrot/conversations/tests/test_data_migrations.py
@@ -0,0 +1,73 @@
from karrot.tests.utils import TestMigrations
from karrot.utils.tests.fake import faker


class TestCleanupConversationParticipantsMigration(TestMigrations):
""" Testing our conversation participant cleanup migration
It's a bit of a basic test, as it doesn't run all the code where
the magic really happens. But it at least ensures it runs in a basic way :)
"""

migrate_from = [
('users', '0027_fix_usernames'),
('groups', '0049_auto_20220930_1506'),
('conversations', '0042_conversationmessageattachment'),
]
migrate_to = [
('groups', '0049_auto_20220930_1506'),
('conversations', '0043__cleanup_conversation_participants'),
]

def setUpBeforeMigration(self, apps):
User = apps.get_model('users', 'User')
Group = apps.get_model('groups', 'Group')
GroupMembership = apps.get_model('groups', 'GroupMembership')
Conversation = apps.get_model('conversations', 'Conversation')
ConversationMessage = apps.get_model('conversations', 'ConversationMessage')
ConversationParticipant = apps.get_model('conversations', 'ConversationParticipant')
ConversationThreadParticipant = apps.get_model('conversations', 'ConversationThreadParticipant')

user = User.objects.create()
group = Group.objects.create(name=faker.name())
other_group = Group.objects.create(name=faker.name())

for g in [group, other_group]:
GroupMembership.objects.create(group=g, user=user)
conversation = Conversation.objects.create(group=g)
ConversationParticipant.objects.create(conversation=conversation, user=user)
message = ConversationMessage.objects.create(conversation=conversation, content='hello', author=user)
ConversationMessage.objects.create(conversation=conversation, content='reply', author=user, thread=message)
ConversationThreadParticipant.objects.create(user=user, thread=message)

self.assertEqual(ConversationParticipant.objects.count(), 2)
self.assertEqual(ConversationThreadParticipant.objects.count(), 2)

GroupMembership.objects.filter(group=group, user=user).delete()

self.user_id = user.id
self.group_id = group.id
self.other_group_id = other_group.id

def test_removes_participants(self):
User = self.apps.get_model('users', 'User')
Group = self.apps.get_model('groups', 'Group')
ConversationParticipant = self.apps.get_model('conversations', 'ConversationParticipant')
ConversationThreadParticipant = self.apps.get_model('conversations', 'ConversationThreadParticipant')

user = User.objects.get(id=self.user_id)
group = Group.objects.get(id=self.group_id)
other_group = Group.objects.get(id=self.other_group_id)

self.assertEqual(ConversationParticipant.objects.count(), 1)
self.assertEqual(ConversationThreadParticipant.objects.count(), 1)

self.assertFalse(ConversationParticipant.objects.filter(conversation__group=group, user=user).exists())
self.assertFalse(
ConversationThreadParticipant.objects.filter(thread__conversation__group=group, user=user).exists()
)

self.assertTrue(ConversationParticipant.objects.filter(conversation__group=other_group, user=user).exists())
self.assertTrue(
ConversationThreadParticipant.objects.filter(thread__conversation__group=other_group, user=user).exists()
)

0 comments on commit 2bf5db4

Please sign in to comment.