1414# limitations under the License.
1515"""Retrieves table metadata from Spanner."""
1616
17- from collections import defaultdict
17+ import collections
1818
1919from spanner_orm import condition
20+ from spanner_orm import error
2021from spanner_orm import model
2122from spanner_orm import update
2223from spanner_orm .admin import api
@@ -30,24 +31,35 @@ class DatabaseMetadata(object):
3031
3132 @classmethod
3233 def column_update (cls , schema_change ):
33- assert isinstance (schema_change , update .ColumnUpdate )
34+ """Handles an ALTER TABLE schema update."""
35+ if not isinstance (schema_change , update .ColumnUpdate ):
36+ raise error .SpannerError (
37+ 'column_update must be provided a ColumnUpdate' )
3438 klass = cls .models ()[schema_change .table ()]
3539 schema_change .validate (klass )
3640
3741 api .SpannerAdminApi .update_schema (schema_change .ddl (klass ))
3842
3943 @classmethod
4044 def create_table (cls , schema_change ):
41- assert isinstance (schema_change , update .CreateTableUpdate )
45+ """Handles a CREATE TABLE schema update."""
46+ if not isinstance (schema_change , update .CreateTableUpdate ):
47+ raise error .SpannerError (
48+ 'create_table must be provided a CreateTableUpdate' )
4249 all_models = cls .models ()
43- assert schema_change .table () not in all_models
50+ if schema_change .table () in all_models :
51+ raise error .SpannerError (
52+ 'Cannot create a table that already exists' )
4453 schema_change .validate ()
4554
4655 api .SpannerAdminApi .update_schema (schema_change .ddl ())
4756
4857 @classmethod
4958 def index_update (cls , schema_change ):
50- assert isinstance (schema_change , update .IndexUpdate )
59+ """Handles a CREATE INDEX or DROP INDEX schema update."""
60+ if not isinstance (schema_change , update .IndexUpdate ):
61+ raise error .SpannerError (
62+ 'index_update must be provided a IndexUpdate' )
5163 klass = cls .models ()[schema_change .table ()]
5264 schema_change .validate (klass )
5365
@@ -80,7 +92,7 @@ def make_classmethod(retval):
8092 @classmethod
8193 def _tables (cls , transaction = None ):
8294 """Compiles table information from column schema."""
83- tables = defaultdict (dict )
95+ tables = collections . defaultdict (dict )
8496 schemas = column .ColumnSchema .where (
8597 transaction , condition .EqualityCondition ('table_catalog' , '' ),
8698 condition .EqualityCondition ('table_schema' , '' ))
@@ -102,15 +114,15 @@ def _indexes(cls, transaction=None):
102114 condition .OrderByCondition (('ordinal_position' ,
103115 condition .OrderType .ASC )))
104116
105- index_columns = defaultdict (list )
117+ index_columns = collections . defaultdict (list )
106118 for schema in index_column_schemas :
107119 key = (schema .table_name , schema .index_name )
108120 index_columns [key ].append (schema .column_name )
109121
110122 index_schemas = index .IndexSchema .where (
111123 transaction , condition .EqualityCondition ('table_catalog' , '' ),
112124 condition .EqualityCondition ('table_schema' , '' ))
113- indexes = defaultdict (dict )
125+ indexes = collections . defaultdict (dict )
114126 for schema in index_schemas :
115127 indexes [schema .table_name ][schema .index_name ] = {
116128 'columns' : index_columns [(schema .table_name , schema .index_name )],
0 commit comments