|
| 1 | +--- |
| 2 | +title: Manually review and modify database migrations |
| 3 | +description: Alembic can generate database migrations parting from model changes, but sometimes we need to modify them manually. |
| 4 | +--- |
| 5 | + |
| 6 | +# Manually review and modify database migrations |
| 7 | + |
| 8 | +## Default column values |
| 9 | + |
| 10 | +When you add a column that uses a default value, any rows that existed previously will have `null` as the value. |
| 11 | + |
| 12 | +You'll have to go into the database to add the default value to those rows. |
| 13 | + |
| 14 | +You can also do this during the migration, since Alembic migrations can execute any arbitrary SQL queries. |
| 15 | + |
| 16 | +Here's an example for a column being added with a default value: |
| 17 | + |
| 18 | +```py title="migrations/versions/sample_migration.py" |
| 19 | +from alembic import op |
| 20 | +import sqlalchemy as sa |
| 21 | + |
| 22 | + |
| 23 | +# revision identifiers, used by Alembic. |
| 24 | +revision = "9c386e4052be" |
| 25 | +down_revision = "713af8a4cb34" |
| 26 | +branch_labels = None |
| 27 | +depends_on = None |
| 28 | + |
| 29 | + |
| 30 | +def upgrade(): |
| 31 | + # ### commands auto generated by Alembic - please adjust! ### |
| 32 | + op.add_column( |
| 33 | + "invoices", |
| 34 | + sa.Column("enable_downloads", sa.Boolean(), nullable=True, default=False), |
| 35 | + ) |
| 36 | + # ### end Alembic commands ### |
| 37 | + |
| 38 | + |
| 39 | +def downgrade(): |
| 40 | + # ### commands auto generated by Alembic - please adjust! ### |
| 41 | + op.drop_column("invoices", "enable_downloads") |
| 42 | + # ### end Alembic commands ### |
| 43 | +``` |
| 44 | + |
| 45 | +You can see that we're adding a column called `enable_downloads` to the `invoices` table. The default value for new rows will be `False`, but what is the value for all the invoices we already have in the database? |
| 46 | + |
| 47 | +The value will be undefined, or `null`. |
| 48 | + |
| 49 | +What we must do is tell Alembic to insert `False` into each of the existing rows. We can do that with SQL: |
| 50 | + |
| 51 | +```py title="migrations/versions/sample_migration.py" |
| 52 | +from alembic import op |
| 53 | +import sqlalchemy as sa |
| 54 | + |
| 55 | + |
| 56 | +# revision identifiers, used by Alembic. |
| 57 | +revision = "9c386e4052be" |
| 58 | +down_revision = "713af8a4cb34" |
| 59 | +branch_labels = None |
| 60 | +depends_on = None |
| 61 | + |
| 62 | + |
| 63 | +def upgrade(): |
| 64 | + # ### commands auto generated by Alembic - please adjust! ### |
| 65 | + op.add_column( |
| 66 | + "invoices", |
| 67 | + sa.Column("enable_downloads", sa.Boolean(), nullable=True, default=False), |
| 68 | + ) |
| 69 | + # highlight-start |
| 70 | + op.execute("UPDATE invoices SET enable_downloads = False") |
| 71 | + # highlight-end |
| 72 | + # ### end Alembic commands ### |
| 73 | + |
| 74 | + |
| 75 | +def downgrade(): |
| 76 | + # ### commands auto generated by Alembic - please adjust! ### |
| 77 | + op.drop_column("invoices", "enable_downloads") |
| 78 | + # ### end Alembic commands ### |
| 79 | +``` |
0 commit comments