-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_sources.go
131 lines (113 loc) · 3.95 KB
/
migrate_sources.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"database/sql"
"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) RunSourceMigration() error {
sourceRepo := postgres.NewSourceRepo(m)
for _, p := range m.projects {
err := m.loadProjectSources(sourceRepo, p.UID, defaultPageable)
if err != nil {
return err
}
}
return nil
}
const (
saveSources = `
INSERT INTO convoy.sources
(id, source_verifier_id, name,type,mask_id,provider,
is_disabled,forward_headers,project_id, pub_sub, created_at, updated_at,
custom_response_body, custom_response_content_type, idempotency_keys
)
VALUES (
:id, :source_verifier_id, :name, :type, :mask_id, :provider,
:is_disabled, :forward_headers, :project_id, :pub_sub, :created_at, :updated_at,
:custom_response_body, :custom_response_content_type, :idempotency_keys
)
`
saveSourceVerifiers = `
INSERT INTO convoy.source_verifiers (
id,type,basic_username,basic_password,
api_key_header_name,api_key_header_value,
hmac_hash,hmac_header,hmac_secret,hmac_encoding
)
VALUES (
:id, :type, :basic_username, :basic_password,
:api_key_header_name, :api_key_header_value,
:hmac_hash, :hmac_header, :hmac_secret, :hmac_encoding
)
`
)
func (m *Migrator) SaveSources(ctx context.Context, sources []datastore.Source) error {
sourceValues := make([]map[string]interface{}, 0, len(sources))
sourceVerifierValues := make([]map[string]interface{}, 0, len(sources))
for i := range sources {
source := &sources[i]
var (
sourceVerifierID *string
hmac datastore.HMac
basic datastore.BasicAuth
apiKey datastore.ApiKey
)
switch source.Verifier.Type {
case datastore.APIKeyVerifier:
apiKey = *source.Verifier.ApiKey
case datastore.BasicAuthVerifier:
basic = *source.Verifier.BasicAuth
case datastore.HMacVerifier:
hmac = *source.Verifier.HMac
}
if !util.IsStringEmpty(string(source.Verifier.Type)) {
sourceVerifierID = &source.VerifierID
sourceVerifierValues = append(sourceVerifierValues, map[string]interface{}{
"id": sourceVerifierID,
"type": source.Verifier.Type,
"basic_username": basic.UserName,
"basic_password": basic.Password,
"api_key_header_name": apiKey.HeaderName,
"api_key_header_value": apiKey.HeaderValue,
"hmac_hash": hmac.Hash,
"hmac_header": hmac.Header,
"hmac_secret": hmac.Secret,
"hmac_encoding": hmac.Encoding,
})
}
sourceValues = append(sourceValues, map[string]interface{}{
"id": source.UID,
"source_verifier_id": sourceVerifierID,
"name": source.Name,
"type": source.Type,
"mask_id": source.MaskID,
"provider": source.Provider,
"is_disabled": source.IsDisabled,
"forward_headers": source.ForwardHeaders,
"project_id": source.ProjectID,
"pub_sub": source.PubSub,
"created_at": source.CreatedAt,
"updated_at": source.UpdatedAt,
"custom_response_body": source.CustomResponse.Body,
"custom_response_content_type": source.CustomResponse.ContentType,
"idempotency_keys": source.IdempotencyKeys,
})
}
tx, err := m.newDB.BeginTxx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
defer rollbackTx(tx)
if len(sourceVerifierValues) > 0 {
_, err = tx.NamedExecContext(ctx, saveSourceVerifiers, sourceVerifierValues)
if err != nil {
return err
}
}
_, err = tx.NamedExecContext(ctx, saveSources, sourceValues)
if err != nil {
return err
}
return tx.Commit()
}