|
| 1 | +Database migrations |
| 2 | +=================== |
| 3 | + |
| 4 | +We use `Django migrations <https://docs.djangoproject.com/en/4.2/topics/migrations/>`__ to manage database schema changes, |
| 5 | +and the `django-safemigrate <https://github.com/aspiredu/django-safemigrate>`__ package to ensure that migrations are run in a given order to avoid downtime. |
| 6 | + |
| 7 | +To make sure that migrations don't cause downtime, |
| 8 | +the following rules should be followed for each case. |
| 9 | + |
| 10 | +Adding a new field |
| 11 | +------------------ |
| 12 | + |
| 13 | +**When adding a new field to a model, it should be nullable.** |
| 14 | +This way, the database can be migrated without downtime, and the field can be populated later. |
| 15 | +Don't forget to make the field non-nullable in a separate migration after the data has been populated. |
| 16 | +You can achieve this by following these steps: |
| 17 | + |
| 18 | +- #. Set the new field as ``null=True`` and ``blank=True`` in the model. |
| 19 | + |
| 20 | + .. code-block:: python |
| 21 | +
|
| 22 | + class MyModel(models.Model): |
| 23 | + new_field = models.CharField( |
| 24 | + max_length=100, null=True, blank=True, default="default" |
| 25 | + ) |
| 26 | +
|
| 27 | +- #. Make sure that the field is always populated with a proper value in the new code, |
| 28 | + and the code handles the case where the field is null. |
| 29 | + |
| 30 | + .. code-block:: python |
| 31 | +
|
| 32 | + if my_model.new_field in [None, "default"]: |
| 33 | + pass |
| 34 | +
|
| 35 | +
|
| 36 | + # If it's a boolean field, make sure that the null option is removed from the form. |
| 37 | + class MyModelForm(forms.ModelForm): |
| 38 | + def __init__(self, *args, **kwargs): |
| 39 | + super().__init__(*args, **kwargs) |
| 40 | + self.fields["new_field"].widget = forms.CheckboxInput() |
| 41 | + self.fields["new_field"].empty_value = False |
| 42 | +
|
| 43 | +- #. Create the migration file (let's call this migration ``app 0001``), |
| 44 | + and mark it as ``Safe.before_deploy``. |
| 45 | + |
| 46 | + .. code-block:: python |
| 47 | +
|
| 48 | + from django.db import migrations, models |
| 49 | + from django_safemigrate import Safe |
| 50 | +
|
| 51 | +
|
| 52 | + class Migration(migrations.Migration): |
| 53 | + safe = Safe.before_deploy |
| 54 | +
|
| 55 | +- #. Create a data migration to populate all null values of the new field with a proper value (let's call this migration ``app 0002``), |
| 56 | + and mark it as ``Safe.after_deploy``. |
| 57 | + |
| 58 | + .. code-block:: python |
| 59 | +
|
| 60 | + from django.db import migrations |
| 61 | +
|
| 62 | +
|
| 63 | + def migrate(apps, schema_editor): |
| 64 | + MyModel = apps.get_model("app", "MyModel") |
| 65 | + MyModel.objects.filter(new_field=None).update(new_field="default") |
| 66 | +
|
| 67 | +
|
| 68 | + class Migration(migrations.Migration): |
| 69 | + safe = Safe.after_deploy |
| 70 | +
|
| 71 | + operations = [ |
| 72 | + migrations.RunPython(migrate), |
| 73 | + ] |
| 74 | +
|
| 75 | +- #. After the deploy has been completed, create a new migration to set the field as non-nullable (let's call this migration ``app 0003``). |
| 76 | + Run this migration on a new deploy, you can mark it as ``Safe.before_deploy`` or ``Safe.always``. |
| 77 | +- #. Remove any handling of the null case from the code. |
| 78 | + |
| 79 | +At the end, the deploy should look like this: |
| 80 | + |
| 81 | +- Deploy web-extra. |
| 82 | +- Run ``django-admin safemigrate`` to run the migration ``app 0001``. |
| 83 | +- Deploy the webs |
| 84 | +- Run ``django-admin migrate`` to run the migration ``app 0002``. |
| 85 | +- Create a new migration to set the field as non-nullable, |
| 86 | + and apply it on the next deploy. |
| 87 | + |
| 88 | +Removing a field |
| 89 | +---------------- |
| 90 | + |
| 91 | +**When removing a field from a model, |
| 92 | +all usages of the field should be removed from the code before the field is removed from the model, |
| 93 | +and the field should be nullable.** |
| 94 | +You can achieve this by following these steps: |
| 95 | + |
| 96 | +- #. Remove all usages of the field from the code. |
| 97 | +- #. Set the field as ``null=True`` and ``blank=True`` in the model. |
| 98 | + |
| 99 | + .. code-block:: python |
| 100 | +
|
| 101 | + class MyModel(models.Model): |
| 102 | + field_to_delete = models.CharField(max_length=100, null=True, blank=True) |
| 103 | +
|
| 104 | +- #. Create the migration file (let's call this migration ``app 0001``), |
| 105 | + and mark it as ``Safe.before_deploy``. |
| 106 | + |
| 107 | + .. code-block:: python |
| 108 | +
|
| 109 | + from django.db import migrations, models |
| 110 | + from django_safemigrate import Safe |
| 111 | +
|
| 112 | +
|
| 113 | + class Migration(migrations.Migration): |
| 114 | + safe = Safe.before_deploy |
| 115 | +
|
| 116 | +- #. Create a migration to remove the field from the database (let's call this migration ``app 0002``), |
| 117 | + and mark it as ``Safe.after_deploy``. |
| 118 | + |
| 119 | + .. code-block:: python |
| 120 | +
|
| 121 | + from django.db import migrations, models |
| 122 | + from django_safemigrate import Safe |
| 123 | +
|
| 124 | +
|
| 125 | + class Migration(migrations.Migration): |
| 126 | + safe = Safe.after_deploy |
| 127 | +
|
| 128 | +At the end, the deploy should look like this: |
| 129 | + |
| 130 | +- Deploy web-extra. |
| 131 | +- Run ``django-admin safemigrate`` to run the migration ``app 0001``. |
| 132 | +- Deploy the webs |
| 133 | +- Run ``django-admin migrate`` to run the migration ``app 0002``. |
0 commit comments