Skip to content

Commit 397c1f0

Browse files
christianbundyDavid-Wobrock
authored andcommitted
Default ends_with_default to False
Problem: Previously in gh-117 we aimed to solve gh-115, which described a problem where we used `SET DEFAULT` and then `DROP DEFAULT` immediately afterward. The solution was to track whether any defaults are retained, but the `ends_with_default` variable is initialized as `None` rather than `False`. At the end of the function we check whether `ends_with_default is False`, never occurs unless a default is set. Solution: Assume that there is no default unless proven otherwise, and iniitializie `ends_with_default` to `False` instead of `None`. Fixes: gh-158
1 parent 3f0531f commit 397c1f0

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

django_migration_linter/sql_analyser/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def has_not_null_column(sql_statements, **kwargs):
1010
# TODO: improve to detect that the same column is concerned
11-
ends_with_default = None
11+
ends_with_default = False
1212
for sql in sql_statements:
1313
if "SET DEFAULT" in sql:
1414
ends_with_default = True

tests/unit/test_sql_analyser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ def test_not_null_followed_by_default(self):
171171
]
172172
self.assertValidSql(sql)
173173

174+
def test_field_to_not_null_with_dropped_default(self):
175+
sql = [
176+
'ALTER TABLE "api_example_example" ALTER COLUMN "foo_id" SET DEFAULT 42;',
177+
'UPDATE "example_example" SET "foo_id" = 42 WHERE "foo_id" IS NULL;',
178+
'ALTER TABLE "example_example" ALTER COLUMN "foo_id" SET NOT NULL;',
179+
'ALTER TABLE "example_example" ALTER COLUMN "foo_id" DROP DEFAULT;',
180+
]
181+
self.assertBackwardIncompatibleSql(sql)
182+
183+
def test_onetoonefield_to_not_null(self):
184+
sql = [
185+
'ALTER TABLE "example_example" ALTER COLUMN "foo" SET NOT NULL;',
186+
]
187+
self.assertBackwardIncompatibleSql(sql)
188+
174189
def test_drop_not_null(self):
175190
sql = 'ALTER TABLE "app_alter_column_drop_not_null_a" ALTER COLUMN "not_null_field" DROP NOT NULL;'
176191
self.assertValidSql(sql)

0 commit comments

Comments
 (0)