From eb105a90d9504b77544584a9bfc421f9db0a0b13 Mon Sep 17 00:00:00 2001 From: Brett Hoerner Date: Wed, 11 Oct 2023 15:36:13 -0600 Subject: [PATCH] feat(plugin-server): reduce constraints related to person overrides --- latest_migrations.manifest | 2 +- .../0354_adjust_personoverride_constraints.py | 66 +++++++++++++++++++ posthog/models/person/person.py | 6 +- 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 posthog/migrations/0354_adjust_personoverride_constraints.py diff --git a/latest_migrations.manifest b/latest_migrations.manifest index adaab5c454cc13..58ff194fab23d8 100644 --- a/latest_migrations.manifest +++ b/latest_migrations.manifest @@ -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 diff --git a/posthog/migrations/0354_adjust_personoverride_constraints.py b/posthog/migrations/0354_adjust_personoverride_constraints.py new file mode 100644 index 00000000000000..ec2487b2c007ba --- /dev/null +++ b/posthog/migrations/0354_adjust_personoverride_constraints.py @@ -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", + ), + ] diff --git a/posthog/models/person/person.py b/posthog/models/person/person.py index b2b3bb3e367256..d4bb279eb5a9bd 100644 --- a/posthog/models/person/person.py +++ b/posthog/models/person/person.py @@ -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() @@ -138,7 +138,6 @@ 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", @@ -146,9 +145,8 @@ class Meta: ] 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",