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(plugin-server): reduce constraints related to person overrides #17936

Closed
wants to merge 1 commit into from
Closed
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
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_adjust_personoverride_constraints
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
3 changes: 3 additions & 0 deletions posthog/management/commands/test_migrations_are_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def _get_table(search_string: str, operation_sql: str) -> Optional[str]:


def validate_migration_sql(sql) -> bool:
if "-- skip-test-migrations-are-safe" in sql:
return False

new_tables = _get_new_tables(sql)
operations = sql.split("\n")
tables_created_so_far: List[str] = []
Expand Down
76 changes: 76 additions & 0 deletions posthog/migrations/0354_adjust_personoverride_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated by Django 3.2.19 on 2023-10-11 21:32

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

# `skip-test-migrations-are-safe` is used because our migraton checks get mad about changing
# constraints and columns, but we're stopping traffic to these tables and truncating them before
# doing the migration, so it's safe.
TRUNCATE_PERSONOVERRIDE_SQL = (
"TRUNCATE posthog_personoverride, posthog_personoverridemapping -- skip-test-migrations-are-safe"
)


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.RemoveConstraint(
model_name="personoverridemapping",
name="unique_uuid",
),
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,
),
# add back constraint that was dropped
migrations.AddConstraint(
model_name="personoverridemapping",
constraint=models.UniqueConstraint(fields=("team_id", "uuid"), name="unique_uuid"),
),
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",
),
]
5 changes: 2 additions & 3 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,7 +138,6 @@ class PersonOverride(models.Model):

class Meta:
constraints = [
models.UniqueConstraint(fields=["team", "old_person_id"], name="unique override per old_person_id"),
Copy link
Contributor Author

@bretthoerner bretthoerner Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old_person_id is a FK to a table (PersonOverrideMapping) that already includes team. By making it a OneToOneField below, it's not possible to have a duplicate old_person_id, period.

models.CheckConstraint(
check=~Q(old_person_id__exact=F("override_person_id")),
name="old_person_id_different_from_override_person_id",
Expand All @@ -148,7 +147,7 @@ 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",
Expand Down