fix: invalidate empty-table proof after writes and table renames
This commit is contained in:
1 parent
c7a4376d35
commit
66f39b1c48
7 files changed
+285
-29
No files matched your search
@@ -11,12 +11,13 @@ from django_migration_linter.sql_analyser import (
|
||||
class SqlAnalyserTestCase(unittest.TestCase):
|
||||
database_vendor = "default"
|
||||
|
||||
def analyse_sql(self, sql):
|
||||
def analyse_sql(self, sql, **kwargs):
|
||||
if isinstance(sql, str):
|
||||
sql = sql.splitlines()
|
||||
return analyse_sql_statements(
|
||||
get_sql_analyser_class(self.database_vendor),
|
||||
sql_statements=sql,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def assertValidSql(self, sql, allow_warnings=False):
|
||||
@@ -249,6 +250,31 @@ class PostgresqlAnalyserTestCase(SqlAnalyserTestCase):
|
||||
]
|
||||
self.assertValidSql(sql)
|
||||
|
||||
def test_add_required_foreign_key_after_insert_into_new_table(self):
|
||||
sql = [
|
||||
'CREATE TABLE "archive_capture" ("id" uuid NOT NULL PRIMARY KEY);',
|
||||
'INSERT INTO "archive_capture" ("id") VALUES (gen_random_uuid());',
|
||||
'ALTER TABLE "archive_capture" ADD COLUMN "source_id" uuid NOT NULL REFERENCES "archive_source" ("id") DEFERRABLE INITIALLY DEFERRED;',
|
||||
]
|
||||
self.assertBackwardIncompatibleSql(sql, "NOT_NULL")
|
||||
|
||||
def test_add_required_foreign_key_after_unknown_data_operation(self):
|
||||
sql = [
|
||||
'CREATE TABLE "archive_capture" ("id" uuid NOT NULL PRIMARY KEY);',
|
||||
'ALTER TABLE "archive_capture" ADD COLUMN "source_id" uuid NOT NULL REFERENCES "archive_source" ("id") DEFERRABLE INITIALLY DEFERRED;',
|
||||
]
|
||||
errors, _, _ = self.analyse_sql(sql, has_unknown_data_operations=True)
|
||||
self.assertTrue(any(error.code == "NOT_NULL" for error in errors))
|
||||
|
||||
def test_add_required_foreign_key_after_table_name_reuse(self):
|
||||
sql = [
|
||||
'CREATE TABLE "foo" ("id" uuid NOT NULL PRIMARY KEY);',
|
||||
'ALTER TABLE "foo" RENAME TO "baz";',
|
||||
'ALTER TABLE "bar" RENAME TO "foo";',
|
||||
'ALTER TABLE "foo" ADD COLUMN "source_id" uuid NOT NULL REFERENCES "archive_source" ("id");',
|
||||
]
|
||||
self.assertBackwardIncompatibleSql(sql, "NOT_NULL")
|
||||
|
||||
def test_alter_column(self):
|
||||
sql = 'ALTER TABLE "app_alter_column_a" ALTER COLUMN "field" TYPE varchar(10) USING "field"::varchar(10);'
|
||||
self.assertBackwardIncompatibleSql(sql)
|
||||
|
||||
Reference in new issue
Block a user