Skip to content

Commit

Permalink
Allow run.session to be null
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 26, 2025
1 parent 149972e commit 9b2ce3f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 114 deletions.
2 changes: 1 addition & 1 deletion temba/flows/migrations/0374_backfill_run_session_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db import migrations, transaction


def backfill_run_session_uuid(apps, schema_editor):
def backfill_run_session_uuid(apps, schema_editor): # pragma: no cover
FlowSession = apps.get_model("flows", "FlowSession")
FlowRun = apps.get_model("flows", "FlowRun")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.4 on 2025-02-26 20:32

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("flows", "0378_alter_flowrun_session_alter_flowsession_status"),
]

operations = [
migrations.RemoveConstraint(
model_name="flowrun",
name="flows_run_active_or_waiting_has_session",
),
]
5 changes: 0 additions & 5 deletions temba/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,11 +1207,6 @@ class Meta:
models.Index(name="flowruns_by_session", fields=("session_uuid",), condition=Q(status__in=("A", "W"))),
]
constraints = [
# all active/waiting runs must have a session
models.CheckConstraint(
check=~Q(status__in=("A", "W")) | Q(session__isnull=False),
name="flows_run_active_or_waiting_has_session",
),
# all non-active/waiting runs must have an exited_on
models.CheckConstraint(
check=Q(status__in=("A", "W")) | Q(exited_on__isnull=False),
Expand Down
9 changes: 1 addition & 8 deletions temba/flows/tests/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ class FlowActivityCountTest(TembaTest):
def test_node_counts(self):
flow = self.create_flow("Test 1")
contact = self.create_contact("Bob", phone="+1234567890")
session = FlowSession.objects.create(
uuid=uuid4(),
contact=contact,
status=FlowSession.STATUS_WAITING,
output_url="http://sessions.com/123.json",
created_on=timezone.now(),
)

def create_run(status, node_uuid):
return FlowRun.objects.create(
uuid=uuid4(),
org=self.org,
session=session,
flow=flow,
contact=contact,
status=status,
session_uuid="082cb7a8-a8fc-468d-b0a4-06f5a5179e2b",
created_on=timezone.now(),
modified_on=timezone.now(),
exited_on=timezone.now() if status not in ("A", "W") else None,
Expand Down
84 changes: 0 additions & 84 deletions temba/flows/tests/test_migrations.py

This file was deleted.

13 changes: 3 additions & 10 deletions temba/flows/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,15 @@ def setUp(self):

def test_get_path(self):
flow = self.create_flow("Test")
session = FlowSession.objects.create(
uuid=uuid4(),
contact=self.contact,
status=FlowSession.STATUS_COMPLETED,
output_url="http://sessions.com/123.json",
ended_on=timezone.now(),
)

# create run with old style path JSON
run = FlowRun.objects.create(
uuid=uuid4(),
org=self.org,
session=session,
flow=flow,
contact=self.contact,
status=FlowRun.STATUS_WAITING,
session_uuid="082cb7a8-a8fc-468d-b0a4-06f5a5179e2b",
path=[
{
"uuid": "b5c3421c-3bbb-4dc7-9bda-683456588a6d",
Expand Down Expand Up @@ -68,10 +61,10 @@ def test_get_path(self):
run = FlowRun.objects.create(
uuid=uuid4(),
org=self.org,
session=session,
flow=flow,
contact=self.contact,
status=FlowRun.STATUS_WAITING,
session_uuid="082cb7a8-a8fc-468d-b0a4-06f5a5179e2b",
path_nodes=[UUID("857a1498-3d5f-40f5-8185-2ce596ce2677"), UUID("59d992c6-c491-473d-a7e9-4f431d705c01")],
path_times=[
datetime(2021, 12, 20, 8, 47, 30, 123000, tzinfo=tzone.utc),
Expand Down Expand Up @@ -187,10 +180,10 @@ def test_big_ids(self):
id=4_000_000_000,
uuid=uuid4(),
org=self.org,
session=session,
flow=self.create_flow("Test"),
contact=self.contact,
status=FlowRun.STATUS_WAITING,
session_uuid=session.uuid,
created_on=timezone.now(),
modified_on=timezone.now(),
path=[
Expand Down
21 changes: 18 additions & 3 deletions temba/flows/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,28 @@ def test_trim(self):
status=FlowSession.STATUS_WAITING,
)
run1 = FlowRun.objects.create(
org=self.org, flow=flow, contact=contact, session=session1, status=FlowRun.STATUS_WAITING
org=self.org,
flow=flow,
contact=contact,
session=session1,
session_uuid=session1.uuid,
status=FlowRun.STATUS_WAITING,
)
run2 = FlowRun.objects.create(
org=self.org, flow=flow, contact=contact, session=session2, status=FlowRun.STATUS_WAITING
org=self.org,
flow=flow,
contact=contact,
session=session2,
session_uuid=session2.uuid,
status=FlowRun.STATUS_WAITING,
)
run3 = FlowRun.objects.create(
org=self.org, flow=flow, contact=contact, session=session3, status=FlowRun.STATUS_WAITING
org=self.org,
flow=flow,
contact=contact,
session=session3,
session_uuid=session3.uuid,
status=FlowRun.STATUS_WAITING,
)

# create an IVR call with session
Expand Down
2 changes: 1 addition & 1 deletion temba/orgs/tests/test_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def _create_flow_content(self, org, user, channels, contacts, groups, add) -> tu
org=org,
flow=flow1,
contact=contacts[0],
session=session1,
session_uuid=session1.uuid,
status=FlowRun.STATUS_COMPLETED,
exited_on=timezone.now(),
)
Expand Down
1 change: 0 additions & 1 deletion temba/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ def create_incoming_call(
flow=flow,
contact=contact,
status=FlowRun.STATUS_COMPLETED,
session=session,
session_uuid=session.uuid,
exited_on=timezone.now(),
)
Expand Down
2 changes: 1 addition & 1 deletion temba/tests/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def save(self):
start=self.start if i == 0 else None,
flow=Flow.objects.get(uuid=run["flow"]["uuid"]),
contact=self.contact,
session=self.session,
session_uuid=self.session.uuid,
created_on=run["created_on"],
**db_state,
)
Expand Down

0 comments on commit 9b2ce3f

Please sign in to comment.