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

fix(aci): Add migration to check group id for exclusion constraint #88484

Merged
merged 13 commits into from
Apr 2, 2025

Conversation

snigdhas
Copy link
Member

@snigdhas snigdhas commented Apr 1, 2025

The existing exclusion constraint looks only for overlapping ranges of (date_started, date_ended). We need to enforce this constraint for each group, so that no distinct group has two open periods that overlap. The table itself is expected to have multiple overlapping ranges since we'll have one row per group.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 1, 2025
Copy link

codecov bot commented Apr 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #88484       +/-   ##
===========================================
+ Coverage   32.99%   87.76%   +54.77%     
===========================================
  Files        8480    10027     +1547     
  Lines      474576   567682    +93106     
  Branches    22311    22246       -65     
===========================================
+ Hits       156568   498213   +341645     
+ Misses     317588    69050   -248538     
+ Partials      420      419        -1     

Copy link
Member

@wedamija wedamija left a comment

Choose a reason for hiding this comment

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

Thanks, this seems right!

@snigdhas snigdhas marked this pull request as ready for review April 2, 2025 04:51
@snigdhas snigdhas requested a review from a team as a code owner April 2, 2025 04:51
@snigdhas snigdhas force-pushed the snigdha/update-constraint branch from b56a942 to dead23f Compare April 2, 2025 04:54
@snigdhas snigdhas force-pushed the snigdha/update-constraint branch from dead23f to e1885d1 Compare April 2, 2025 04:55
Copy link
Contributor

github-actions bot commented Apr 2, 2025

This PR has a migration; here is the generated SQL for src/sentry/migrations/0855_update_group_open_periods_constraint.py ()

--
-- Remove constraint exclude_open_period_overlap from model groupopenperiod
--
ALTER TABLE "sentry_groupopenperiod" DROP CONSTRAINT "exclude_open_period_overlap";
--
-- Create constraint exclude_open_period_overlap on model groupopenperiod
--
ALTER TABLE "sentry_groupopenperiod" ADD CONSTRAINT "exclude_open_period_overlap" EXCLUDE USING GIST ("group_id" WITH =, (TSTZRANGE("date_started", "date_ended", '[)')) WITH &&);

Copy link
Contributor

github-actions bot commented Apr 2, 2025

This PR has a migration; here is the generated SQL for src/sentry/migrations/0856_update_group_open_periods_constraint.py ()

--
-- Remove constraint exclude_open_period_overlap from model groupopenperiod
--
ALTER TABLE "sentry_groupopenperiod" DROP CONSTRAINT "exclude_open_period_overlap";
--
-- Custom state/database change combination
--
ALTER TABLE "sentry_groupopenperiod"
                ADD CONSTRAINT "exclude_open_period_overlap" EXCLUDE USING GIST (
                    "group_id" gist_int8_ops WITH =,
                    (TSTZRANGE("date_started", "date_ended", '[)')) WITH &&
                );

Comment on lines 36 to 52
migrations.AddConstraint(
model_name="groupopenperiod",
constraint=django.contrib.postgres.constraints.ExclusionConstraint(
expressions=[
(models.F("group"), "="),
(
sentry.models.groupopenperiod.TsTzRange(
"date_started",
"date_ended",
django.contrib.postgres.fields.ranges.RangeBoundary(),
),
"&&",
),
],
name="exclude_open_period_overlap",
),
),
Copy link
Member

Choose a reason for hiding this comment

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

So as far as I can tell, we need to pass opsclasses here to use the bigint col in this constraint. There's no way to do this in Django for now.

So let's use SeparateDatabaseAndState. Put this AddConstraint in the state, and use add this to the database

SafeRunSQL("""ALTER TABLE "sentry_groupopenperiod" 
                ADD CONSTRAINT "exclude_open_period_overlap" EXCLUDE USING GIST (
                    "group_id" gist_int8_ops WITH =,
                    (TSTZRANGE("date_started", "date_ended", '[)')) WITH &&
                );""", use_statement_timeout=False)

Copy link
Member

@wedamija wedamija left a comment

Choose a reason for hiding this comment

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

Ahh, damn... I think I know why it's failing on most tests.

When we generate the database without migrations, it looks at the model definition and produces the sql... so it's still creating the GIST in the wrong way.

Basically, there's not a great way to make this index and have it on the table definition.

What we could do is include the ExclusionConstraint as a comment where it is now, and explain why we can't actually define it in the code. Then just create it using SafeRunSQL, and not have any state representing this exclusion constraint.

You can also set remove the checked = False line since we won't be explicitly checking anymore

Copy link
Contributor

github-actions bot commented Apr 2, 2025

This PR has a migration; here is the generated SQL for src/sentry/migrations/0857_update_group_open_periods_constraint.py ()

--
-- Remove constraint exclude_open_period_overlap from model groupopenperiod
--
ALTER TABLE "sentry_groupopenperiod" DROP CONSTRAINT "exclude_open_period_overlap";
--
-- Raw SQL operation
--
ALTER TABLE "sentry_groupopenperiod"
                ADD CONSTRAINT "exclude_open_period_overlap" EXCLUDE USING GIST (
                    "group_id" gist_int8_ops WITH =,
                    (TSTZRANGE("date_started", "date_ended", '[)')) WITH &&
                );

Comment on lines 49 to 65
migrations.AddConstraint(
model_name="groupopenperiod",
constraint=django.contrib.postgres.constraints.ExclusionConstraint(
expressions=[
(models.F("group"), "="),
(
sentry.models.groupopenperiod.TsTzRange(
"date_started",
"date_ended",
django.contrib.postgres.fields.ranges.RangeBoundary(),
),
"&&",
),
],
name="exclude_open_period_overlap",
),
),
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the state operation, django won't be aware of this because it doesn't exist on the model. You can remove the SeparateDatabaseAndState in general and put the SafeRunSQL as its own operation

@snigdhas snigdhas merged commit 8e85f00 into master Apr 2, 2025
48 checks passed
@snigdhas snigdhas deleted the snigdha/update-constraint branch April 2, 2025 22:43
adrian-codecov pushed a commit that referenced this pull request Apr 3, 2025
…88484)

The existing exclusion constraint looks only for overlapping ranges of
(date_started, date_ended). We need to enforce this constraint for each
group, so that no distinct group has two open periods that overlap. The
table itself is expected to have multiple overlapping ranges since we'll
have one row per group.
snigdhas added a commit that referenced this pull request Apr 3, 2025
The previous attempt (#88484) to
do this failed because of the order of operations, so we've split this
up into two steps:

1. remove the existing constraint
#88708
2. add a new updated constraint (this pr)

Note that the table is empty. For context, the old exclusion constraint
looks only for overlapping ranges of (date_started, date_ended). We need
to enforce this constraint for each group, so that no distinct group has
two open periods that overlap. The table itself is expected to have
multiple overlapping ranges since we'll have one row per group.
andrewshie-sentry pushed a commit that referenced this pull request Apr 8, 2025
…88484)

The existing exclusion constraint looks only for overlapping ranges of
(date_started, date_ended). We need to enforce this constraint for each
group, so that no distinct group has two open periods that overlap. The
table itself is expected to have multiple overlapping ranges since we'll
have one row per group.
andrewshie-sentry pushed a commit that referenced this pull request Apr 8, 2025
The previous attempt (#88484) to
do this failed because of the order of operations, so we've split this
up into two steps:

1. remove the existing constraint
#88708
2. add a new updated constraint (this pr)

Note that the table is empty. For context, the old exclusion constraint
looks only for overlapping ranges of (date_started, date_ended). We need
to enforce this constraint for each group, so that no distinct group has
two open periods that overlap. The table itself is expected to have
multiple overlapping ranges since we'll have one row per group.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants