Skip to content

Commit

Permalink
feat(plugin-server): reduce constraints related to person overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
bretthoerner committed Oct 11, 2023
1 parent 1f9b42c commit eb105a9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -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: 0353_add_5_minute_interval_to_batch_exports
posthog: 0354_auto_20231011_2132
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
66 changes: 66 additions & 0 deletions posthog/migrations/0354_adjust_personoverride_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by Django 3.2.19 on 2023-10-11 21:32

from django.db import migrations, models
import django.db.models.deletion

TRUNCATE_PERSONOVERRIDE_SQL = "TRUNCATE posthog_personoverride, posthog_personoverridemapping"


class Migration(migrations.Migration):
dependencies = [
("posthog", "0353_add_5_minute_interval_to_batch_exports"),
]

operations = [
migrations.RunSQL(TRUNCATE_PERSONOVERRIDE_SQL, "SELECT 1"),
migrations.RemoveConstraint(
model_name="personoverride",
name="unique override per old_person_id",
),
migrations.RemoveField(
model_name="personoverride",
name="team",
),
migrations.RemoveField(
model_name="personoverridemapping",
name="team_id",
),
migrations.AddField(
model_name="personoverridemapping",
name="team",
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to="posthog.team"),
preserve_default=False,
),
migrations.AlterField(
model_name="personoverride",
name="old_person_id",
field=models.OneToOneField(
db_column="old_person_id",
on_delete=django.db.models.deletion.CASCADE,
related_name="person_override_old",
to="posthog.personoverridemapping",
),
),
# drop the old exclusion constraint
migrations.RunSQL(
"ALTER TABLE posthog_personoverride DROP CONSTRAINT exclude_override_person_id_from_being_old_person_id",
"""
ALTER TABLE posthog_personoverride
ADD CONSTRAINT exclude_override_person_id_from_being_old_person_id
EXCLUDE USING gist((array[old_person_id, override_person_id]) WITH &&, override_person_id WITH <>)
DEFERRABLE
INITIALLY DEFERRED
""",
),
# add the new exclusion constraint
migrations.RunSQL(
"""
ALTER TABLE posthog_personoverride
ADD CONSTRAINT exclude_override_person_id_from_being_old_person_id
EXCLUDE USING gist (old_person_id WITH =, override_person_id WITH =)
DEFERRABLE
INITIALLY DEFERRED
""",
"ALTER TABLE posthog_personoverride DROP CONSTRAINT exclude_override_person_id_from_being_old_person_id",
),
]
6 changes: 2 additions & 4 deletions posthog/models/person/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Meta:
]

id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")
team_id = models.BigIntegerField()
team: models.ForeignKey = models.ForeignKey("Team", on_delete=models.CASCADE)
uuid = models.UUIDField()


Expand All @@ -138,17 +138,15 @@ class PersonOverride(models.Model):

class Meta:
constraints = [
models.UniqueConstraint(fields=["team", "old_person_id"], name="unique override per old_person_id"),
models.CheckConstraint(
check=~Q(old_person_id__exact=F("override_person_id")),
name="old_person_id_different_from_override_person_id",
),
]

id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")
team: models.ForeignKey = models.ForeignKey("Team", on_delete=models.CASCADE)

old_person_id: models.ForeignKey = models.ForeignKey(
old_person_id: models.OneToOneField = models.OneToOneField(
"PersonOverrideMapping",
db_column="old_person_id",
related_name="person_override_old",
Expand Down

0 comments on commit eb105a9

Please sign in to comment.