-
Notifications
You must be signed in to change notification settings - Fork 10
/
destination.go
360 lines (312 loc) · 10.2 KB
/
destination.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright © 2022 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package postgres
import (
"context"
"encoding/json"
"fmt"
"strings"
sq "github.com/Masterminds/squirrel"
"github.com/conduitio/conduit-commons/config"
"github.com/conduitio/conduit-commons/opencdc"
"github.com/conduitio/conduit-connector-postgres/destination"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/jackc/pgx/v5"
)
type Destination struct {
sdk.UnimplementedDestination
config destination.Config
getTableName destination.TableFn
conn *pgx.Conn
stmtBuilder sq.StatementBuilderType
}
func NewDestination() sdk.Destination {
d := &Destination{
stmtBuilder: sq.StatementBuilder.PlaceholderFormat(sq.Dollar),
}
return sdk.DestinationWithMiddleware(d, sdk.DefaultDestinationMiddleware()...)
}
func (d *Destination) Parameters() config.Parameters {
return d.config.Parameters()
}
func (d *Destination) Configure(ctx context.Context, cfg config.Config) error {
err := sdk.Util.ParseConfig(ctx, cfg, &d.config, NewDestination().Parameters())
if err != nil {
return err
}
// try parsing the url
_, err = pgx.ParseConfig(d.config.URL)
if err != nil {
return fmt.Errorf("invalid url: %w", err)
}
d.getTableName, err = d.config.TableFunction()
if err != nil {
return fmt.Errorf("invalid table name or table function: %w", err)
}
return nil
}
func (d *Destination) Open(ctx context.Context) error {
conn, err := pgx.Connect(ctx, d.config.URL)
if err != nil {
return fmt.Errorf("failed to open connection: %w", err)
}
d.conn = conn
return nil
}
// Write routes incoming records to their appropriate handler based on the
// operation.
func (d *Destination) Write(ctx context.Context, recs []opencdc.Record) (int, error) {
b := &pgx.Batch{}
for _, rec := range recs {
var err error
switch rec.Operation {
case opencdc.OperationCreate:
err = d.handleInsert(ctx, rec, b)
case opencdc.OperationUpdate:
err = d.handleUpdate(ctx, rec, b)
case opencdc.OperationDelete:
err = d.handleDelete(ctx, rec, b)
case opencdc.OperationSnapshot:
err = d.handleInsert(ctx, rec, b)
default:
return 0, fmt.Errorf("invalid operation %q", rec.Operation)
}
if err != nil {
return 0, err
}
}
br := d.conn.SendBatch(ctx, b)
defer br.Close()
for i := range recs {
// fetch error for each statement
_, err := br.Exec()
if err != nil {
// the batch is executed in a transaction, if one failed all failed
return 0, fmt.Errorf("failed to execute query for record %d: %w", i, err)
}
}
return len(recs), nil
}
func (d *Destination) Teardown(ctx context.Context) error {
if d.conn != nil {
return d.conn.Close(ctx)
}
return nil
}
// handleInsert adds a query to the batch that stores the record in the target
// table. It checks for the existence of a key. If no key is present or a key
// exists and no key column name is configured, it will plainly insert the data.
// Otherwise it upserts the record.
func (d *Destination) handleInsert(ctx context.Context, r opencdc.Record, b *pgx.Batch) error {
if !d.hasKey(r) || d.config.Key == "" {
return d.insert(ctx, r, b)
}
return d.upsert(ctx, r, b)
}
// handleUpdate adds a query to the batch that updates the record in the target
// table. It assumes the record has a key and fails if one is not present.
func (d *Destination) handleUpdate(ctx context.Context, r opencdc.Record, b *pgx.Batch) error {
if !d.hasKey(r) {
return fmt.Errorf("key must be provided on update actions")
}
// TODO handle case if the key was updated
return d.upsert(ctx, r, b)
}
// handleDelete adds a query to the batch that deletes the record from the
// target table. It assumes the record has a key and fails if one is not present.
func (d *Destination) handleDelete(ctx context.Context, r opencdc.Record, b *pgx.Batch) error {
if !d.hasKey(r) {
return fmt.Errorf("key must be provided on delete actions")
}
return d.remove(ctx, r, b)
}
func (d *Destination) upsert(ctx context.Context, r opencdc.Record, b *pgx.Batch) error {
payload, err := d.getPayload(r)
if err != nil {
return fmt.Errorf("failed to get payload: %w", err)
}
key, err := d.getKey(r)
if err != nil {
return fmt.Errorf("failed to get key: %w", err)
}
keyColumnName := d.getKeyColumnName(key, d.config.Key)
tableName, err := d.getTableName(r)
if err != nil {
return fmt.Errorf("failed to get table name for write: %w", err)
}
query, args, err := d.formatUpsertQuery(key, payload, keyColumnName, tableName)
if err != nil {
return fmt.Errorf("error formatting query: %w", err)
}
sdk.Logger(ctx).Trace().
Str("table_name", tableName).
Any("key", map[string]interface{}{keyColumnName: key[keyColumnName]}).
Msg("upserting record")
b.Queue(query, args...)
return nil
}
func (d *Destination) remove(ctx context.Context, r opencdc.Record, b *pgx.Batch) error {
key, err := d.getKey(r)
if err != nil {
return err
}
keyColumnName := d.getKeyColumnName(key, d.config.Key)
tableName, err := d.getTableName(r)
if err != nil {
return fmt.Errorf("failed to get table name for write: %w", err)
}
sdk.Logger(ctx).Trace().
Str("table_name", tableName).
Any("key", map[string]interface{}{keyColumnName: key[keyColumnName]}).
Msg("deleting record")
query, args, err := d.stmtBuilder.
Delete(tableName).
Where(sq.Eq{keyColumnName: key[keyColumnName]}).
ToSql()
if err != nil {
return fmt.Errorf("error formatting delete query: %w", err)
}
b.Queue(query, args...)
return nil
}
// insert is an append-only operation that doesn't care about keys, but
// can error on constraints violations so should only be used when no table
// key or unique constraints are otherwise present.
func (d *Destination) insert(ctx context.Context, r opencdc.Record, b *pgx.Batch) error {
tableName, err := d.getTableName(r)
if err != nil {
return err
}
key, err := d.getKey(r)
if err != nil {
return err
}
payload, err := d.getPayload(r)
if err != nil {
return err
}
colArgs, valArgs := d.formatColumnsAndValues(key, payload)
sdk.Logger(ctx).Trace().
Str("table_name", tableName).
Msg("inserting record")
query, args, err := d.stmtBuilder.
Insert(tableName).
Columns(colArgs...).
Values(valArgs...).
ToSql()
if err != nil {
return fmt.Errorf("error formatting insert query: %w", err)
}
b.Queue(query, args...)
return nil
}
func (d *Destination) getPayload(r opencdc.Record) (opencdc.StructuredData, error) {
if r.Payload.After == nil {
return opencdc.StructuredData{}, nil
}
return d.structuredDataFormatter(r.Payload.After)
}
func (d *Destination) getKey(r opencdc.Record) (opencdc.StructuredData, error) {
if r.Key == nil {
return opencdc.StructuredData{}, nil
}
return d.structuredDataFormatter(r.Key)
}
func (d *Destination) structuredDataFormatter(data opencdc.Data) (opencdc.StructuredData, error) {
if data == nil {
return opencdc.StructuredData{}, nil
}
if sdata, ok := data.(opencdc.StructuredData); ok {
return sdata, nil
}
raw := data.Bytes()
if len(raw) == 0 {
return opencdc.StructuredData{}, nil
}
m := make(map[string]interface{})
err := json.Unmarshal(raw, &m)
if err != nil {
return nil, err
}
return m, nil
}
// formatUpsertQuery manually formats the UPSERT and ON CONFLICT query statements.
// The `ON CONFLICT` portion of this query needs to specify the constraint
// name.
// * In our case, we can only rely on the record.Key's parsed key value.
// * If other schema constraints prevent a write, this won't upsert on
// that conflict.
func (d *Destination) formatUpsertQuery(
key opencdc.StructuredData,
payload opencdc.StructuredData,
keyColumnName string,
tableName string,
) (string, []interface{}, error) {
upsertQuery := fmt.Sprintf("ON CONFLICT (%s) DO UPDATE SET", keyColumnName)
for column := range payload {
// tuples form a comma separated list, so they need a comma at the end.
// `EXCLUDED` references the new record's values. This will overwrite
// every column's value except for the key column.
tuple := fmt.Sprintf("%s=EXCLUDED.%s,", column, column)
// TODO: Consider removing this space.
upsertQuery += " "
// add the tuple to the query string
upsertQuery += tuple
}
// remove the last comma from the list of tuples
upsertQuery = strings.TrimSuffix(upsertQuery, ",")
// we have to manually append a semi colon to the upsert sql;
upsertQuery += ";"
colArgs, valArgs := d.formatColumnsAndValues(key, payload)
return d.stmtBuilder.
Insert(tableName).
Columns(colArgs...).
Values(valArgs...).
SuffixExpr(sq.Expr(upsertQuery)).
ToSql()
}
// formatColumnsAndValues turns the key and payload into a slice of ordered
// columns and values for upserting into Postgres.
func (d *Destination) formatColumnsAndValues(key, payload opencdc.StructuredData) ([]string, []interface{}) {
var colArgs []string
var valArgs []interface{}
// range over both the key and payload values in order to format the
// query for args and values in proper order
for key, val := range key {
colArgs = append(colArgs, key)
valArgs = append(valArgs, val)
delete(payload, key) // NB: Delete Key from payload arguments
}
for field, value := range payload {
colArgs = append(colArgs, field)
valArgs = append(valArgs, value)
}
return colArgs, valArgs
}
// getKeyColumnName will return the name of the first item in the key or the
// connector-configured default name of the key column name.
func (d *Destination) getKeyColumnName(key opencdc.StructuredData, defaultKeyName string) string {
if len(key) > 1 {
// Go maps aren't order preserving, so anything over len 1 will have
// non deterministic results until we handle composite keys.
panic("composite keys not yet supported")
}
for k := range key {
return k
}
return defaultKeyName
}
func (d *Destination) hasKey(e opencdc.Record) bool {
return e.Key != nil && len(e.Key.Bytes()) > 0
}