@@ -192,24 +192,83 @@ func (p *Persister) createVerifiableAddresses(ctx context.Context, i *identity.I
192
192
defer span .End ()
193
193
194
194
for k := range i .VerifiableAddresses {
195
- i .VerifiableAddresses [k ].IdentityID = i .ID
196
- i .VerifiableAddresses [k ].NID = p .NetworkID (ctx )
197
- i .VerifiableAddresses [k ].Value = stringToLowerTrim (i .VerifiableAddresses [k ].Value )
198
195
if err := p .GetConnection (ctx ).Create (& i .VerifiableAddresses [k ]); err != nil {
199
196
return err
200
197
}
201
198
}
202
199
return nil
203
200
}
204
201
202
+ func updateAssociation [T interface {
203
+ Hash () string
204
+ }](ctx context.Context , p * Persister , i * identity.Identity , inID []T ) error {
205
+ var inDB []T
206
+ if err := p .GetConnection (ctx ).
207
+ Where ("identity_id = ? AND nid = ?" , i .ID , p .NetworkID (ctx )).
208
+ Order ("id ASC" ).
209
+ All (& inDB ); err != nil {
210
+
211
+ return sqlcon .HandleError (err )
212
+ }
213
+
214
+ newAssocs := make (map [string ]* T )
215
+ oldAssocs := make (map [string ]* T )
216
+ for i , a := range inID {
217
+ newAssocs [a .Hash ()] = & inID [i ]
218
+ }
219
+ for i , a := range inDB {
220
+ oldAssocs [a .Hash ()] = & inDB [i ]
221
+ }
222
+
223
+ // Subtle: we delete the old associations from the DB first, because else
224
+ // they could cause UNIQUE constraints to fail on insert.
225
+ for h , a := range oldAssocs {
226
+ if _ , found := newAssocs [h ]; found {
227
+ newAssocs [h ] = nil // Ignore associations that are already in the db.
228
+ } else {
229
+ if err := p .GetConnection (ctx ).Destroy (a ); err != nil {
230
+ return sqlcon .HandleError (err )
231
+ }
232
+ }
233
+ }
234
+
235
+ for _ , a := range newAssocs {
236
+ if a != nil {
237
+ if err := p .GetConnection (ctx ).Create (a ); err != nil {
238
+ return sqlcon .HandleError (err )
239
+ }
240
+ }
241
+ }
242
+
243
+ return nil
244
+ }
245
+
246
+ func (p * Persister ) normalizeAllAddressess (ctx context.Context , id * identity.Identity ) {
247
+ p .normalizeRecoveryAddresses (ctx , id )
248
+ p .normalizeVerifiableAddresses (ctx , id )
249
+ }
250
+
251
+ func (p * Persister ) normalizeVerifiableAddresses (ctx context.Context , id * identity.Identity ) {
252
+ for k := range id .VerifiableAddresses {
253
+ id .VerifiableAddresses [k ].IdentityID = id .ID
254
+ id .VerifiableAddresses [k ].NID = p .NetworkID (ctx )
255
+ id .VerifiableAddresses [k ].Value = stringToLowerTrim (id .VerifiableAddresses [k ].Value )
256
+ }
257
+ }
258
+
259
+ func (p * Persister ) normalizeRecoveryAddresses (ctx context.Context , id * identity.Identity ) {
260
+ for k := range id .RecoveryAddresses {
261
+ id .RecoveryAddresses [k ].IdentityID = id .ID
262
+ id .RecoveryAddresses [k ].NID = p .NetworkID (ctx )
263
+ id .RecoveryAddresses [k ].Value = stringToLowerTrim (id .RecoveryAddresses [k ].Value )
264
+ }
265
+ }
266
+
205
267
func (p * Persister ) createRecoveryAddresses (ctx context.Context , i * identity.Identity ) error {
206
268
ctx , span := p .r .Tracer (ctx ).Tracer ().Start (ctx , "persistence.sql.createRecoveryAddresses" )
207
269
defer span .End ()
208
270
209
271
for k := range i .RecoveryAddresses {
210
- i .RecoveryAddresses [k ].IdentityID = i .ID
211
- i .RecoveryAddresses [k ].NID = p .NetworkID (ctx )
212
- i .RecoveryAddresses [k ].Value = stringToLowerTrim (i .RecoveryAddresses [k ].Value )
213
272
if err := p .GetConnection (ctx ).Create (& i .RecoveryAddresses [k ]); err != nil {
214
273
return err
215
274
}
@@ -285,6 +344,8 @@ func (p *Persister) CreateIdentity(ctx context.Context, i *identity.Identity) er
285
344
return sqlcon .HandleError (err )
286
345
}
287
346
347
+ p .normalizeAllAddressess (ctx , i )
348
+
288
349
if err := p .createVerifiableAddresses (ctx , i ); err != nil {
289
350
return sqlcon .HandleError (err )
290
351
}
@@ -350,27 +411,25 @@ func (p *Persister) UpdateIdentity(ctx context.Context, i *identity.Identity) er
350
411
return sql .ErrNoRows
351
412
}
352
413
353
- for _ , tn := range []string {
354
- new (identity.Credentials ).TableName (ctx ),
355
- new (identity.VerifiableAddress ).TableName (ctx ),
356
- new (identity.RecoveryAddress ).TableName (ctx ),
357
- } {
358
- /* #nosec G201 TableName is static */
359
- if err := tx .RawQuery (fmt .Sprintf (
360
- `DELETE FROM %s WHERE identity_id = ? AND nid = ?` , tn ), i .ID , p .NetworkID (ctx )).Exec (); err != nil {
361
- return err
362
- }
414
+ p .normalizeAllAddressess (ctx , i )
415
+ if err := updateAssociation (ctx , p , i , i .RecoveryAddresses ); err != nil {
416
+ return err
363
417
}
364
-
365
- if err := p .update (WithTransaction (ctx , tx ), i ); err != nil {
418
+ if err := updateAssociation (ctx , p , i , i .VerifiableAddresses ); err != nil {
366
419
return err
367
420
}
368
421
369
- if err := p .createVerifiableAddresses (ctx , i ); err != nil {
370
- return err
422
+ /* #nosec G201 TableName is static */
423
+ if err := tx .RawQuery (
424
+ fmt .Sprintf (
425
+ `DELETE FROM %s WHERE identity_id = ? AND nid = ?` ,
426
+ new (identity.Credentials ).TableName (ctx )),
427
+ i .ID , p .NetworkID (ctx )).Exec (); err != nil {
428
+
429
+ return sqlcon .HandleError (err )
371
430
}
372
431
373
- if err := p .createRecoveryAddresses ( ctx , i ); err != nil {
432
+ if err := p .update ( WithTransaction ( ctx , tx ) , i ); err != nil {
374
433
return err
375
434
}
376
435
0 commit comments