diff --git a/storage/ent/client/types.go b/storage/ent/client/types.go index e8e0d0bb..535ab5f8 100644 --- a/storage/ent/client/types.go +++ b/storage/ent/client/types.go @@ -185,6 +185,10 @@ func toStorageUserIdentity(u *db.UserIdentity) storage.UserIdentity { // Correctness of json structure is guaranteed on uploading panic(err) } + if s.Consents == nil { + // Ensure Consents is non-nil even if JSON was "null". + s.Consents = make(map[string][]string) + } } else { // Server code assumes this will be non-nil. s.Consents = make(map[string][]string) diff --git a/storage/ent/client/useridentity.go b/storage/ent/client/useridentity.go index a929c494..edd81d9d 100644 --- a/storage/ent/client/useridentity.go +++ b/storage/ent/client/useridentity.go @@ -10,6 +10,9 @@ import ( // CreateUserIdentity saves provided user identity into the database. func (d *Database) CreateUserIdentity(ctx context.Context, identity storage.UserIdentity) error { + if identity.Consents == nil { + identity.Consents = make(map[string]storage.Consent) + } encodedConsents, err := json.Marshal(identity.Consents) if err != nil { return fmt.Errorf("encode consents user identity: %w", err) @@ -78,6 +81,10 @@ func (d *Database) UpdateUserIdentity(ctx context.Context, userID string, connec return rollback(tx, "update user identity updating: %w", err) } + if newUserIdentity.Consents == nil { + newUserIdentity.Consents = make(map[string]storage.Consent) + } + encodedConsents, err := json.Marshal(newUserIdentity.Consents) if err != nil { return rollback(tx, "encode consents user identity: %w", err) diff --git a/storage/sql/crud.go b/storage/sql/crud.go index 9089da28..04c9be3b 100644 --- a/storage/sql/crud.go +++ b/storage/sql/crud.go @@ -911,7 +911,7 @@ func scanUserIdentity(s scanner) (u storage.UserIdentity, err error) { func (c *conn) DeleteUserIdentity(ctx context.Context, userID, connectorID string) error { result, err := c.Exec(`delete from user_identity where user_id = $1 AND connector_id = $2`, userID, connectorID) if err != nil { - return fmt.Errorf("delete user_identity: user_id = %s, connector_id = %s", userID, connectorID) + return fmt.Errorf("delete user_identity: user_id = %s, connector_id = %s: %w", userID, connectorID, err) } // For now mandate that the driver implements RowsAffected. If we ever need to support