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

ref(crons): Add null constraint to monitorcheckin monitor_environment #83832

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

evanpurkhiser
Copy link
Member

All old check-ins that didn't have this set have fallen out of
retention. This was added as compat while we added environments

select count(1) from sentry_monitorcheckin where monitor_environment_id is null;

All return 0 across US / DE

@evanpurkhiser evanpurkhiser requested review from a team as code owners January 22, 2025 16:47
@evanpurkhiser evanpurkhiser requested review from asottile-sentry and a team and removed request for a team January 22, 2025 16:47
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 22, 2025
@evanpurkhiser evanpurkhiser enabled auto-merge (squash) January 22, 2025 16:50
Copy link
Contributor

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

--
-- Alter field monitor_environment on monitorcheckin
--
SET CONSTRAINTS "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo" IMMEDIATE; ALTER TABLE "sentry_monitorcheckin" DROP CONSTRAINT "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo";
ALTER TABLE "sentry_monitorcheckin" ADD CONSTRAINT "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo" FOREIGN KEY ("monitor_environment_id") REFERENCES "sentry_monitorenvironment" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "sentry_monitorcheckin" VALIDATE CONSTRAINT "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo";
ALTER TABLE "sentry_monitorcheckin" ADD CONSTRAINT "sentry_monitorcheckin_monitor_environment_id_1e84da50_notnull" CHECK ("monitor_environment_id" IS NOT NULL) NOT VALID;
ALTER TABLE "sentry_monitorcheckin" VALIDATE CONSTRAINT "sentry_monitorcheckin_monitor_environment_id_1e84da50_notnull";
ALTER TABLE "sentry_monitorcheckin" ALTER COLUMN "monitor_environment_id" SET NOT NULL;
ALTER TABLE "sentry_monitorcheckin" DROP CONSTRAINT "sentry_monitorcheckin_monitor_environment_id_1e84da50_notnull";

Copy link
Member

@asottile-sentry asottile-sentry left a comment

Choose a reason for hiding this comment

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

surprised the migrations codeowners didn't kick in 🤔

@evanpurkhiser
Copy link
Member Author

Going to get some @getsentry/owners-migrations eyes on this, though I believe this is generating the correct DDL according to

https://develop.sentry.dev/backend/application-domains/database-migrations/#adding-constraints-to-columns-including-not-null

We want the DEFERRED and VALIDATE after

Copy link

codecov bot commented Jan 22, 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   #83832   +/-   ##
=======================================
  Coverage   87.63%   87.63%           
=======================================
  Files        9510     9510           
  Lines      542008   541992   -16     
  Branches    21222    21222           
=======================================
- Hits       474968   474959    -9     
+ Misses      66674    66667    -7     
  Partials      366      366           

# is a schema change, it's completely safe to run the operation after the code has deployed.
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment

is_post_deployment = False
Copy link
Member

@vgrozdanic vgrozdanic Jan 22, 2025

Choose a reason for hiding this comment

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

This table has over 600M rows just in US , so the validations of constraints very likely won't finish in 5 seconds (our time limit for in-deployment migrations).

We should mark this as post-deployment migration and run it manually

@markstory
Copy link
Member

Interesting that Django is dropping and rebuilding the foreign key constraint. I agree with @vgrozdanic that this should be done as a post_deploy migration to avoid query timeouts.

@wedamija
Copy link
Member

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

--
-- Alter field monitor_environment on monitorcheckin
--
SET CONSTRAINTS "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo" IMMEDIATE; ALTER TABLE "sentry_monitorcheckin" DROP CONSTRAINT "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo";
ALTER TABLE "sentry_monitorcheckin" ADD CONSTRAINT "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo" FOREIGN KEY ("monitor_environment_id") REFERENCES "sentry_monitorenvironment" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "sentry_monitorcheckin" VALIDATE CONSTRAINT "sentry_monitorchecki_monitor_environment__1e84da50_fk_sentry_mo";
ALTER TABLE "sentry_monitorcheckin" ADD CONSTRAINT "sentry_monitorcheckin_monitor_environment_id_1e84da50_notnull" CHECK ("monitor_environment_id" IS NOT NULL) NOT VALID;
ALTER TABLE "sentry_monitorcheckin" VALIDATE CONSTRAINT "sentry_monitorcheckin_monitor_environment_id_1e84da50_notnull";
ALTER TABLE "sentry_monitorcheckin" ALTER COLUMN "monitor_environment_id" SET NOT NULL;
ALTER TABLE "sentry_monitorcheckin" DROP CONSTRAINT "sentry_monitorcheckin_monitor_environment_id_1e84da50_notnull";

Don't know why it feels the need to drop/recreate the fk constraint here... not a huge problem, it'll just slow it down

@vgrozdanic
Copy link
Member

I was also interested in drop/add of a non-related constraint and started searching through Django tickets to see if there is already a bug reported because it feels like a bug.

There were a few reports and a fix for a similar behavior, but I would have to check in demo project first if the newest version of Django has this fixed and if maybe this problems comes because of our mocks in migrations (although unlikely) 🤷

@evanpurkhiser evanpurkhiser force-pushed the evanpurkhiser/ref-crons-add-null-constraint-to-monitorcheckin-monitor-environment branch from d09b73a to 61e3212 Compare January 22, 2025 18:10
@evanpurkhiser
Copy link
Member Author

evanpurkhiser commented Jan 22, 2025

Fixed tests and changed over to a post deploy

Comment on lines -31 to -32
if monitor_env is None:
return None
Copy link
Member

Choose a reason for hiding this comment

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

This code will be active before the migration is run. Will that behave correctly?

Copy link
Member

Choose a reason for hiding this comment

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

it's never None so it's currently dead code

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, what Anthony said.

All old check-ins that didn't have this set have fallen out of
retention. This was added as compat while we added environments

```sql
select count(1) from sentry_monitorcheckin where monitor_environment_id is null;
```

All return 0 across US / DE
@evanpurkhiser evanpurkhiser force-pushed the evanpurkhiser/ref-crons-add-null-constraint-to-monitorcheckin-monitor-environment branch from 7410589 to 70a4f4f Compare January 22, 2025 20:35
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.

5 participants