Skip to content

Commit f81cf97

Browse files
authored
django: introspect STRING field internal_size
fixes #248
1 parent aea66dc commit f81cf97

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

django_spanner/features.py

-3
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
283283
# DatabaseIntrospection.get_key_columns() is only required if this
284284
# backend needs it (which it currently doesn't).
285285
'introspection.tests.IntrospectionTests.test_get_key_columns',
286-
# DatabaseIntrospection.get_table_description() isn't fully implemented:
287-
# https://github.com/orijtech/django-spanner/issues/248
288-
'introspection.tests.IntrospectionTests.test_get_table_description_col_lengths',
289286
# DatabaseIntrospection.get_relations() isn't implemented:
290287
# https://github.com/orijtech/django-spanner/issues/311
291288
'introspection.tests.IntrospectionTests.test_get_relations',

django_spanner/introspection.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
2121
type_pb2.TIMESTAMP: 'DateTimeField',
2222
}
2323

24+
def get_field_type(self, data_type, description):
25+
if data_type == type_pb2.STRING and description.internal_size == 'MAX':
26+
return 'TextField'
27+
return super().get_field_type(data_type, description)
28+
2429
def get_table_list(self, cursor):
2530
"""Return a list of table and view names in the current database."""
2631
# The second TableInfo field is 't' for table or 'v' for view.
@@ -37,13 +42,19 @@ def get_table_description(self, cursor, table_name):
3742
for line in cursor.description:
3843
column_name, type_code = line[0], line[1]
3944
details = column_details[column_name]
45+
if details.spanner_type.startswith('STRING'):
46+
# Extract the size of the string from, e.g. STRING(#).
47+
internal_size = details.spanner_type[7:-1]
48+
if internal_size != 'MAX':
49+
internal_size = int(internal_size)
50+
else:
51+
internal_size = None
4052
descriptions.append(
4153
FieldInfo(
4254
column_name,
4355
type_code,
44-
# TODO: Fill these in as they're implemented.
4556
None, # display_size
46-
None, # internal_size
57+
internal_size,
4758
None, # precision
4859
None, # scale
4960
details.null_ok,

0 commit comments

Comments
 (0)