Skip to content

Commit

Permalink
Disable problematic field alteration skips on SQLite for Djang 3.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
charettes committed Jan 16, 2021
1 parent 76c7278 commit 3a2bbe1
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions syzygy/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def _alter_field_db_default(schema_editor, model, name, drop=False):
schema_editor.execute(sql, params)


@contextmanager
def _force_field_alteration(schema_editor):
# Django 3.2 implements an optimization to prevent SQLite table rebuilds
# when unnecessary. Until proper db_default alteration support lands this
# optimization has to be disabled under some circumstances.
_field_should_be_altered = getattr(schema_editor, "_field_should_be_altered", None)
if _field_should_be_altered is None:
yield
return
schema_editor._field_should_be_altered = lambda old_field, new_field: True
try:
yield
finally:
schema_editor._field_should_be_altered = _field_should_be_altered


@contextmanager
def _include_column_default(schema_editor, field_name):
column_sql_ = schema_editor.column_sql
Expand All @@ -39,7 +55,8 @@ def column_sql(model, field, include_default=False):

schema_editor.column_sql = column_sql
try:
yield
with _force_field_alteration(schema_editor):
yield
finally:
schema_editor.column_sql = column_sql_

Expand Down Expand Up @@ -169,7 +186,10 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
return
if schema_editor.connection.vendor == "sqlite":
# Trigger a table rebuild to DROP the database level DEFAULT
super().database_forwards(app_label, schema_editor, from_state, to_state)
with _force_field_alteration(schema_editor):
super().database_forwards(
app_label, schema_editor, from_state, to_state
)
else:
_alter_field_db_default(schema_editor, model, self.name, drop=True)

Expand Down

0 comments on commit 3a2bbe1

Please sign in to comment.