Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add timestamps to segments models #4236

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading