Fix #209 - Raise when getting SQL statements fail.

This commit is contained in:
David Wobrock authored and David Wobrock committed 2022-07-28 23:45:14 +02:00
1 parent 74a7ee9239
commit bef47906d1
4 files changed
+34 -7

No files matched your search

+5 -1
View File
@@ -1,5 +1,9 @@
## 4.1.1 (unreleased) ## 5.0.0 (unreleased)
- **Breaking change**: stop silently ignoring when the internal `sqlmigrate` call fails and the linter cannot analyse the migration.
Instead, the linter crashes and lets the `sqlmigrate` error raise, in order to avoid letting a problematic migration pass.
One common reason for such an error is the SQL generation which requires the database to be actually migrated in order to fetch actual constraint names from it.
The crash is a sign to double-check the migration. But if you are certain the migration is safe, you can ignore it (issue #209)
- Fixed `RunPython` model import check when using a `through` object like `MyModel.many_to_many.through.objects.filter(...)` (issue #218) - Fixed `RunPython` model import check when using a `through` object like `MyModel.many_to_many.through.objects.filter(...)` (issue #218)
## 4.1.0 ## 4.1.0
+2 -5
View File
@@ -315,14 +315,11 @@ class MigrationLinter(object):
) )
except (ValueError, ProgrammingError): except (ValueError, ProgrammingError):
logger.warning( logger.warning(
( "Error while executing sqlmigrate on (%s, %s).",
"Error while executing sqlmigrate on (%s, %s). "
"Continuing execution with empty SQL."
),
app_label, app_label,
migration_name, migration_name,
) )
sql_statement = "" raise
return sql_statement.splitlines() return sql_statement.splitlines()
@staticmethod @staticmethod
@@ -8,6 +8,11 @@ from django.test.utils import override_settings
class LintMigrationsCommandTestCase(TransactionTestCase): class LintMigrationsCommandTestCase(TransactionTestCase):
databases = {"default", "sqlite"} databases = {"default", "sqlite"}
def setUp(self):
call_command("migrate", "app_unique_together", "0002")
self.addCleanup(call_command, "migrate", "app_unique_together")
super().setUp()
def test_plain(self): def test_plain(self):
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
call_command("lintmigrations") call_command("lintmigrations")
+22 -1
View File
@@ -2,6 +2,7 @@ import os
import unittest import unittest
from django.conf import settings from django.conf import settings
from django.core.management import call_command
from django_migration_linter import MigrationLinter from django_migration_linter import MigrationLinter
from tests import fixtures from tests import fixtures
@@ -9,7 +10,20 @@ from tests import fixtures
class BaseBackwardCompatibilityDetection(object): class BaseBackwardCompatibilityDetection(object):
def setUp(self, *args, **kwargs): def setUp(self, *args, **kwargs):
self.database = next(iter(self.databases))
self.test_project_path = os.path.dirname(settings.BASE_DIR) self.test_project_path = os.path.dirname(settings.BASE_DIR)
call_command(
"migrate",
"app_unique_together",
"0002",
database=self.database,
)
self.addCleanup(
call_command,
"migrate",
"app_unique_together",
database=self.database,
)
return super(BaseBackwardCompatibilityDetection, self).setUp(*args, **kwargs) return super(BaseBackwardCompatibilityDetection, self).setUp(*args, **kwargs)
def _test_linter_finds_errors(self, app=None, commit_id=None): def _test_linter_finds_errors(self, app=None, commit_id=None):
@@ -25,7 +39,7 @@ class BaseBackwardCompatibilityDetection(object):
def _launch_linter(self, app=None, commit_id=None): def _launch_linter(self, app=None, commit_id=None):
linter = MigrationLinter( linter = MigrationLinter(
self.test_project_path, self.test_project_path,
database=next(iter(self.databases)), database=self.database,
no_cache=True, no_cache=True,
) )
linter.lint_all_migrations(app_label=app, git_commit_id=commit_id) linter.lint_all_migrations(app_label=app, git_commit_id=commit_id)
@@ -91,6 +105,13 @@ class BaseBackwardCompatibilityDetection(object):
def test_with_git_ref(self): def test_with_git_ref(self):
self._test_linter_finds_errors(commit_id="v0.1.4") self._test_linter_finds_errors(commit_id="v0.1.4")
def test_failing_get_sql(self):
call_command("migrate", "app_unique_together", database=self.database)
linter = MigrationLinter(database=self.database)
with self.assertRaises(ValueError):
linter.get_sql("app_unique_together", "0003")
class SqliteBackwardCompatibilityDetectionTestCase( class SqliteBackwardCompatibilityDetectionTestCase(
BaseBackwardCompatibilityDetection, unittest.TestCase BaseBackwardCompatibilityDetection, unittest.TestCase