-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_apikeys.go
95 lines (79 loc) · 2.26 KB
/
migrate_apikeys.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/auth"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/database/postgres"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/datastore"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/util"
)
func (m *Migrator) RunAPIKeyMigration() error {
apiKeyRepo := postgres.NewAPIKeyRepo(m)
// migrate project api keys
for _, p := range m.projects {
err := m.loadAPIKeys(apiKeyRepo, p.UID, "", defaultPageable)
if err != nil {
return err
}
}
// migrate user api keys
err := m.loadAPIKeys(apiKeyRepo, "", m.user.UID, defaultPageable)
if err != nil {
return err
}
return nil
}
const (
saveAPIKeys = `
INSERT INTO convoy.api_keys (id,name,key_type,mask_id,role_type,role_project,role_endpoint,hash,salt,user_id,expires_at,created_at,updated_at, deleted_at)
VALUES (
:id, :name, :key_type, :mask_id, :role_type, :role_project,
:role_endpoint, :hash, :salt, :user_id, :expires_at,
:created_at, :updated_at, :deleted_at
)
`
)
func (m *Migrator) SaveAPIKeys(ctx context.Context, keys []datastore.APIKey) error {
values := make([]map[string]interface{}, 0, len(keys))
for i := range keys {
key := &keys[i]
var (
userID *string
endpointID *string
projectID *string
roleType *auth.RoleType
)
if !util.IsStringEmpty(key.UserID) {
userID = &key.UserID
}
if !util.IsStringEmpty(key.Role.Endpoint) {
endpointID = &key.Role.Endpoint
}
if !util.IsStringEmpty(key.Role.Project) {
projectID = &key.Role.Project
}
if !util.IsStringEmpty(string(key.Role.Type)) {
roleType = &key.Role.Type
}
values = append(values, map[string]interface{}{
"id": key.UID,
"name": key.Name,
"key_type": key.Type,
"mask_id": key.MaskID,
"role_type": roleType,
"role_project": projectID,
"role_endpoint": endpointID,
"hash": key.Hash,
"salt": key.Salt,
"user_id": userID,
"expires_at": key.ExpiresAt,
"created_at": key.CreatedAt,
"updated_at": key.UpdatedAt,
"deleted_at": key.DeletedAt,
})
}
if len(values) > 0 {
_, err := m.newDB.NamedExecContext(ctx, saveAPIKeys, values)
return err
}
return nil
}