From df7b744d28b852c7d2965ad99d398fe9de97c6b4 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Wed, 26 Feb 2025 19:51:57 +0000 Subject: [PATCH] Fix contacts whose current_session_uuid doesn't match a waiting session --- .../0205_create_session_expires_fires.py | 15 +++++++++++---- temba/contacts/tests/test_migrations.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/temba/contacts/migrations/0205_create_session_expires_fires.py b/temba/contacts/migrations/0205_create_session_expires_fires.py index ab6c437d2e..d248a5c575 100644 --- a/temba/contacts/migrations/0205_create_session_expires_fires.py +++ b/temba/contacts/migrations/0205_create_session_expires_fires.py @@ -12,7 +12,7 @@ def create_session_expires_fires(apps, schema_editor): ContactFire = apps.get_model("contacts", "ContactFire") FlowSession = apps.get_model("flows", "FlowSession") - num_created = 0 + num_created, num_skipped = 0, 0 while True: # find contacts with waiting sessions that don't have a corresponding session expiration fire @@ -24,7 +24,7 @@ def create_session_expires_fires(apps, schema_editor): if not batch: break - sessions = FlowSession.objects.filter(uuid__in=[c.current_session_uuid for c in batch]).only( + sessions = FlowSession.objects.filter(uuid__in=[c.current_session_uuid for c in batch], status="W").only( "uuid", "created_on" ) created_on_by_uuid = {s.uuid: s.created_on for s in sessions} @@ -43,10 +43,17 @@ def create_session_expires_fires(apps, schema_editor): session_uuid=contact.current_session_uuid, ) ) + else: + contact.current_session_uuid = None + contact.current_flow = None + contact.save(update_fields=("current_session_uuid", "current_flow")) - ContactFire.objects.bulk_create(to_create) + num_skipped += 1 + + if to_create: + ContactFire.objects.bulk_create(to_create) num_created += len(to_create) - print(f"Created {num_created} session expiration fires") + print(f"Created {num_created} session expiration fires ({num_skipped} skipped)") def apply_manual(): # pragma: no cover diff --git a/temba/contacts/tests/test_migrations.py b/temba/contacts/tests/test_migrations.py index d151debdd0..6b0a1851c9 100644 --- a/temba/contacts/tests/test_migrations.py +++ b/temba/contacts/tests/test_migrations.py @@ -51,6 +51,18 @@ def create_contact_and_sessions(name, phone, current_session_uuid): # contact with no waiting session self.contact4 = self.create_contact("Dan", phone="+1234567004") + # contact with session mismatch + self.contact5 = self.create_contact( + "Dan", phone="+1234567004", current_session_uuid="ffca65c7-42ac-40cd-bef0-63aedc099ec9" + ) + FlowSession.objects.create( + uuid="80466ed4-de5c-49e8-acad-2432b4e9cdf9", + contact=self.contact5, + status=FlowSession.STATUS_WAITING, + output_url="http://sessions.com/123.json", + created_on=timezone.now(), + ) + def test_migration(self): def assert_session_expire(contact): self.assertTrue(contact.fires.exists()) @@ -70,3 +82,7 @@ def assert_session_expire(contact): assert_session_expire(self.contact3) self.assertFalse(self.contact4.fires.exists()) + self.assertFalse(self.contact5.fires.exists()) + + self.contact5.refresh_from_db() + self.assertIsNone(self.contact5.current_session_uuid)