-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instruction on removing nullable fields from Django models (#2659)
Adds an instruction on removing nullable fields without downtime.
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from django.db import connection | ||
from django.db.migrations import RemoveField | ||
from django.db.migrations.loader import MigrationLoader | ||
|
||
|
||
class RemoveFieldState(RemoveField): | ||
""" | ||
Remove field from Django's migration state, but not from the database. | ||
This is essentially the same as RemoveField, but database_forwards and database_backwards methods are modified | ||
to do nothing. | ||
""" | ||
|
||
def database_forwards(self, app_label, schema_editor, from_state, to_state): | ||
pass | ||
|
||
def database_backwards(self, app_label, schema_editor, from_state, to_state): | ||
pass | ||
|
||
def describe(self): | ||
return f"{super().describe()} (state)" | ||
|
||
@property | ||
def migration_name_fragment(self): | ||
return f"{super().migration_name_fragment}_state" | ||
|
||
|
||
class RemoveFieldDB(RemoveField): | ||
""" | ||
Remove field from the database, but not from Django's migration state. | ||
This is implemented as a custom operation, because Django's RemoveField operation does not support | ||
removing fields from the database after it has been removed from the state. The workaround is to use the state | ||
that was in effect before the field was removed from the state (i.e. just before the RemoveFieldState migration). | ||
""" | ||
|
||
def __init__(self, model_name, name, remove_state_migration): | ||
""" | ||
Specifying "remove_state_migration" allows database operations to run against a particular historical state. | ||
Example: remove_state_migration = ("alerts", "0014_alertreceivechannel_restricted_at") will "trick" Django | ||
into thinking that the last applied migration in the "alerts" app is 0013. | ||
""" | ||
super().__init__(model_name, name) | ||
self.remove_state_migration = remove_state_migration | ||
|
||
def deconstruct(self): | ||
"""Update serialized representation of the operation.""" | ||
deconstructed = super().deconstruct() | ||
return ( | ||
deconstructed[0], | ||
deconstructed[1], | ||
deconstructed[2] | {"remove_state_migration": self.remove_state_migration} | ||
) | ||
|
||
def state_forwards(self, app_label, state): | ||
"""Skip any state changes.""" | ||
pass | ||
|
||
def database_forwards(self, app_label, schema_editor, from_state, to_state): | ||
# use historical state instead of what Django provides | ||
from_state = self.state_before_remove_state_migration | ||
|
||
super().database_forwards(app_label, schema_editor, from_state, to_state) | ||
|
||
def database_backwards(self, app_label, schema_editor, from_state, to_state): | ||
# use historical state instead of what Django provides | ||
to_state = self.state_before_remove_state_migration | ||
|
||
super().database_backwards(app_label, schema_editor, from_state, to_state) | ||
|
||
def describe(self): | ||
return f"{super().describe()} (db)" | ||
|
||
@property | ||
def migration_name_fragment(self): | ||
return f"{super().migration_name_fragment}_db" | ||
|
||
@property | ||
def state_before_remove_state_migration(self): | ||
"""Get project state just before migration "remove_state_migration" was applied.""" | ||
return MigrationLoader(connection).project_state(self.remove_state_migration, at_end=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.core.management import BaseCommand | ||
from django.db import connection | ||
from django.db.migrations import Migration | ||
from django.db.migrations.autodetector import MigrationAutodetector | ||
from django.db.migrations.loader import MigrationLoader | ||
from django.db.migrations.writer import MigrationWriter | ||
|
||
from common.migrations.remove_field import RemoveFieldDB, RemoveFieldState | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Generate two migrations that remove a field from the state and the database separately. | ||
This allows removing a field in 2 separate releases and avoid downtime. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"args", nargs=3, help="app_label model_name field_name, example: alerts AlertReceiveChannel restricted_at" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
app_label, model_name, field_name = args | ||
|
||
# Check that the app, the model, and the field to be removed exist | ||
project_state = MigrationLoader(connection).project_state() | ||
model_state = project_state.apps.get_model(app_label, model_name) | ||
model_state._meta.get_field(field_name) | ||
|
||
# Write migration that removes the field from the state | ||
remove_state_migration = self.write_operation( | ||
app_label, RemoveFieldState(model_name=model_name, name=field_name), project_state | ||
) | ||
|
||
# Write migration that removes the field from the database | ||
self.write_operation( | ||
app_label, | ||
RemoveFieldDB( | ||
model_name=model_name, name=field_name, remove_state_migration=(app_label, remove_state_migration.name) | ||
), | ||
project_state, | ||
) | ||
|
||
@staticmethod | ||
def write_operation(app_label, operation, project_state): | ||
""" | ||
Some Django magic to write a single-operation migration to a file, so it's similar to what Django would generate | ||
when running the "makemigrations" command. | ||
""" | ||
|
||
migration_class = type("Migration", (Migration,), {"operations": [operation]}) | ||
|
||
changes = MigrationAutodetector(project_state, project_state).arrange_for_graph( | ||
changes={app_label: [migration_class(None, app_label)]}, | ||
graph=MigrationLoader(connection).graph, | ||
migration_name=operation.migration_name_fragment, | ||
) | ||
|
||
migration = changes[app_label][0] | ||
writer = MigrationWriter(migration) | ||
with open(writer.path, "w", encoding="utf-8") as file: | ||
file.write(writer.as_string()) | ||
|
||
return migration |