You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -145,144 +145,202 @@ This is complicated due to our deploy process. When we deploy, we run migrations
145
145
146
146
To avoid this, follow these steps:
147
147
148
-
- Mark the column as nullable if it isn't, and create a migration. (ex. `BoundedIntegerField(null=True)`)
149
-
- Deploy.
150
-
- Remove the column from the model, but in the migration make sure we only mark the state as removed.
151
-
- Deploy.
152
-
- Finally, create a migration that deletes the column.
148
+
- (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.
149
+
- Make another PR that:
150
+
- 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`.
151
+
- If the column is a foreign key, remove the database level foreign key constraint it by setting `db_constraint=False`.
152
+
- 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.
153
+
- Combine these migrations together to save making multiple deploys
154
+
- Deploy. It's important that all previous prs are in production before we remove the actual column from the table.
155
+
- Make a PR that create a new migration that has the same `SafeRemoveField` operation as before, but set `deletion_action=DeletionAction.DELETE` instead. This deletes the actual column from the table in Postgres.
156
+
- Deploy
157
+
158
+
Here's an example of removing the `project` column from this model. It is both a foreign key and not null:
159
+
160
+
```python
161
+
@region_silo_model
162
+
classTestModel(Model):
163
+
__relocation_scope__ = RelocationScope.Excluded
164
+
165
+
project = FlexibleForeignKey("sentry.Project")
166
+
name = models.TextField()
167
+
168
+
classMeta:
169
+
app_label ="uptime"
170
+
db_table ="uptime_testmodel"
171
+
```
172
+
173
+
First we remove the constraint and make the column nullable:
Here's an example of removing columns that were already nullable. First we remove the columns from the model, and then modify the migration to only update the state and make no database operations.
204
+
Deleting this way is unsafe, so we want to replace this with `SafeDeleteModel` instead. So this becomes
Once this is deployed, we can then deploy the actual column deletion. This pr will have only a migration, since Django no longer knows about these fields. Note that the reverse SQL is only for dev, so it's fine to not assign a default or do any sort of backfill:
212
+
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
170
213
171
214
```python
172
215
operations = [
173
-
migrations.SeparateDatabaseAndState(
174
-
database_operations=[
175
-
migrations.RunSQL(
176
-
"""
177
-
ALTER TABLE "sentry_alertrule" DROP COLUMN "alert_threshold";
178
-
ALTER TABLE "sentry_alertrule" DROP COLUMN "resolve_threshold";
179
-
ALTER TABLE "sentry_alertrule" DROP COLUMN "threshold_type";
180
-
""",
181
-
reverse_sql="""
182
-
ALTER TABLE "sentry_alertrule" ADD COLUMN "alert_threshold" smallint NULL;
183
-
ALTER TABLE "sentry_alertrule" ADD COLUMN "resolve_threshold" int NULL;
184
-
ALTER TABLE "sentry_alertrule" ADD COLUMN "threshold_type" int NULL;
So now we deploy this migration and move onto the final step.
231
+
232
+
In this last step, we will use `SafeRemoveField` again to finally remove the column from the table in Postgres. First, we generate an empty migration using `sentry django makemigrations <your_app> --empty` to produce an empty migration, and then modify the operations to be like:
Then we deploy this and we're done. So to recap the steps here:
241
+
- Remove all references to the column in the code in a separate PR and merge. Doesn't matter if this deploys before the next step or not.
242
+
- If the column has an fk constraint them remove it. If it's not null and has no db_default then mark it as nullable. Then delete the column using SafeRemoveField(..., deletion_action=DeletionAction.MOVE_TO_PENDING). These operations can be in the same migration to save time.
243
+
- Deploy all previous before continuing.
244
+
- Remove the column from the table in from Postgres using SafeRemoveField(..., deletion_action=DeletionAction.DELETE),
245
+
194
246
### Deleting Tables
195
247
196
248
Extra care is needed here if the table is referenced as a foreign key in other tables. In that case, first remove the foreign key columns in the other tables, then come back to this step.
197
249
198
-
- Remove any database level foreign key constraints from this table to other tables by setting `db_constraint=False` on the columns.
199
-
- Deploy
200
-
- Remove the model and all references from the sentry codebase. Make sure that the migration only marks the state as removed.
201
-
- Deploy.
202
-
- Create a migrations that deletes the table.
250
+
- (Optional, but ideal) Make a PR to remove all uses of the model 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
251
+
- Make another PR to
252
+
- Remove any database level foreign key constraints from this table to other tables by setting `db_constraint=False` on the columns.
253
+
- Remove the model and in the generated migration use `SafeDeleteModel(..., deletion_action=DeletionAction.MOVE_TO_PENDING)` to replace `DeleteModel(...)`. This only marks the state for the model as removed.
254
+
- Deploy. It's important that all previous prs are in production before we remove the actual table.
255
+
- Make a PR that create a new migration that has the same `SafeDeleteModel` operation as before, but set `deletion_action=DeletionAction.DELETE` instead. This deletes the actual table from Postgres.
First we checked that it's not referenced by any other models, and it's not. Next we need to remove and db level foreign key constraints. To do this, we change these two columns and generate a migration:
273
+
First, we remove all references to this model from the codebase, including making sure that it's not referenced by any other models. This is best done as a separate pr to keep things clean.
274
+
275
+
Next we need to remove any db level foreign key constraints. To do this, we change this column and generate a migration:
So now we deploy this and move onto the next stage.
305
+
Deleting this way is unsafe, so we want to replace this with `SafeDeleteModel` instead. So this becomes
258
306
259
-
The next stage involves removing all references to the model from the codebase. So we do that, and then we generate a migration that removes the model from the migration state, but not the database. The operations in this migration look like
Using `SafeDeleteModel` allows us to remove all the state related to the model, but defer deleting it until a later migration. So now, we can combine the migration to remove the constraints and delete the model state together like so
and the generated SQL shows no database changes occurring. So now we deploy this and move into the final step.
329
+
So now we deploy this migration and move onto the final step.
271
330
272
-
In this last step, we just want to manually write DDL to remove the table. So we use `sentry django makemigrations --empty` to produce an empty migration, and then modify the operations to be like:
331
+
In this last step, we will use `SafeDeleteModel` again to finally remove the table from Postgres. First, we generate an empty migration using `sentry django makemigrations <your_app> --empty` to produce an empty migration, and then modify the operations to be like:
273
332
274
333
```python
275
334
operations = [
276
-
migrations.RunSQL(
277
-
"""
278
-
DROP TABLE "sentry_alertruletriggeraction";
279
-
""",
280
-
reverse_sql="CREATE TABLE sentry_alertruletriggeraction (fake_col int)", # We just create a fake table here so that the DROP will work if we roll back the migration.
Then we deploy this and we're done. So to recap the steps here:
340
+
- Remove all references to the model in the code in a separate PR and merge. Doesn't matter if this deploys before the next step or not.
341
+
- Remove any fk constraints and delete the model using SafeDeleteModel(..., deletion_action=DeletionAction.MOVE_TO_PENDING). These operations can be in the same migration to save time.
342
+
- Deploy all previous before continuing.
343
+
- Remove the table from Postgres using SafeDeleteModel(..., deletion_action=DeletionAction.DELETE),
0 commit comments