Skip to content

Conversation

@wedamija
Copy link
Member

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 wedamija requested review from a team November 20, 2024 19:20
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 20, 2024
@wedamija
Copy link
Member Author

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

wedamija added a commit that referenced this pull request Nov 21, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as `SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure that the pending deletion merges and deploys first, and then the real deletion.
wedamija added a commit to getsentry/sentry-docs that referenced this pull request Nov 21, 2024
wedamija added a commit that referenced this pull request Nov 21, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as `SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure that the pending deletion merges and deploys first, and then the real deletion.
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 wedamija force-pushed the danf/migrations-safe-deletes branch from a3cfac3 to e187edf Compare November 21, 2024 22:49
wedamija added a commit that referenced this pull request Nov 21, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as `SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure that the pending deletion merges and deploys first, and then the real deletion.
@codecov
Copy link

codecov bot commented Nov 21, 2024

Codecov Report

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

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/new_migrations/monkey/models.py 74.19% 8 Missing ⚠️
src/sentry/new_migrations/monkey/state.py 94.11% 2 Missing ⚠️
Additional details and impacted files
@@           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           

Copy link
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@wedamija wedamija merged commit f6c2bc7 into master Nov 22, 2024
49 of 50 checks passed
@wedamija wedamija deleted the danf/migrations-safe-deletes branch November 22, 2024 17:04
wedamija added a commit that referenced this pull request Nov 22, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as `SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure that the pending deletion merges and deploys first, and then the real deletion.
wedamija added a commit to getsentry/sentry-docs that referenced this pull request Nov 22, 2024
wedamija added a commit that referenced this pull request Nov 22, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as
`SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and
also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure
that the pending deletion merges and deploys first, and then the real
deletion.
wedamija added a commit to getsentry/sentry-docs that referenced this pull request Nov 22, 2024
harshithadurai pushed a commit that referenced this pull request Nov 25, 2024
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.

<!-- Describe your PR here. -->
harshithadurai pushed a commit that referenced this pull request Nov 25, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as
`SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and
also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure
that the pending deletion merges and deploys first, and then the real
deletion.
evanh pushed a commit that referenced this pull request Nov 25, 2024
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.

<!-- Describe your PR here. -->
evanh pushed a commit that referenced this pull request Nov 25, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as
`SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and
also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure
that the pending deletion merges and deploys first, and then the real
deletion.
andrewshie-sentry pushed a commit that referenced this pull request Dec 2, 2024
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.

<!-- Describe your PR here. -->
andrewshie-sentry pushed a commit that referenced this pull request Dec 2, 2024
This builds on #81063

This adds in `SafeDeleteColumn`, which works the same way as
`SafeDeleteModel`, except on database columns.

It performs checks that the column doesn't have a db constraint set, and
also that it is either nullable or has a db_default set.

Similarly to `SafeDeleteModel` we still need to be careful to make sure
that the pending deletion merges and deploys first, and then the real
deletion.
@sentry
Copy link

sentry bot commented Dec 3, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ CrossTransactionAssertionError: Transaction opened for db {'secondary'}, but command running against db default pytest.runtest.protocol tests/sentry/db/postgre... View Issue

Did you find this useful? React with a 👍 or 👎

@github-actions github-actions bot locked and limited conversation to collaborators Dec 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants