getsentry / sentry

Developer-first error tracking and performance monitoring
https://sentry.io
Other
39.23k stars 4.21k forks source link

feat(migrations): Add in `SafeDeleteModel` migration operation #81063

Open wedamija opened 1 day ago

wedamija commented 1 day ago

This adds in the SafeDeleteModel operation. Most of this pr is testing and monkeypatching Django migrations to be able to hook this in.

SafeDeleteModel allows us to delete models in two steps without using custom sql. If accepts a deletion_action parameter, which must be passed, and can be either MOVE_TO_PENDING or DELETE.

MOVE_TO_PENDING checks that there are no constraints on the table and removes the table from the migation state. We keep track of pending models separately, so that they can be deleted in the next step.

DELETE runs the delete command on the database. It is able to still reference the model since we keep track of this in our patched state. If delete is run before MOVE_TO_PENDING then the migration will be rejected.

Some care is still needed when using these - we still have to ensure that the PENDING migration runs and fully deploys before the DELETE does. But other than that this reduces the manual error prone sql work around this process a lot.

wedamija commented 1 day ago

Note: Most of the volume of this pr is migrations for tests - Mark them viewed so you can focus on the core part

codecov[bot] commented 12 hours ago

Codecov Report

Attention: Patch coverage is 86.48649% with 10 lines in your changes missing coverage. Please review.

:white_check_mark: All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/new_migrations/monkey/models.py 74.19% 8 Missing :warning:
src/sentry/new_migrations/monkey/state.py 94.11% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #81063 +/- ## ======================================= Coverage 80.33% 80.34% ======================================= Files 7223 7225 +2 Lines 320085 320155 +70 Branches 20778 20778 ======================================= + Hits 257156 257218 +62 - Misses 62536 62544 +8 Partials 393 393 ```