@@ -186,7 +186,8 @@ def upsert_sample(db: DatabaseSession,
186
186
collection_identifier : Optional [str ],
187
187
collection_date : Optional [str ],
188
188
encounter_id : Optional [int ],
189
- additional_details : dict ) -> Tuple [Any , str ]:
189
+ additional_details : dict ,
190
+ access_role : Optional [str ] = None ) -> Tuple [Any , str ]:
190
191
"""
191
192
Upsert sample by its *identifier* and/or *collection_identifier*.
192
193
An existing sample has its *identifier*, *collection_identifier*,
@@ -200,13 +201,15 @@ def upsert_sample(db: DatabaseSession,
200
201
"collection_date" : collection_date ,
201
202
"encounter_id" : encounter_id ,
202
203
"additional_details" : Json (additional_details ) if additional_details else None ,
204
+ "additional_details_without_prov" : Json ({k : additional_details [k ] for k in additional_details if k != '_provenance' }) if additional_details else None ,
205
+ "access_role" : access_role ,
203
206
}
204
207
205
208
# Look for existing sample(s)
206
209
with db .cursor () as cursor :
207
210
cursor .execute ("""
208
211
select
209
- sample_id as id, identifier, collection_identifier, encounter_id, details,
212
+ sample_id as id, identifier, collection_identifier, encounter_id, details, access_role,
210
213
row (
211
214
identifier,
212
215
collection_identifier
@@ -223,8 +226,9 @@ def upsert_sample(db: DatabaseSession,
223
226
row(
224
227
coalesce(%(collection_date)s, collected)::timestamp,
225
228
coalesce(%(encounter_id)s::integer, encounter_id),
226
- coalesce(details, '{}'::jsonb) || coalesce(%(additional_details)s, '{}')::jsonb
227
- )::text as metadata_changed
229
+ coalesce(details, '{}'::jsonb) || coalesce(%(additional_details_without_prov)s, '{}')::jsonb
230
+ )::text as metadata_changed,
231
+ row(access_role)::text != row(coalesce(%(access_role)s, access_role))::text as access_role_changed
228
232
from warehouse.sample
229
233
where identifier = %(identifier)s
230
234
or collection_identifier = %(collection_identifier)s
@@ -239,12 +243,13 @@ def upsert_sample(db: DatabaseSession,
239
243
status = 'created'
240
244
241
245
sample = db .fetch_row ("""
242
- insert into warehouse.sample (identifier, collection_identifier, collected, encounter_id, details)
246
+ insert into warehouse.sample (identifier, collection_identifier, collected, encounter_id, details, access_role )
243
247
values (%(identifier)s,
244
248
%(collection_identifier)s,
245
249
date_or_null(%(collection_date)s),
246
250
%(encounter_id)s,
247
- %(additional_details)s)
251
+ %(additional_details)s,
252
+ %(access_role)s)
248
253
returning sample_id as id, identifier, collection_identifier, encounter_id
249
254
""" , data )
250
255
@@ -256,9 +261,10 @@ def upsert_sample(db: DatabaseSession,
256
261
LOG .info (f"Updating existing sample { sample .id } " )
257
262
LOG .info (f"Sample.identifiers_changed is «{ sample .identifiers_changed } » " )
258
263
LOG .info (f"Sample.metadata_changed is «{ sample .metadata_changed } » " )
264
+ LOG .info (f"Sample.access_role_changed is «{ sample .access_role_changed } » " )
259
265
260
266
# can safely skip upsert if metadata is unchanged and not updating identifiers or if all data is unchanged
261
- if sample .metadata_changed == False and (not update_identifiers or sample .identifiers_changed == False ):
267
+ if sample .metadata_changed == False and sample . access_role_changed == False and (not update_identifiers or sample .identifiers_changed == False ):
262
268
LOG .info (f"Skipping upsert for sample { sample .id } «{ sample .identifier } » (no change)." )
263
269
return sample , status
264
270
@@ -286,16 +292,22 @@ def upsert_sample(db: DatabaseSession,
286
292
if overwrite_collection_date else SQL ("""
287
293
collected = coalesce(collected, date_or_null(%(collection_date)s)), """ )
288
294
295
+ # Update access_role if value changed
296
+ access_role_update_composable = SQL ("""
297
+ access_role = %(access_role)s, """ ) if sample .access_role_changed else SQL ("" )
298
+
289
299
sample = db .fetch_row (SQL ("""
290
300
update warehouse.sample
291
301
set {}
302
+ {}
292
303
{}
293
304
encounter_id = coalesce(%(encounter_id)s, encounter_id),
294
305
details = coalesce(details, {}) || %(additional_details)s
295
306
where sample_id = %(sample_id)s
296
307
returning sample_id as id, identifier, collection_identifier, encounter_id
297
308
""" ).format (identifiers_update_composable ,
298
309
collected_update_composable ,
310
+ access_role_update_composable ,
299
311
Literal (Json ({}))),
300
312
{ ** data , "sample_id" : sample .id })
301
313
0 commit comments