forked from stellar/go
/
db_store_update.go
62 lines (55 loc) · 1.53 KB
/
db_store_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package account
import (
"database/sql"
"github.com/lib/pq"
)
func (s *DBStore) Update(a Account) error {
tx, err := s.DB.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
var accountID int64
// Delete an identity will delete the associated auth methods because of the ON DELETE CASCADE reference.
// https://github.com/aiblocks/go/blob/b3e0a353a901ce0babad5b4953330e55f2c674a1/exp/services/recoverysigner/internal/db/dbmigrate/migrations/20200311000002-create-auth-methods.sql#L11
err = tx.Get(&accountID, `
WITH deleted_identities AS (
DELETE FROM identities
USING accounts
WHERE identities.account_id = accounts.id AND accounts.address = $1
RETURNING identities.account_id AS account_id
)
SELECT DISTINCT account_id FROM deleted_identities
`, a.Address)
if err == sql.ErrNoRows {
return ErrNotFound
}
if err != nil {
return err
}
for _, i := range a.Identities {
var authTypes, authValues pq.StringArray
for _, m := range i.AuthMethods {
authTypes = append(authTypes, string(m.Type))
authValues = append(authValues, m.Value)
}
_, err = tx.Exec(`
WITH new_identity AS (
INSERT INTO identities (account_id, role)
VALUES ($1, $2)
RETURNING account_id, id
)
INSERT INTO auth_methods (account_id, identity_id, type_, value)
SELECT account_id, id, unnest($3::auth_method_type[]), unnest($4::text[])
FROM new_identity
`, accountID, i.Role, authTypes, authValues)
if err != nil {
return err
}
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}