Skip to content

Commit

Permalink
feat: Add timestamps to segments models (#4236)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Jun 28, 2024
1 parent bc9b340 commit a5b2421
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
84 changes: 84 additions & 0 deletions api/segments/migrations/0024_add_timestamps_to_segments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Generated by Django 3.2.25 on 2024-06-25 19:46

from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def set_null_timestamps(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
Segment = apps.get_model("segments", "Segment")
Segment.objects.update(created_at=None, updated_at=None)
HistoricalSegment = apps.get_model("segments", "HistoricalSegment")
HistoricalSegment.objects.update(created_at=None, updated_at=None)
Condition = apps.get_model("segments", "Condition")
Condition.objects.update(created_at=None, updated_at=None)
HistoricalCondition = apps.get_model("segments", "HistoricalCondition")
HistoricalCondition.objects.update(created_at=None, updated_at=None)

# Note that segment rules don't presently have a matching
# HistoricalSegmentRule as the history is not set up.
SegmentRule = apps.get_model("segments", "SegmentRule")
SegmentRule.objects.update(created_at=None, updated_at=None)


class Migration(migrations.Migration):

dependencies = [
("segments", "0023_add_versioning_to_segments"),
]

operations = [
migrations.AddField(
model_name="condition",
name="created_at",
field=models.DateTimeField(auto_now_add=True, null=True),
),
migrations.AddField(
model_name="condition",
name="updated_at",
field=models.DateTimeField(auto_now=True, null=True),
),
migrations.AddField(
model_name="historicalcondition",
name="created_at",
field=models.DateTimeField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name="historicalcondition",
name="updated_at",
field=models.DateTimeField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name="historicalsegment",
name="created_at",
field=models.DateTimeField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name="historicalsegment",
name="updated_at",
field=models.DateTimeField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name="segment",
name="created_at",
field=models.DateTimeField(auto_now_add=True, null=True),
),
migrations.AddField(
model_name="segment",
name="updated_at",
field=models.DateTimeField(auto_now=True, null=True),
),
migrations.AddField(
model_name="segmentrule",
name="created_at",
field=models.DateTimeField(auto_now_add=True, null=True),
),
migrations.AddField(
model_name="segmentrule",
name="updated_at",
field=models.DateTimeField(auto_now=True, null=True),
),
migrations.RunPython(
set_null_timestamps, reverse_code=migrations.RunPython.noop
),
]
9 changes: 9 additions & 0 deletions api/segments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class Segment(
)
metadata = GenericRelation(Metadata)

created_at = models.DateTimeField(null=True, auto_now_add=True)
updated_at = models.DateTimeField(null=True, auto_now=True)

# Only serves segments that are the canonical version.
objects = SegmentManager()

Expand Down Expand Up @@ -198,6 +201,9 @@ class SegmentRule(SoftDeleteExportableModel):

type = models.CharField(max_length=50, choices=RULE_TYPES)

created_at = models.DateTimeField(null=True, auto_now_add=True)
updated_at = models.DateTimeField(null=True, auto_now=True)

def clean(self):
super().clean()
parents = [self.segment, self.rule]
Expand Down Expand Up @@ -305,6 +311,9 @@ class Condition(
SegmentRule, on_delete=models.CASCADE, related_name="conditions"
)

created_at = models.DateTimeField(null=True, auto_now_add=True)
updated_at = models.DateTimeField(null=True, auto_now=True)

def __str__(self):
return "Condition for %s: %s %s %s" % (
str(self.rule),
Expand Down

0 comments on commit a5b2421

Please sign in to comment.