Fixed #31286 -- Made database specific fields checks databases aware.

Follow up to 0b83c8cc4d.
This commit is contained in:
Taoup 2020-02-19 19:14:54 +08:00 committed by Mariusz Felisiak
parent 94d4bd3a09
commit 271fdab8b7
4 changed files with 12 additions and 9 deletions

View File

@ -335,12 +335,15 @@ class Field(RegisterLookupMixin):
else: else:
return [] return []
def _check_backend_specific_checks(self, **kwargs): def _check_backend_specific_checks(self, databases=None, **kwargs):
if databases is None:
return []
app_label = self.model._meta.app_label app_label = self.model._meta.app_label
for db in connections: errors = []
if router.allow_migrate(db, app_label, model_name=self.model._meta.model_name): for alias in databases:
return connections[db].validation.check_field(self, **kwargs) if router.allow_migrate(alias, app_label, model_name=self.model._meta.model_name):
return [] errors.extend(connections[alias].validation.check_field(self, **kwargs))
return errors
def _check_validators(self): def _check_validators(self):
errors = [] errors = []

View File

@ -27,7 +27,7 @@ class TestMultiDBChecks(SimpleTestCase):
model = Model() model = Model()
with self._patch_check_field_on('default') as mock_check_field_default: with self._patch_check_field_on('default') as mock_check_field_default:
with self._patch_check_field_on('other') as mock_check_field_other: with self._patch_check_field_on('other') as mock_check_field_other:
model.check() model.check(databases={'default', 'other'})
self.assertTrue(mock_check_field_default.called) self.assertTrue(mock_check_field_default.called)
self.assertFalse(mock_check_field_other.called) self.assertFalse(mock_check_field_other.called)
@ -38,6 +38,6 @@ class TestMultiDBChecks(SimpleTestCase):
model = OtherModel() model = OtherModel()
with self._patch_check_field_on('other') as mock_check_field_other: with self._patch_check_field_on('other') as mock_check_field_other:
with self._patch_check_field_on('default') as mock_check_field_default: with self._patch_check_field_on('default') as mock_check_field_default:
model.check() model.check(databases={'default', 'other'})
self.assertTrue(mock_check_field_other.called) self.assertTrue(mock_check_field_other.called)
self.assertFalse(mock_check_field_default.called) self.assertFalse(mock_check_field_default.called)

View File

@ -25,4 +25,4 @@ class BackendSpecificChecksTests(SimpleTestCase):
field = Model._meta.get_field('field') field = Model._meta.get_field('field')
with mock.patch.object(connections['default'].validation, 'check_field', return_value=[error]): with mock.patch.object(connections['default'].validation, 'check_field', return_value=[error]):
self.assertEqual(field.check(), [error]) self.assertEqual(field.check(databases={'default'}), [error])

View File

@ -758,7 +758,7 @@ class TextFieldTests(TestCase):
value = models.TextField(db_index=True) value = models.TextField(db_index=True)
field = Model._meta.get_field('value') field = Model._meta.get_field('value')
field_type = field.db_type(connection) field_type = field.db_type(connection)
self.assertEqual(field.check(), [ self.assertEqual(field.check(databases=self.databases), [
DjangoWarning( DjangoWarning(
'%s does not support a database index on %s columns.' '%s does not support a database index on %s columns.'
% (connection.display_name, field_type), % (connection.display_name, field_type),