6
6
7
7
from django .db import NotSupportedError
8
8
from django .db .backends .base .schema import BaseDatabaseSchemaEditor
9
+ from django_spanner ._opentelemetry_tracing import trace_call
9
10
10
11
11
12
class DatabaseSchemaEditor (BaseDatabaseSchemaEditor ):
@@ -119,7 +120,15 @@ def create_model(self, model):
119
120
sql += " " + tablespace_sql
120
121
# Prevent using [] as params, in the case a literal '%' is used in the
121
122
# definition
122
- self .execute (sql , params or None )
123
+ trace_attributes = {
124
+ "model_name" : self .quote_name (model ._meta .db_table )
125
+ }
126
+ with trace_call (
127
+ "CloudSpannerDjango.create_model" ,
128
+ self .connection ,
129
+ trace_attributes ,
130
+ ):
131
+ self .execute (sql , params or None )
123
132
124
133
# Add any field index and index_together's (deferred as SQLite
125
134
# _remake_table needs it)
@@ -144,8 +153,25 @@ def delete_model(self, model):
144
153
model , index = True , primary_key = False
145
154
)
146
155
for index_name in index_names :
147
- self .execute (self ._delete_index_sql (model , index_name ))
148
- super ().delete_model (model )
156
+ trace_attributes = {
157
+ "model_name" : self .quote_name (model ._meta .db_table ),
158
+ "index_name" : index_name ,
159
+ }
160
+ with trace_call (
161
+ "CloudSpannerDjango.delete_model.delete_index" ,
162
+ self .connection ,
163
+ trace_attributes ,
164
+ ):
165
+ self .execute (self ._delete_index_sql (model , index_name ))
166
+ trace_attributes = {
167
+ "model_name" : self .quote_name (model ._meta .db_table )
168
+ }
169
+ with trace_call (
170
+ "CloudSpannerDjango.delete_model" ,
171
+ self .connection ,
172
+ trace_attributes ,
173
+ ):
174
+ super ().delete_model (model )
149
175
150
176
def add_field (self , model , field ):
151
177
"""
@@ -250,8 +276,28 @@ def remove_field(self, model, field):
250
276
# column.
251
277
index_names = self ._constraint_names (model , [field .column ], index = True )
252
278
for index_name in index_names :
253
- self .execute (self ._delete_index_sql (model , index_name ))
254
- super ().remove_field (model , field )
279
+ trace_attributes = {
280
+ "model_name" : self .quote_name (model ._meta .db_table ),
281
+ "field" : field .column ,
282
+ "index_name" : index_name ,
283
+ }
284
+ with trace_call (
285
+ "CloudSpannerDjango.remove_field.delete_index" ,
286
+ self .connection ,
287
+ trace_attributes ,
288
+ ):
289
+ self .execute (self ._delete_index_sql (model , index_name ))
290
+
291
+ trace_attributes = {
292
+ "model_name" : self .quote_name (model ._meta .db_table ),
293
+ "field" : field .column ,
294
+ }
295
+ with trace_call (
296
+ "CloudSpannerDjango.remove_field" ,
297
+ self .connection ,
298
+ trace_attributes ,
299
+ ):
300
+ super ().remove_field (model , field )
255
301
256
302
def column_sql (
257
303
self , model , field , include_default = False , exclude_not_null = False
@@ -320,7 +366,14 @@ def add_index(self, model, index):
320
366
(field_name , " DESC" if order == "DESC" else "" )
321
367
for field_name , order in index .fields_orders
322
368
]
323
- super ().add_index (model , index )
369
+ trace_attributes = {
370
+ "model_name" : self .quote_name (model ._meta .db_table ),
371
+ "index" : "|" .join (index .fields ),
372
+ }
373
+ with trace_call (
374
+ "CloudSpannerDjango.add_index" , self .connection , trace_attributes ,
375
+ ):
376
+ super ().add_index (model , index )
324
377
325
378
def quote_value (self , value ):
326
379
# A more complete implementation isn't currently required.
@@ -355,20 +408,48 @@ def _alter_field(
355
408
"index isn't yet supported."
356
409
)
357
410
for index_name in index_names :
358
- self .execute (self ._delete_index_sql (model , index_name ))
359
- super ()._alter_field (
360
- model ,
361
- old_field ,
362
- new_field ,
363
- old_type ,
364
- new_type ,
365
- old_db_params ,
366
- new_db_params ,
367
- strict = False ,
368
- )
411
+ trace_attributes = {
412
+ "model_name" : self .quote_name (model ._meta .db_table ),
413
+ "alter_field" : old_field .column ,
414
+ "index_name" : index_name ,
415
+ }
416
+ with trace_call (
417
+ "CloudSpannerDjango.alter_field.delete_index" ,
418
+ self .connection ,
419
+ trace_attributes ,
420
+ ):
421
+ self .execute (self ._delete_index_sql (model , index_name ))
422
+ trace_attributes = {
423
+ "model_name" : self .quote_name (model ._meta .db_table ),
424
+ "alter_field" : old_field .column ,
425
+ }
426
+ with trace_call (
427
+ "CloudSpannerDjango.alter_field" ,
428
+ self .connection ,
429
+ trace_attributes ,
430
+ ):
431
+ super ()._alter_field (
432
+ model ,
433
+ old_field ,
434
+ new_field ,
435
+ old_type ,
436
+ new_type ,
437
+ old_db_params ,
438
+ new_db_params ,
439
+ strict = False ,
440
+ )
369
441
# Recreate the index that was dropped earlier.
370
442
if nullability_changed and new_field .db_index :
371
- self .execute (self ._create_index_sql (model , [new_field ]))
443
+ trace_attributes = {
444
+ "model_name" : self .quote_name (model ._meta .db_table ),
445
+ "alter_field" : new_field .column ,
446
+ }
447
+ with trace_call (
448
+ "CloudSpannerDjango.alter_field.recreate_index" ,
449
+ self .connection ,
450
+ trace_attributes ,
451
+ ):
452
+ self .execute (self ._create_index_sql (model , [new_field ]))
372
453
373
454
def _alter_column_type_sql (self , model , old_field , new_field , new_type ):
374
455
# Spanner needs to use sql_alter_column_not_null if the field is
0 commit comments