Util for marking Django DB fields as deprecated, enabling migration consistency with rolling deploys
Deprecated. Its authors have deprecated it and no longer maintain it.
pip install django-deprecated-fieldDjango Deprecated Field
A Django field util that allows you to mark model fields as deprecated. This will enable you to have migration consistency when using rolling deploy.
Motivation
When you have multiple instances of Django running, for redundancy, you typically deploy new versions in a rolling manner. One instance will be updated to the new versions before the other, and for a period, you will be running both the new and the old version in parallel. Normally, the database migrations will be run before any of the Django instances are updated. This poses a problem for certain types of database migrations, since your database schema needs to support both versions at all times.
One of these problematic migrations is deleting a column from a table. You can quickly end up in a situation where the row has been deleted from the table with the migration, and you have one Django instance to expects it to still exist. Since Django enumerates all known fields in database queries (SELECT field1, field2 FROM ... not SELECT * FROM ...), the instance with the old version will generate queries that aren't consistent with the database schema, and will fail.
One approach to this can be to first deploy a version that has the field removed from the Django model class, but doesn't include the accompanying database migration file. That will remove the Django instances' dependency on the field. Then, in a second deploy that contains the migration file, the column is actually deleted from the database table.
However, this means that you have to allow builds/deploys that have inconsistencies between the Django models in the code, and the migrations. That can lead to a lot of other, unwanted inconsistencies.
django-deprecated-field solves this problem, by introducing the notion of a field being "deprecated, marked for deletion".
Features
When a field is deprecated, we:
- Ensure that is is nullable, which is necessary for cross-version compatibility for
INSERTs - Omit the field from
SELECTs and other queries, ensuring cross-version compatibility forSELECTs, etc - Log a warning, or raise an exception, if the deprecated field is accessed in any code
Usage
Mark a field as deprecated:
from django.db import models
from django_deprecated_field import deprecated
class MyClass(models.Model):
my_field = deprecated(
models.CharField(...)
)
The deprecated() field will return a new version of the CharField that ensures that it will be compatible with both new and old versions. If the field wasn't nullable before, it will become so now, so you may have to generate a new migration .