-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
orm.go
337 lines (286 loc) · 10.6 KB
/
orm.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package chains
import (
"database/sql"
"fmt"
"strings"
"time"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/services/pg"
)
type ChainsORM[I ID, CFG Config, C DBChain[I, CFG]] interface {
Chain(I, ...pg.QOpt) (C, error)
Chains(offset, limit int, qopts ...pg.QOpt) ([]C, int, error)
CreateChain(id I, config CFG, qopts ...pg.QOpt) (C, error)
UpdateChain(id I, enabled bool, config CFG, qopts ...pg.QOpt) (C, error)
DeleteChain(id I, qopts ...pg.QOpt) error
GetChainsByIDs(ids []I) (chains []C, err error)
EnabledChains(...pg.QOpt) ([]C, error)
}
type NodesORM[I ID, N Node] interface {
CreateNode(N, ...pg.QOpt) (N, error)
DeleteNode(int32, ...pg.QOpt) error
GetNodesByChainIDs(chainIDs []I, qopts ...pg.QOpt) (nodes []N, err error)
NodeNamed(string, ...pg.QOpt) (N, error)
Nodes(offset, limit int, qopts ...pg.QOpt) (nodes []N, count int, err error)
NodesForChain(chainID I, offset, limit int, qopts ...pg.QOpt) (nodes []N, count int, err error)
}
// ORM manages chains and nodes.
type ORM[I ID, C Config, N Node] interface {
ChainsORM[I, C, DBChain[I, C]]
NodesORM[I, N]
StoreString(chainID I, key, val string) error
Clear(chainID I, key string) error
// SetupNodes is a shim to help with configuring multiple nodes via ENV.
// All existing nodes are dropped, and any missing chains are automatically created.
// Then all nodes are inserted, and conflicts are ignored.
SetupNodes(nodes []N, chainIDs []I) error
EnsureChains([]I, ...pg.QOpt) error
}
type orm[I ID, C Config, N Node] struct {
*chainsORM[I, C]
*nodesORM[I, N]
}
// NewORM returns an ORM backed by q, for the tables <prefix>_chains and <prefix>_nodes with column <prefix>_chain_id.
// Additional Node fields should be included in nodeCols.
// https://app.shortcut.com/chainlinklabs/story/33622/remove-legacy-config
func NewORM[I ID, C Config, N Node](q pg.Q, prefix string, nodeCols ...string) ORM[I, C, N] {
return orm[I, C, N]{
newChainsORM[I, C](q, prefix),
newNodesORM[I, N](q, prefix, nodeCols...),
}
}
// https://app.shortcut.com/chainlinklabs/story/33622/remove-legacy-config
func (o orm[I, C, N]) SetupNodes(nodes []N, ids []I) error {
return o.chainsORM.q.Transaction(func(q pg.Queryer) error {
tx := pg.WithQueryer(q)
if err := o.truncateNodes(tx); err != nil {
return err
}
if err := o.EnsureChains(ids, tx); err != nil {
return err
}
return o.ensureNodes(nodes, tx)
})
}
// DBChain is a generic DB chain for an ID and Config.
//
// A DBChain type alias can be used for convenience:
//
// type DBChain = chains.DBChain[string, pkg.ChainCfg]
type DBChain[I ID, C Config] struct {
ID I
Cfg C
CreatedAt time.Time
UpdatedAt time.Time
Enabled bool
}
// chainsORM is a generic ORM for chains.
type chainsORM[I ID, C Config] struct {
q pg.Q
prefix string
}
// newChainsORM returns an chainsORM backed by q, for the table <prefix>_chains.
func newChainsORM[I ID, C Config](q pg.Q, prefix string) *chainsORM[I, C] {
return &chainsORM[I, C]{q: q, prefix: prefix}
}
func (o *chainsORM[I, C]) Chain(id I, qopts ...pg.QOpt) (dbchain DBChain[I, C], err error) {
q := o.q.WithOpts(qopts...)
chainSQL := fmt.Sprintf(`SELECT * FROM %s_chains WHERE id = $1;`, o.prefix)
err = q.Get(&dbchain, chainSQL, id)
return
}
func (o *chainsORM[I, C]) GetChainsByIDs(ids []I) (chains []DBChain[I, C], err error) {
sql := fmt.Sprintf(`SELECT * FROM %s_chains WHERE id = ANY($1) ORDER BY created_at, id;`, o.prefix)
chainIDs := pq.Array(ids)
if err = o.q.Select(&chains, sql, chainIDs); err != nil {
return nil, err
}
return chains, nil
}
func (o *chainsORM[I, C]) CreateChain(id I, config C, qopts ...pg.QOpt) (chain DBChain[I, C], err error) {
q := o.q.WithOpts(qopts...)
sql := fmt.Sprintf(`INSERT INTO %s_chains (id, cfg, created_at, updated_at) VALUES ($1, $2, now(), now()) RETURNING *`, o.prefix)
err = q.Get(&chain, sql, id, config)
return
}
func (o *chainsORM[I, C]) EnsureChains(ids []I, qopts ...pg.QOpt) (err error) {
named := make([]struct{ ID I }, len(ids))
for i, id := range ids {
named[i].ID = id
}
q := o.q.WithOpts(qopts...)
sql := fmt.Sprintf("INSERT INTO %s_chains (id, created_at, updated_at) VALUES (:id, NOW(), NOW()) ON CONFLICT DO NOTHING;", o.prefix)
if _, err := q.NamedExec(sql, named); err != nil {
return errors.Wrapf(err, "failed to insert chains %v", ids)
}
return nil
}
func (o *chainsORM[I, C]) UpdateChain(id I, enabled bool, config C, qopts ...pg.QOpt) (chain DBChain[I, C], err error) {
q := o.q.WithOpts(qopts...)
sql := fmt.Sprintf(`UPDATE %s_chains SET enabled = $1, cfg = $2, updated_at = now() WHERE id = $3 RETURNING *`, o.prefix)
err = q.Get(&chain, sql, enabled, config, id)
return
}
// StoreString saves a string value into the config for the given chain and key
func (o *chainsORM[I, C]) StoreString(chainID I, name, val string) error {
s := fmt.Sprintf(`UPDATE %s_chains SET cfg = cfg || jsonb_build_object($1::text, $2::text) WHERE id = $3`, o.prefix)
res, err := o.q.Exec(s, name, val, chainID)
if err != nil {
return errors.Wrapf(err, "failed to store chain config for chain ID %v", chainID)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return errors.Wrapf(sql.ErrNoRows, "no chain found with ID %v", chainID)
}
return nil
}
// Clear deletes a config value for the given chain and key
func (o *chainsORM[I, C]) Clear(chainID I, name string) error {
s := fmt.Sprintf(`UPDATE %s_chains SET cfg = cfg - $1 WHERE id = $2`, o.prefix)
res, err := o.q.Exec(s, name, chainID)
if err != nil {
return errors.Wrapf(err, "failed to clear chain config for chain ID %v", chainID)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return errors.Wrapf(sql.ErrNoRows, "no chain found with ID %v", chainID)
}
return nil
}
func (o *chainsORM[I, C]) DeleteChain(id I, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
query := fmt.Sprintf(`DELETE FROM %s_chains WHERE id = $1`, o.prefix)
result, err := q.Exec(query, id)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return sql.ErrNoRows
}
return nil
}
func (o *chainsORM[I, C]) Chains(offset, limit int, qopts ...pg.QOpt) (chains []DBChain[I, C], count int, err error) {
err = o.q.WithOpts(qopts...).Transaction(func(q pg.Queryer) error {
if err = q.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM %s_chains", o.prefix)); err != nil {
return errors.Wrap(err, "failed to fetch chains count")
}
sql := fmt.Sprintf(`SELECT * FROM %s_chains ORDER BY created_at, id LIMIT $1 OFFSET $2;`, o.prefix)
err = q.Select(&chains, sql, pg.Limit(limit), offset)
return errors.Wrap(err, "failed to fetch chains")
}, pg.OptReadOnlyTx())
return
}
func (o *chainsORM[I, C]) EnabledChains(qopts ...pg.QOpt) (chains []DBChain[I, C], err error) {
q := o.q.WithOpts(qopts...)
chainsSQL := fmt.Sprintf(`SELECT * FROM %s_chains WHERE enabled ORDER BY created_at, id;`, o.prefix)
if err = q.Select(&chains, chainsSQL); err != nil {
return
}
return
}
// nodesORM is a generic ORM for nodes.
type nodesORM[I ID, N Node] struct {
q pg.Q
prefix string
createNodeQ string
ensureNodeQ string
}
func newNodesORM[I ID, N Node](q pg.Q, prefix string, nodeCols ...string) *nodesORM[I, N] {
// pre-compute query for CreateNode
var withColon []string
for _, c := range nodeCols {
withColon = append(withColon, ":"+c)
}
query := fmt.Sprintf(`INSERT INTO %s_nodes (name, %s_chain_id, %s, created_at, updated_at)
VALUES (:name, :%s_chain_id, %s, now(), now())`,
prefix, prefix, strings.Join(nodeCols, ", "), prefix, strings.Join(withColon, ", "))
return &nodesORM[I, N]{q: q, prefix: prefix,
createNodeQ: query + ` RETURNING *;`,
ensureNodeQ: query + ` ON CONFLICT DO NOTHING;`,
}
}
func (o *nodesORM[I, N]) ensureNodes(nodes []N, qopts ...pg.QOpt) (err error) {
q := o.q.WithOpts(qopts...)
_, err = q.NamedExec(o.ensureNodeQ, nodes)
err = errors.Wrap(err, "failed to insert nodes")
return
}
func (o *nodesORM[I, N]) CreateNode(data N, qopts ...pg.QOpt) (node N, err error) {
q := o.q.WithOpts(qopts...)
stmt, err := q.PrepareNamed(o.createNodeQ)
if err != nil {
return node, err
}
err = stmt.Get(&node, data)
return node, err
}
func (o *nodesORM[I, N]) DeleteNode(id int32, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
query := fmt.Sprintf(`DELETE FROM %s_nodes WHERE id = $1`, o.prefix)
result, err := q.Exec(query, id)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return sql.ErrNoRows
}
return nil
}
func (o *nodesORM[I, N]) truncateNodes(qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
_, err := q.Exec(fmt.Sprintf(`TRUNCATE %s_nodes;`, o.prefix))
if err != nil {
return errors.Wrapf(err, "failed to truncate %s_nodes table", o.prefix)
}
return nil
}
func (o *nodesORM[I, N]) NodeNamed(name string, qopts ...pg.QOpt) (node N, err error) {
q := o.q.WithOpts(qopts...)
err = q.Get(&node, fmt.Sprintf("SELECT * FROM %s_nodes WHERE name = $1;", o.prefix), name)
return
}
func (o *nodesORM[I, N]) Nodes(offset, limit int, qopts ...pg.QOpt) (nodes []N, count int, err error) {
err = o.q.WithOpts(qopts...).Transaction(func(q pg.Queryer) error {
if err = q.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM %s_nodes", o.prefix)); err != nil {
return errors.Wrap(err, "failed to fetch nodes count")
}
sql := fmt.Sprintf(`SELECT * FROM %s_nodes ORDER BY created_at, id LIMIT $1 OFFSET $2;`, o.prefix)
err = q.Select(&nodes, sql, pg.Limit(limit), offset)
return errors.Wrap(err, "failed to fetch nodes")
}, pg.OptReadOnlyTx())
return
}
func (o *nodesORM[I, N]) NodesForChain(chainID I, offset, limit int, qopts ...pg.QOpt) (nodes []N, count int, err error) {
err = o.q.WithOpts(qopts...).Transaction(func(q pg.Queryer) error {
if err = q.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM %s_nodes WHERE %s_chain_id = $1", o.prefix, o.prefix), chainID); err != nil {
return errors.Wrap(err, "failed to fetch nodes count")
}
sql := fmt.Sprintf(`SELECT * FROM %s_nodes WHERE %s_chain_id = $1 ORDER BY created_at, id LIMIT $2 OFFSET $3;`, o.prefix, o.prefix)
err = q.Select(&nodes, sql, chainID, pg.Limit(limit), offset)
return errors.Wrap(err, "failed to fetch nodes")
}, pg.OptReadOnlyTx())
return
}
func (o *nodesORM[I, N]) GetNodesByChainIDs(chainIDs []I, qopts ...pg.QOpt) (nodes []N, err error) {
sql := fmt.Sprintf(`SELECT * FROM %s_nodes WHERE %s_chain_id = ANY($1) ORDER BY created_at, id;`, o.prefix, o.prefix)
cids := pq.Array(chainIDs)
if err = o.q.WithOpts(qopts...).Select(&nodes, sql, cids); err != nil {
return nil, err
}
return nodes, nil
}