-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create versioning for segments (#4138)
Co-authored-by: Matthew Elwell <matthew.elwell@flagsmith.com>
- Loading branch information
1 parent
5162687
commit bc9b340
Showing
10 changed files
with
721 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class SegmentAuditLogHelper: | ||
def __init__(self) -> None: | ||
self.skip_audit_log = {} | ||
|
||
def should_skip_audit_log(self, segment_id: int) -> None | bool: | ||
return self.skip_audit_log.get(segment_id) | ||
|
||
def set_skip_audit_log(self, segment_id: int) -> None: | ||
self.skip_audit_log[segment_id] = True | ||
|
||
def unset_skip_audit_log(self, segment_id: int) -> None: | ||
if segment_id in self.skip_audit_log: | ||
del self.skip_audit_log[segment_id] | ||
|
||
|
||
segment_audit_log_helper = SegmentAuditLogHelper() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from core.models import SoftDeleteExportableManager | ||
from django.db.models import F | ||
|
||
|
||
class SegmentManager(SoftDeleteExportableManager): | ||
def get_queryset(self): | ||
""" | ||
Returns only the canonical segments, which will always be | ||
the highest version. | ||
""" | ||
return super().get_queryset().filter(id=F("version_of")) |
58 changes: 58 additions & 0 deletions
58
api/segments/migrations/0023_add_versioning_to_segments.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Generated by Django 3.2.25 on 2024-06-10 15:31 | ||
import os | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("segments", "0022_add_soft_delete_to_segment_rules_and_conditions"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="historicalsegment", | ||
name="version", | ||
field=models.IntegerField(null=True), | ||
), | ||
migrations.AddField( | ||
model_name="historicalsegment", | ||
name="version_of", | ||
field=models.ForeignKey( | ||
blank=True, | ||
db_constraint=False, | ||
null=True, | ||
on_delete=django.db.models.deletion.DO_NOTHING, | ||
related_name="+", | ||
to="segments.segment", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="segment", | ||
name="version", | ||
field=models.IntegerField(null=True), | ||
), | ||
migrations.AddField( | ||
model_name="segment", | ||
name="version_of", | ||
field=models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="versioned_segments", | ||
to="segments.segment", | ||
), | ||
), | ||
migrations.RunSQL( | ||
sql=open( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
"sql", | ||
"0023_add_versioning_to_segments.sql", | ||
) | ||
).read(), | ||
reverse_sql=migrations.RunSQL.noop, | ||
), | ||
] |
1 change: 1 addition & 0 deletions
1
api/segments/migrations/sql/0023_add_versioning_to_segments.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UPDATE segments_segment SET version_of_id = id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.