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

Update docs on how to delete tables and columns #11897

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

Conversation

wedamija
Copy link
Member

This uses the new operations from
getsentry/sentry#81063
getsentry/sentry#81098

@wedamija wedamija requested review from a team November 21, 2024 01:58
Copy link

vercel bot commented Nov 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
develop-docs 🛑 Canceled (Inspect) Nov 21, 2024 10:31pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
changelog ⬜️ Ignored (Inspect) Visit Preview Nov 21, 2024 10:31pm
sentry-docs ⬜️ Ignored (Inspect) Visit Preview Nov 21, 2024 10:31pm

- If the column is a foreign key, remove the database level foreign key constraint it by setting `db_constraint=False`.
- Remove the column and in the generated migration use `SafeRemoveField(..., deletion_action=DeletionAction.MOVE_TO_PENDING)` to replace `RemoveField(...)`. This only marks the state for the column as removed.
- Combine these migrations together to save making multiple deploys
- Deploy. It's important that all previous prs are in production before we remove the actual column from the table.
Copy link
Member

Choose a reason for hiding this comment

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

Should we call this out even more explicitly, maybe a <Note> type thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if it's necessary, people should just be following these steps in order, and we've had the docs like this for a while

- Remove the column from the model, but in the migration make sure we only mark the state as removed.
- Deploy.
- Finally, create a migration that deletes the column.
- (Optional, but ideal) Make a PR to remove all uses of the column in the codebase in a separate PR. This mostly helps with code cleanliness. This should be merged ahead of the migration prs, but we don't need to worry about whether it is deployed first.
Copy link
Member

@evanpurkhiser evanpurkhiser Nov 22, 2024

Choose a reason for hiding this comment

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

I'm slightly confused about the optionality of this, is it just optional that it's a separate PR. It kind of makes it sound like we don't need to remove all the uses of the column, we definitely do.

I actually wouldn't mind if this just was the prescribed flow and remove the note about optionality.

Suggested change
- (Optional, but ideal) Make a PR to remove all uses of the column in the codebase in a separate PR. This mostly helps with code cleanliness. This should be merged ahead of the migration prs, but we don't need to worry about whether it is deployed first.
- Make a PR to remove all uses of the column in the codebase in a separate PR. This mostly helps with code cleanliness. This should be merged ahead of the migration prs, but we don't need to worry about whether it is deployed first.

- Finally, create a migration that deletes the column.
- (Optional, but ideal) Make a PR to remove all uses of the column in the codebase in a separate PR. This mostly helps with code cleanliness. This should be merged ahead of the migration prs, but we don't need to worry about whether it is deployed first.
- Make another PR that:
- Checks if the column is either not nullable, or doesn't have a db_default set. If either of these is true, then make it nullable via `null=True`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Checks if the column is either not nullable, or doesn't have a db_default set. If either of these is true, then make it nullable via `null=True`.
- Checks if the column is either not nullable, or doesn't have a `db_default` set. If either of these is true, then make it nullable via `null=True`.


```python
operations = [
SafeRemoveField(model_name="testmodel", name="project", deletion_action=DeletionAction.MOVE_TO_PENDING),
Copy link
Member

Choose a reason for hiding this comment

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

Do you have to import SafeRemoveField? Should we monkey-patch it onto the migrations module?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, monkeypatching it in feels a bit weird... Usually I just quick import things using my IDE, so I'm not sure it's good to do even more monkeying here?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah sounds good. Let’s at least add the import to the code example though

]
```

Using `SafeRemoveField` allows us to remove all the state related to the column, but defer deleting it until a later migration. So now, we can combine the migration to remove the constraints and delete the column state together like so
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 a little confusing here that the next codeblock we're saying "That second migration you just made, throw that one away, we can combine them into the first"

We do need to describe both, since in the case where there's no alter needed for removing null or FKs, we just want the safe remove, but I feel like we should tweak this a bit to make it more clear that that's whats happening here

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe it'd help to just post the two migrations necessary first, then write the steps to get to that state after?

Copy link
Member

Choose a reason for hiding this comment

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

I think that would make sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants