Fixed #30123 -- Removed tuple support in DatabaseIntrospection.get_field_type().

Support for returning tuples was undocumented and error prone.
This commit is contained in:
Nick Pope 2019-01-19 13:35:51 +00:00 committed by Tim Graham
parent 8d01edfa65
commit 0ef9979669
3 changed files with 9 additions and 19 deletions

View File

@ -241,12 +241,6 @@ class Command(BaseCommand):
field_type = 'TextField' field_type = 'TextField'
field_notes.append('This field type is a guess.') field_notes.append('This field type is a guess.')
# This is a hook for data_types_reverse to return a tuple of
# (field_type, field_params_dict).
if type(field_type) is tuple:
field_type, new_params = field_type
field_params.update(new_params)
# Add max_length for all CharFields. # Add max_length for all CharFields.
if field_type == 'CharField' and row.internal_size: if field_type == 'CharField' and row.internal_size:
field_params['max_length'] = int(row.internal_size) field_params['max_length'] = int(row.internal_size)

View File

@ -211,6 +211,8 @@ backends.
* The second argument of ``DatabaseIntrospection.get_geometry_type()`` is now * The second argument of ``DatabaseIntrospection.get_geometry_type()`` is now
the row description instead of the column name. the row description instead of the column name.
* ``DatabaseIntrospection.get_field_type()`` may no longer return tuples.
Miscellaneous Miscellaneous
------------- -------------

View File

@ -76,7 +76,7 @@ class IntrospectionTests(TransactionTestCase):
with connection.cursor() as cursor: with connection.cursor() as cursor:
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table) desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
self.assertEqual( self.assertEqual(
[datatype(r[1], r) for r in desc], [connection.introspection.get_field_type(r[1], r) for r in desc],
[ [
'AutoField' if connection.features.can_introspect_autofield else 'IntegerField', 'AutoField' if connection.features.can_introspect_autofield else 'IntegerField',
'CharField', 'CharField',
@ -93,7 +93,7 @@ class IntrospectionTests(TransactionTestCase):
with connection.cursor() as cursor: with connection.cursor() as cursor:
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table) desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
self.assertEqual( self.assertEqual(
[r[3] for r in desc if datatype(r[1], r) == 'CharField'], [r[3] for r in desc if connection.introspection.get_field_type(r[1], r) == 'CharField'],
[30, 30, 254] [30, 30, 254]
) )
@ -110,7 +110,10 @@ class IntrospectionTests(TransactionTestCase):
def test_bigautofield(self): def test_bigautofield(self):
with connection.cursor() as cursor: with connection.cursor() as cursor:
desc = connection.introspection.get_table_description(cursor, City._meta.db_table) desc = connection.introspection.get_table_description(cursor, City._meta.db_table)
self.assertIn(connection.features.introspected_big_auto_field_type, [datatype(r[1], r) for r in desc]) self.assertIn(
connection.features.introspected_big_auto_field_type,
[connection.introspection.get_field_type(r[1], r) for r in desc],
)
# Regression test for #9991 - 'real' types in postgres # Regression test for #9991 - 'real' types in postgres
@skipUnlessDBFeature('has_real_datatype') @skipUnlessDBFeature('has_real_datatype')
@ -119,7 +122,7 @@ class IntrospectionTests(TransactionTestCase):
cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);") cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table') desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
cursor.execute('DROP TABLE django_ixn_real_test_table;') cursor.execute('DROP TABLE django_ixn_real_test_table;')
self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField') self.assertEqual(connection.introspection.get_field_type(desc[0][1], desc[0]), 'FloatField')
@skipUnlessDBFeature('can_introspect_foreign_keys') @skipUnlessDBFeature('can_introspect_foreign_keys')
def test_get_relations(self): def test_get_relations(self):
@ -208,12 +211,3 @@ class IntrospectionTests(TransactionTestCase):
self.assertEqual(val['orders'], ['ASC'] * len(val['columns'])) self.assertEqual(val['orders'], ['ASC'] * len(val['columns']))
indexes_verified += 1 indexes_verified += 1
self.assertEqual(indexes_verified, 4) self.assertEqual(indexes_verified, 4)
def datatype(dbtype, description):
"""Helper to convert a data type into a string."""
dt = connection.introspection.get_field_type(dbtype, description)
if type(dt) is tuple:
return dt[0]
else:
return dt