-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
chore(Persons): Remove foreign key constraints from Persons and related models #27636
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0545_insight_filters_to_query | ||
0546_remove_featureflaghashkeyoverride_unique_hash_key_for_a_user_team_feature_flag_combo_and_more |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,13 +371,15 @@ class FeatureFlagHashKeyOverride(models.Model): | |
# and we only ever want to get the key. | ||
feature_flag_key = models.CharField(max_length=400) | ||
person = models.ForeignKey("Person", on_delete=models.CASCADE) | ||
team = models.ForeignKey("Team", on_delete=models.CASCADE) | ||
team_id = ( | ||
models.IntegerField() | ||
) # Note: in order to move persons to separate database, this is a int instead of foreingkey | ||
Comment on lines
+374
to
+376
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider adding a db_index=True to team_id since it was previously indexed as a foreign key |
||
hash_key = models.CharField(max_length=400) | ||
|
||
class Meta: | ||
constraints = [ | ||
models.UniqueConstraint( | ||
fields=["team", "person", "feature_flag_key"], | ||
fields=["team_id", "person", "feature_flag_key"], | ||
name="Unique hash_key for a user/team/feature_flag combo", | ||
) | ||
] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,9 +37,9 @@ class Person(models.Model): | |
# used for evaluating if we need to override the value or not (value: set or set_once) | ||
properties_last_operation = models.JSONField(null=True, blank=True) | ||
|
||
team = models.ForeignKey("Team", on_delete=models.CASCADE) | ||
team_id = models.IntegerField() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally this all makes sense. Long term I think we will want to ditch the Django models alltogether and handle migrations from the node service instead (or whatever service "owns" Persons). I guess we lose cascading deleted here although I'm not sure if that ever actually happened? |
||
properties = models.JSONField(default=dict) | ||
is_user = models.ForeignKey("User", on_delete=models.CASCADE, null=True, blank=True) | ||
is_user_id = models.IntegerField(null=True, blank=True) | ||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Removing ForeignKey constraints means application code must ensure team_id and is_user_id values are valid. Consider adding validation in save() method. |
||
is_identified = models.BooleanField(default=False) | ||
uuid = models.UUIDField(db_index=True, default=UUIDT, editable=False) | ||
|
||
|
@@ -118,35 +118,35 @@ def split_person(self, main_distinct_id: Optional[str], max_splits: Optional[int | |
|
||
|
||
class PersonDistinctId(models.Model): | ||
team = models.ForeignKey("Team", on_delete=models.CASCADE, db_index=False) | ||
team_id = models.IntegerField(db_index=False) | ||
person = models.ForeignKey(Person, on_delete=models.CASCADE) | ||
distinct_id = models.CharField(max_length=400) | ||
|
||
# current version of the id, used to sync with ClickHouse and collapse rows correctly for new clickhouse table | ||
version = models.BigIntegerField(null=True, blank=True) | ||
|
||
class Meta: | ||
constraints = [models.UniqueConstraint(fields=["team", "distinct_id"], name="unique distinct_id for team")] | ||
constraints = [models.UniqueConstraint(fields=["team_id", "distinct_id"], name="unique distinct_id for team")] | ||
|
||
|
||
class PersonlessDistinctId(models.Model): | ||
id = models.BigAutoField(primary_key=True) | ||
team = models.ForeignKey("Team", on_delete=models.CASCADE, db_index=False) | ||
team_id = models.IntegerField(db_index=False) | ||
distinct_id = models.CharField(max_length=400) | ||
is_merged = models.BooleanField(default=False) | ||
created_at = models.DateTimeField(auto_now_add=True, blank=True) | ||
|
||
class Meta: | ||
constraints = [ | ||
models.UniqueConstraint(fields=["team", "distinct_id"], name="unique personless distinct_id for team") | ||
models.UniqueConstraint(fields=["team_id", "distinct_id"], name="unique personless distinct_id for team") | ||
] | ||
|
||
|
||
class PersonOverrideMapping(models.Model): | ||
"""A model of persons to be overriden in merge or merge-like events.""" | ||
|
||
id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID") | ||
team_id = models.BigIntegerField() | ||
team_id = models.IntegerField() | ||
uuid = models.UUIDField() | ||
|
||
class Meta: | ||
|
@@ -169,7 +169,7 @@ class PersonOverride(models.Model): | |
""" | ||
|
||
id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID") | ||
team = models.ForeignKey("Team", on_delete=models.CASCADE) | ||
team_id = models.IntegerField() | ||
|
||
old_person_id = models.ForeignKey( | ||
"PersonOverrideMapping", | ||
|
@@ -190,7 +190,7 @@ class PersonOverride(models.Model): | |
class Meta: | ||
constraints = [ | ||
models.UniqueConstraint( | ||
fields=["team", "old_person_id"], | ||
fields=["team_id", "old_person_id"], | ||
name="unique override per old_person_id", | ||
), | ||
models.CheckConstraint( | ||
|
@@ -228,7 +228,7 @@ class PendingPersonOverride(models.Model): | |
""" | ||
|
||
id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID") | ||
team_id = models.BigIntegerField() | ||
team_id = models.IntegerField() | ||
old_person_id = models.UUIDField() | ||
override_person_id = models.UUIDField() | ||
oldest_event = models.DateTimeField() | ||
|
@@ -273,7 +273,7 @@ class FlatPersonOverride(models.Model): | |
""" | ||
|
||
id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID") | ||
team_id = models.BigIntegerField() | ||
team_id = models.IntegerField() | ||
old_person_id = models.UUIDField() | ||
override_person_id = models.UUIDField() | ||
oldest_event = models.DateTimeField() | ||
|
@@ -317,12 +317,12 @@ def get_distinct_ids_for_subquery(person: Person | None, team: Team) -> list[str | |
|
||
if person is not None: | ||
first_ids = ( | ||
PersonDistinctId.objects.filter(person=person, team=team) | ||
PersonDistinctId.objects.filter(person=person, team_id=team.pk) | ||
.order_by("id") | ||
.values_list("distinct_id", flat=True)[:first_ids_limit] | ||
) | ||
last_ids = ( | ||
PersonDistinctId.objects.filter(person=person, team=team) | ||
PersonDistinctId.objects.filter(person=person, team_id=team.pk) | ||
.order_by("-id") | ||
.values_list("distinct_id", flat=True)[:last_ids_limit] | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: removing foreign key constraint allows invalid cohort_ids to be inserted - consider adding application-level validation