Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
feat: Simpler column add migration guide (#1051)
Browse files Browse the repository at this point in the history
We don't need to add columns with dangerous migrations anymore. All
column adds can be done as deploy time migrations.
  • Loading branch information
markstory authored Oct 16, 2023
1 parent ef57785 commit 20f9cd6
Showing 1 changed file with 3 additions and 45 deletions.
48 changes: 3 additions & 45 deletions src/docs/database-migrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -290,55 +290,13 @@ Renaming tables is dangerous and will result in downtime. The reason this occurs

### Adding Columns

When creating new columns they should either be:
With postgres 14, columns can be added to tables of all sizes as deploy time
migrations if you follow the guidelines on default values & allowing nulls. When
creating new columns they should either be:

- Not null with a default. https://develop.sentry.dev/database-migrations/#adding-not-null-to-columns
- Created as nullable. If no default value can be set on the column, then it's best just to make it nullable.

### Adding Columns to Large Tables

Because adding a column to a large table (more than 10M rows) can exceed the 5s
statement timeout we have during deploys, you need to use two migrations to
safely ship changes to large tables.

First create the column in the database without telling Django about the presence of the column. This is an `is_dangerous` operation. It should look similar to:

```python
is_dangerous = True

operations = [
migrations.SeparateDatabaseAndState(
database_operations=[
migrations.AddField(
model_name="group",
name="snooze_counter",
field=sentry.db.models.fields.bounded.BoundedBigIntegerField(),
)
],
state_operations=[],
)
]
```

Once this migration has been executed, you can deploy updates to Django's model
state. Update your Django ORM model with the new field, and use another
migration to update Django's schema state:

```python
operations = [
migrations.SeparateDatabaseAndState(
database_operations=[],
state_operations=[
migrations.AddField(
model_name="group",
name="snooze_counter",
field=sentry.db.models.fields.bounded.BoundedBigIntegerField(),
)
],
)
]
```

### Adding Columns With a Default

We run Postgres 14 in production. This means that we can now safely add columns with a default without worrying about rewriting the table. We still need to be careful though.
Expand Down

1 comment on commit 20f9cd6

@vercel
Copy link

@vercel vercel bot commented on 20f9cd6 Oct 16, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

develop – ./

develop-git-master.sentry.dev
develop.sentry.dev

Please sign in to comment.