Skip to content

Commit

Permalink
remove constraint on resend_frequency_days
Browse files Browse the repository at this point in the history
raquelmsmith committed Sep 14, 2023
1 parent 6e6d5e1 commit 1fd0bc9
Showing 5 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0015_add_verified_properties
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0351_auto_20230913_1822
posthog: 0351_auto_20230914_2107
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
7 changes: 4 additions & 3 deletions posthog/email.py
Original file line number Diff line number Diff line change
@@ -88,12 +88,13 @@ def _send_email(

with transaction.atomic():
for dest in to:
record, _ = MessagingRecord.objects.get_or_create(
raw_email=dest["raw_email"], campaign_key=campaign_key, resend_frequency_days=resend_frequency_days
)
record, _ = MessagingRecord.objects.get_or_create(raw_email=dest["raw_email"], campaign_key=campaign_key)
# Lock object (database-level) while the message is sent
record = MessagingRecord.objects.select_for_update().get(pk=record.pk)

if resend_frequency_days and resend_frequency_days != record.resend_frequency_days:
record.resend_frequency_days = resend_frequency_days

if _should_send_email(record) is False:
record.save()
continue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.19 on 2023-09-13 18:22
# Generated by Django 3.2.19 on 2023-09-14 21:07

from django.db import migrations, models

@@ -25,8 +25,4 @@ class Migration(migrations.Migration):
name="never_drop_data",
field=models.BooleanField(blank=True, default=False, null=True),
),
migrations.AlterUniqueTogether(
name="messagingrecord",
unique_together={("email_hash", "campaign_key", "resend_frequency_days")},
),
]
2 changes: 1 addition & 1 deletion posthog/models/messaging.py
Original file line number Diff line number Diff line change
@@ -31,4 +31,4 @@ class MessagingRecord(UUIDModel):
resend_dates: models.JSONField = models.JSONField(default=list, null=True, blank=True)

class Meta:
unique_together = ("email_hash", "campaign_key", "resend_frequency_days") # one record per email per campaign
unique_together = ("email_hash", "campaign_key") # one record per email per campaign
15 changes: 6 additions & 9 deletions posthog/test/test_email.py
Original file line number Diff line number Diff line change
@@ -71,10 +71,9 @@ def test_can_send_same_campaign_twice_with_resend_frequency(self) -> None:
with override_instance_config("EMAIL_HOST", "localhost"):
sent_at = timezone.now() - timezone.timedelta(days=8)

record, _ = MessagingRecord.objects.get_or_create(
raw_email="[email protected]", campaign_key="campaign_2", resend_frequency_days=7
)
record, _ = MessagingRecord.objects.get_or_create(raw_email="[email protected]", campaign_key="campaign_2")
record.sent_at = sent_at
record.resend_frequency_days = 7
record.save()

assert record.resend_frequency_days == 7
@@ -103,10 +102,9 @@ def test_can_send_same_campaign_thrice_with_resend_frequency(self) -> None:
with override_instance_config("EMAIL_HOST", "localhost"):
sent_at = timezone.now() - timezone.timedelta(days=20)

record, _ = MessagingRecord.objects.get_or_create(
raw_email="[email protected]", campaign_key="campaign_2", resend_frequency_days=7
)
record, _ = MessagingRecord.objects.get_or_create(raw_email="[email protected]", campaign_key="campaign_2")
record.sent_at = sent_at
record.resend_frequency_days = 7
record.resend_dates = [timezone.now().timestamp() - 10 * 24 * 60 * 60]
record.save()

@@ -130,10 +128,9 @@ def test_cant_send_same_campaign_twice_less_than_resend_frequency(self) -> None:
with override_instance_config("EMAIL_HOST", "localhost"):
sent_at = timezone.now() - timezone.timedelta(days=6)

record, _ = MessagingRecord.objects.get_or_create(
raw_email="[email protected]", campaign_key="campaign_2", resend_frequency_days=7
)
record, _ = MessagingRecord.objects.get_or_create(raw_email="[email protected]", campaign_key="campaign_2")
record.sent_at = sent_at
record.resend_frequency_days = 7
record.save()

assert record.resend_frequency_days == 7

0 comments on commit 1fd0bc9

Please sign in to comment.