/
postgres.go
1206 lines (1040 loc) · 38.7 KB
/
postgres.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package adapters
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jitsucom/jitsu/server/drivers/base"
"github.com/jitsucom/jitsu/server/errorj"
"github.com/jitsucom/jitsu/server/timestamp"
"github.com/jitsucom/jitsu/server/utils"
"github.com/jitsucom/jitsu/server/uuid"
"github.com/lib/pq"
"math"
"sort"
"strconv"
"strings"
"time"
"github.com/jitsucom/jitsu/server/logging"
"github.com/jitsucom/jitsu/server/typing"
_ "github.com/lib/pq"
)
const (
tableSchemaQuery = `SELECT
pg_attribute.attname AS name,
pg_catalog.format_type(pg_attribute.atttypid,pg_attribute.atttypmod) AS column_type
FROM pg_attribute
JOIN pg_class ON pg_class.oid = pg_attribute.attrelid
LEFT JOIN pg_attrdef pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum
LEFT JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
LEFT JOIN pg_constraint ON pg_constraint.conrelid = pg_class.oid AND pg_attribute.attnum = ANY (pg_constraint.conkey)
WHERE pg_class.relkind = 'r'::char
AND pg_namespace.nspname = $1
AND pg_class.relname = $2
AND pg_attribute.attnum > 0`
primaryKeyFieldsQuery = `SELECT tco.constraint_name as constraint_name,
kcu.column_name as key_column
FROM information_schema.table_constraints tco
JOIN information_schema.key_column_usage kcu
ON kcu.constraint_name = tco.constraint_name
AND kcu.constraint_schema = tco.constraint_schema
AND kcu.constraint_name = tco.constraint_name
WHERE tco.constraint_type = 'PRIMARY KEY' AND
kcu.table_schema = $1 AND
kcu.table_name = $2`
createDbSchemaIfNotExistsTemplate = `CREATE SCHEMA IF NOT EXISTS "%s"`
addColumnTemplate = `ALTER TABLE "%s"."%s" ADD COLUMN %s`
dropPrimaryKeyTemplate = `ALTER TABLE "%s"."%s" DROP CONSTRAINT %s`
alterPrimaryKeyTemplate = `ALTER TABLE "%s"."%s" ADD CONSTRAINT %s PRIMARY KEY (%s)`
createTableTemplate = `CREATE TABLE "%s"."%s" (%s)`
insertTemplate = `INSERT INTO "%s"."%s" (%s) VALUES %s`
mergeTemplate = `INSERT INTO "%s"."%s"(%s) VALUES %s ON CONFLICT ON CONSTRAINT %s DO UPDATE set %s;`
bulkMergeTemplate = `INSERT INTO "%s"."%s"(%s) SELECT %s FROM "%s"."%s" ON CONFLICT ON CONSTRAINT %s DO UPDATE SET %s`
bulkMergePrefix = `excluded`
deleteQueryTemplate = `DELETE FROM "%s"."%s" WHERE %s`
updateStatement = `UPDATE "%s"."%s" SET %s WHERE %s=$%d`
dropTableTemplate = `DROP TABLE %s"%s"."%s"`
copyColumnTemplate = `UPDATE "%s"."%s" SET %s = %s`
dropColumnTemplate = `ALTER TABLE "%s"."%s" DROP COLUMN %s`
renameColumnTemplate = `ALTER TABLE "%s"."%s" RENAME COLUMN %s TO %s`
renameTableTemplate = `ALTER TABLE "%s"."%s" RENAME TO "%s"`
postgresTruncateTableTemplate = `TRUNCATE "%s"."%s"`
PostgresValuesLimit = 65535 // this is a limitation of parameters one can pass as query values. If more parameters are passed, error is returned
)
var (
SchemaToPostgres = map[typing.DataType]string{
typing.STRING: "text",
typing.INT64: "bigint",
typing.FLOAT64: "double precision",
typing.TIMESTAMP: "timestamp",
typing.BOOL: "boolean",
typing.UNKNOWN: "text",
}
)
type ErrorPayload struct {
Dataset string
Bucket string
Project string
Database string
Cluster string
Schema string
Table string
Partition string
PrimaryKeys []string
Statement string
Values []interface{}
ValuesMapString string
TotalObjects int
}
func (ep *ErrorPayload) String() string {
var msgParts []string
if ep.Dataset != "" {
msgParts = append(msgParts, fmt.Sprintf("dataset: %s", ep.Dataset))
}
if ep.Bucket != "" {
msgParts = append(msgParts, fmt.Sprintf("bucket: %s", ep.Bucket))
}
if ep.Project != "" {
msgParts = append(msgParts, fmt.Sprintf("project: %s", ep.Project))
}
if ep.Database != "" {
msgParts = append(msgParts, fmt.Sprintf("database: %s", ep.Database))
}
if ep.Cluster != "" {
msgParts = append(msgParts, fmt.Sprintf("cluster: %s", ep.Cluster))
}
if ep.Schema != "" {
msgParts = append(msgParts, fmt.Sprintf("schema: %s", ep.Schema))
}
if ep.Table != "" {
msgParts = append(msgParts, fmt.Sprintf("table: %s", ep.Table))
}
if ep.Partition != "" {
msgParts = append(msgParts, fmt.Sprintf("partition: %s", ep.Partition))
}
if len(ep.PrimaryKeys) > 0 {
msgParts = append(msgParts, fmt.Sprintf("primary keys: %v", ep.PrimaryKeys))
}
if ep.Statement != "" {
msgParts = append(msgParts, fmt.Sprintf("statement: %s", utils.ShortenStringWithEllipsis(ep.Statement, 1000)))
}
if len(ep.Values) > 0 {
msgParts = append(msgParts, fmt.Sprintf("values: %s", utils.ShortenStringWithEllipsis(fmt.Sprint(ep.Values), 1000)))
}
if ep.TotalObjects > 1 {
msgParts = append(msgParts, fmt.Sprintf("objects count: %d", ep.TotalObjects))
}
if ep.ValuesMapString != "" {
msgParts = append(msgParts, fmt.Sprintf("values of 1st object: %s", utils.ShortenStringWithEllipsis(ep.ValuesMapString, 1000)))
}
if len(msgParts) > 0 {
return "\n" + strings.Join(msgParts, "\n") + "\n"
} else {
return ""
}
}
func ObjectValuesToString(header []string, valueArgs []interface{}) string {
var firstObjectValues strings.Builder
firstObjectValues.WriteString("{")
for i, name := range header {
if i != 0 {
firstObjectValues.WriteString(", ")
}
firstObjectValues.WriteString(name + ": " + fmt.Sprint(valueArgs[i]))
}
firstObjectValues.WriteString("}")
return firstObjectValues.String()
}
// DataSourceConfig dto for deserialized datasource config (e.g. in Postgres or AwsRedshift destination)
type DataSourceConfig struct {
Host string `mapstructure:"host,omitempty" json:"host,omitempty" yaml:"host,omitempty"`
Port int `mapstructure:"port,omitempty" json:"port,omitempty" yaml:"port,omitempty"`
Db string `mapstructure:"db,omitempty" json:"db,omitempty" yaml:"db,omitempty"`
Schema string `mapstructure:"schema,omitempty" json:"schema,omitempty" yaml:"schema,omitempty"`
Username string `mapstructure:"username,omitempty" json:"username,omitempty" yaml:"username,omitempty"`
Password string `mapstructure:"password,omitempty" json:"password,omitempty" yaml:"password,omitempty"`
Parameters map[string]string `mapstructure:"parameters,omitempty" json:"parameters,omitempty" yaml:"parameters,omitempty"`
SSLConfiguration *SSLConfig `mapstructure:"ssl,omitempty" json:"ssl,omitempty" yaml:"ssl,omitempty"`
S3 *S3Config `mapstructure:"s3,omitempty" json:"s3,omitempty" yaml:"s3,omitempty"`
}
// Validate required fields in DataSourceConfig
func (dsc *DataSourceConfig) Validate() error {
if dsc == nil {
return errors.New("Datasource config is required")
}
if dsc.Host == "" {
return errors.New("Datasource host is required parameter")
}
if dsc.Db == "" {
return errors.New("Datasource db is required parameter")
}
if dsc.Username == "" {
return errors.New("Datasource username is required parameter")
}
if dsc.Parameters == nil {
dsc.Parameters = map[string]string{}
}
if dsc.SSLConfiguration != nil {
if err := dsc.SSLConfiguration.Validate(); err != nil {
return err
}
}
return nil
}
// Postgres is adapter for creating,patching (schema or table), inserting data to postgres
type Postgres struct {
ctx context.Context
config *DataSourceConfig
dataSource *sql.DB
queryLogger *logging.QueryLogger
sqlTypes typing.SQLTypes
}
// NewPostgresUnderRedshift returns configured Postgres adapter instance without mapping old types
func NewPostgresUnderRedshift(ctx context.Context, config *DataSourceConfig, queryLogger *logging.QueryLogger, sqlTypes typing.SQLTypes) (*Postgres, error) {
connectionString := fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s ",
config.Host, config.Port, config.Db, config.Username, config.Password)
//concat provided connection parameters
for k, v := range config.Parameters {
connectionString += k + "=" + v + " "
}
dataSource, err := sql.Open("postgres", connectionString)
if err != nil {
return nil, err
}
if err := dataSource.Ping(); err != nil {
dataSource.Close()
return nil, err
}
//set default value
dataSource.SetConnMaxLifetime(10 * time.Minute)
return &Postgres{ctx: ctx, config: config, dataSource: dataSource, queryLogger: queryLogger, sqlTypes: sqlTypes}, nil
}
// NewPostgres return configured Postgres adapter instance
func NewPostgres(ctx context.Context, config *DataSourceConfig, queryLogger *logging.QueryLogger, sqlTypes typing.SQLTypes) (*Postgres, error) {
connectionString := fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s ",
config.Host, config.Port, config.Db, config.Username, config.Password)
//concat provided connection parameters
for k, v := range config.Parameters {
connectionString += k + "=" + v + " "
}
dataSource, err := sql.Open("postgres", connectionString)
if err != nil {
return nil, err
}
if err := dataSource.Ping(); err != nil {
dataSource.Close()
return nil, err
}
//set default value
dataSource.SetConnMaxLifetime(10 * time.Minute)
return &Postgres{ctx: ctx, config: config, dataSource: dataSource, queryLogger: queryLogger, sqlTypes: reformatMappings(sqlTypes, SchemaToPostgres)}, nil
}
// Type returns Postgres type
func (Postgres) Type() string {
return "Postgres"
}
// OpenTx opens underline sql transaction and return wrapped instance
func (p *Postgres) OpenTx() (*Transaction, error) {
tx, err := p.dataSource.BeginTx(p.ctx, nil)
if err != nil {
err = checkErr(err)
return nil, errorj.BeginTransactionError.Wrap(err, "failed to begin transaction").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
})
}
return &Transaction{tx: tx, dbType: p.Type()}, nil
}
// CreateDbSchema creates database schema instance if doesn't exist
func (p *Postgres) CreateDbSchema(dbSchemaName string) error {
query := fmt.Sprintf(createDbSchemaIfNotExistsTemplate, dbSchemaName)
p.queryLogger.LogDDL(query)
if _, err := p.dataSource.ExecContext(p.ctx, query); err != nil {
err = checkErr(err)
return errorj.CreateSchemaError.Wrap(err, "failed to create db schema").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: dbSchemaName,
Statement: query,
})
}
return nil
}
// CreateTable creates database table with name,columns provided in Table representation
func (p *Postgres) CreateTable(table *Table) (err error) {
wrappedTx, err := p.OpenTx()
if err != nil {
return err
}
defer func() {
if err != nil {
rbErr := wrappedTx.Rollback()
if rbErr != nil {
err = errorj.Group(err, rbErr)
}
} else {
err = wrappedTx.Commit()
}
}()
return p.createTableInTransaction(wrappedTx, table)
}
// PatchTableSchema adds new columns(from provided Table) to existing table
func (p *Postgres) PatchTableSchema(patchTable *Table) (err error) {
wrappedTx, err := p.OpenTx()
if err != nil {
return err
}
defer func() {
if err != nil {
rbErr := wrappedTx.Rollback()
if rbErr != nil {
err = errorj.Group(err, rbErr)
}
} else {
err = wrappedTx.Commit()
}
}()
return p.patchTableSchemaInTransaction(wrappedTx, patchTable)
}
// GetTableSchema returns table (name,columns with name and types) representation wrapped in Table struct
func (p *Postgres) GetTableSchema(tableName string) (*Table, error) {
table, err := p.getTable(tableName)
if err != nil {
return nil, err
}
//don't select primary keys of non-existent table
if len(table.Columns) == 0 {
return table, nil
}
primaryKeyName, pkFields, err := p.getPrimaryKey(tableName)
if err != nil {
return nil, err
}
table.PKFields = pkFields
table.PrimaryKeyName = primaryKeyName
jitsuPrimaryKeyName := BuildConstraintName(table.Schema, table.Name)
if primaryKeyName != "" && primaryKeyName != jitsuPrimaryKeyName {
logging.Warnf("[%s] table: %s.%s has a custom primary key with name: %s that isn't managed by Jitsu. Custom primary key will be used in rows deduplication and updates. primary_key_fields configuration provided in Jitsu config will be ignored.", p.destinationId(), table.Schema, table.Name, primaryKeyName)
}
return table, nil
}
// Insert inserts data with InsertContext as a single object or a batch into Redshift
func (p *Postgres) Insert(insertContext *InsertContext) error {
if insertContext.eventContext != nil {
return p.insertSingle(insertContext.eventContext)
} else {
return p.insertBatch(insertContext.table, insertContext.objects, insertContext.deleteConditions)
}
}
func (p *Postgres) insertBatch(table *Table, objects []map[string]interface{}, deleteConditions *base.DeleteConditions) (err error) {
wrappedTx, err := p.OpenTx()
if err != nil {
return err
}
defer func() {
if err != nil {
rbErr := wrappedTx.Rollback()
if rbErr != nil {
err = errorj.Group(err, rbErr)
}
} else {
err = wrappedTx.Commit()
}
}()
if !deleteConditions.IsEmpty() {
if err = p.deleteInTransaction(wrappedTx, table, deleteConditions); err != nil {
return err
}
}
if len(table.PKFields) == 0 {
return p.bulkInsertInTransaction(wrappedTx, table, objects, PostgresValuesLimit)
}
//deduplication for bulkMerge success (it fails if there is any duplicate)
deduplicatedObjectsBuckets := deduplicateObjects(table, objects)
for _, objectsBucket := range deduplicatedObjectsBuckets {
if err = p.bulkMergeInTransaction(wrappedTx, table, objectsBucket); err != nil {
return err
}
}
return nil
}
// insertSingle inserts single provided object in postgres with typecasts
// uses upsert (merge on conflict) if primary_keys are configured
func (p *Postgres) insertSingle(eventContext *EventContext) error {
columnsWithoutQuotes, columnsWithQuotes, placeholders, values := p.buildInsertPayload(eventContext.Table, eventContext.ProcessedEvent)
var statement string
if len(eventContext.Table.PKFields) == 0 {
statement = fmt.Sprintf(insertTemplate, p.config.Schema, eventContext.Table.Name, strings.Join(columnsWithQuotes, ", "), "("+strings.Join(placeholders, ", ")+")")
} else {
statement = fmt.Sprintf(mergeTemplate, p.config.Schema, eventContext.Table.Name, strings.Join(columnsWithQuotes, ","), "("+strings.Join(placeholders, ", ")+")", eventContext.Table.PrimaryKeyName, p.buildUpdateSection(columnsWithoutQuotes))
}
p.queryLogger.LogQueryWithValues(statement, values)
if _, err := p.dataSource.ExecContext(p.ctx, statement, values...); err != nil {
err = checkErr(err)
return errorj.ExecuteInsertError.Wrap(err, "failed to execute single insert").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: eventContext.Table.Name,
PrimaryKeys: eventContext.Table.GetPKFields(),
Statement: statement,
Values: values,
})
}
return nil
}
// Truncate deletes all records in tableName table
func (p *Postgres) Truncate(tableName string) error {
sqlParams := SqlParams{
dataSource: p.dataSource,
queryLogger: p.queryLogger,
ctx: p.ctx,
}
statement := fmt.Sprintf(postgresTruncateTableTemplate, p.config.Schema, tableName)
if err := sqlParams.commonTruncate(statement); err != nil {
return errorj.TruncateError.Wrap(err, "failed to truncate table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: tableName,
Statement: statement,
})
}
return nil
}
func (p *Postgres) getTable(tableName string) (*Table, error) {
table := &Table{Schema: p.config.Schema, Name: tableName, Columns: map[string]typing.SQLColumn{}, PKFields: map[string]bool{}}
rows, err := p.dataSource.QueryContext(p.ctx, tableSchemaQuery, p.config.Schema, tableName)
if err != nil {
err = checkErr(err)
return nil, errorj.GetTableError.Wrap(err, "failed to get table columns").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: tableSchemaQuery,
Values: []interface{}{p.config.Schema, tableName},
})
}
defer rows.Close()
for rows.Next() {
var columnName, columnPostgresType string
if err := rows.Scan(&columnName, &columnPostgresType); err != nil {
return nil, errorj.GetTableError.Wrap(err, "failed to scan result").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: tableSchemaQuery,
Values: []interface{}{p.config.Schema, tableName},
})
}
if columnPostgresType == "-" {
//skip dropped postgres field
continue
}
table.Columns[columnName] = typing.SQLColumn{Type: columnPostgresType}
}
if err := rows.Err(); err != nil {
return nil, errorj.GetTableError.Wrap(err, "failed read last row").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: tableSchemaQuery,
Values: []interface{}{p.config.Schema, tableName},
})
}
return table, nil
}
// create table columns and pk key
// override input table sql type with configured cast type
// make fields from Table PkFields - 'not null'
func (p *Postgres) createTableInTransaction(wrappedTx *Transaction, table *Table) error {
var columnsDDL []string
pkFields := table.GetPKFieldsMap()
for _, columnName := range table.SortedColumnNames() {
column := table.Columns[columnName]
columnsDDL = append(columnsDDL, p.columnDDL(columnName, column, pkFields))
}
//sorting columns asc
sort.Strings(columnsDDL)
query := fmt.Sprintf(createTableTemplate, p.config.Schema, table.Name, strings.Join(columnsDDL, ", "))
p.queryLogger.LogDDL(query)
if _, err := wrappedTx.tx.ExecContext(p.ctx, query); err != nil {
err = checkErr(err)
return errorj.CreateTableError.Wrap(err, "failed to create table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: query,
})
}
if err := p.createPrimaryKeyInTransaction(wrappedTx, table); err != nil {
return err
}
return nil
}
// alter table with columns (if not empty)
// recreate primary key (if not empty) or delete primary key if Table.DeletePkFields is true
func (p *Postgres) patchTableSchemaInTransaction(wrappedTx *Transaction, patchTable *Table) error {
pkFields := patchTable.GetPKFieldsMap()
//patch columns
for _, columnName := range patchTable.SortedColumnNames() {
column := patchTable.Columns[columnName]
columnDDL := p.columnDDL(columnName, column, pkFields)
query := fmt.Sprintf(addColumnTemplate, p.config.Schema, patchTable.Name, columnDDL)
p.queryLogger.LogDDL(query)
if _, err := wrappedTx.tx.ExecContext(p.ctx, query); err != nil {
err = checkErr(err)
return errorj.PatchTableError.Wrap(err, "failed to patch table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: patchTable.Name,
PrimaryKeys: patchTable.GetPKFields(),
Statement: query,
})
}
}
//patch primary keys - delete old
if patchTable.DeletePkFields {
err := p.deletePrimaryKeyInTransaction(wrappedTx, patchTable)
if err != nil {
return err
}
}
//patch primary keys - create new
if len(patchTable.PKFields) > 0 {
err := p.createPrimaryKeyInTransaction(wrappedTx, patchTable)
if err != nil {
return err
}
}
return nil
}
// createPrimaryKeyInTransaction create primary key constraint
func (p *Postgres) createPrimaryKeyInTransaction(wrappedTx *Transaction, table *Table) error {
if len(table.PKFields) == 0 {
return nil
}
var quotedColumnNames []string
for _, column := range table.GetPKFields() {
quotedColumnNames = append(quotedColumnNames, fmt.Sprintf(`"%s"`, column))
}
statement := fmt.Sprintf(alterPrimaryKeyTemplate,
p.config.Schema, table.Name, table.PrimaryKeyName, strings.Join(quotedColumnNames, ","))
p.queryLogger.LogDDL(statement)
if _, err := wrappedTx.tx.ExecContext(p.ctx, statement); err != nil {
err = checkErr(err)
return errorj.CreatePrimaryKeysError.Wrap(err, "failed to set primary key").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: statement,
})
}
return nil
}
// delete primary key
func (p *Postgres) deletePrimaryKeyInTransaction(wrappedTx *Transaction, table *Table) error {
query := fmt.Sprintf(dropPrimaryKeyTemplate, p.config.Schema, table.Name, table.PrimaryKeyName)
p.queryLogger.LogDDL(query)
if _, err := wrappedTx.tx.ExecContext(p.ctx, query); err != nil {
err = checkErr(err)
return errorj.DeletePrimaryKeysError.Wrap(err, "failed to delete primary key").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: query,
})
}
return nil
}
// DropTable drops table in transaction
func (p *Postgres) DropTable(table *Table) (err error) {
wrappedTx, err := p.OpenTx()
if err != nil {
return err
}
defer func() {
if err != nil {
rbErr := wrappedTx.Rollback()
if rbErr != nil {
err = errorj.Group(err, rbErr)
}
} else {
err = wrappedTx.Commit()
}
}()
return p.dropTableInTransaction(wrappedTx, table, false)
}
func (p *Postgres) ReplaceTable(originalTable, replacementTable string, dropOldTable bool) (err error) {
wrappedTx, err := p.OpenTx()
if err != nil {
return err
}
defer func() {
if err != nil {
rbErr := wrappedTx.Rollback()
if rbErr != nil {
err = errorj.Group(err, rbErr)
}
} else {
err = wrappedTx.Commit()
}
}()
tmpTable := "deprecated_" + originalTable + timestamp.Now().Format("_20060102_150405")
err1 := p.renameTableInTransaction(wrappedTx, true, originalTable, tmpTable)
err = p.renameTableInTransaction(wrappedTx, false, replacementTable, originalTable)
if dropOldTable && err1 == nil && err == nil {
return p.dropTableInTransaction(wrappedTx, &Table{Name: tmpTable}, true)
}
return
}
// Update one record in Postgres
func (p *Postgres) Update(table *Table, object map[string]interface{}, whereKey string, whereValue interface{}) error {
columns := make([]string, len(object), len(object))
values := make([]interface{}, len(object)+1, len(object)+1)
i := 0
for name, value := range object {
columns[i] = name + "= $" + strconv.Itoa(i+1) //$0 - wrong
values[i] = value
i++
}
values[i] = whereValue
statement := fmt.Sprintf(updateStatement, p.config.Schema, table.Name, strings.Join(columns, ", "), whereKey, i+1)
p.queryLogger.LogQueryWithValues(statement, values)
if _, err := p.dataSource.ExecContext(p.ctx, statement, values...); err != nil {
err = checkErr(err)
return errorj.UpdateError.Wrap(err, "failed to update").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: statement,
Values: values,
})
}
return nil
}
// bulkInsertInTransaction should be used when table has no primary keys. Inserts data in batches to improve performance.
func (p *Postgres) bulkInsertInTransaction(wrappedTx *Transaction, table *Table, objects []map[string]interface{}, valuesLimit int) error {
var placeholdersBuilder strings.Builder
var headerWithoutQuotes []string
for _, name := range table.SortedColumnNames() {
headerWithoutQuotes = append(headerWithoutQuotes, name)
}
valuesAmount := len(objects) * len(table.Columns)
maxValues := valuesAmount
if maxValues > valuesLimit {
maxValues = valuesLimit
}
valueArgs := make([]interface{}, 0, maxValues)
placeholdersCounter := 1
operation := 0
operations := int(math.Max(1, float64(valuesAmount)/float64(valuesLimit)))
for _, row := range objects {
// if number of values exceeds limit, we have to execute insert query on processed rows
if len(valueArgs)+len(headerWithoutQuotes) > valuesLimit {
operation++
if err := p.executeInsertInTransaction(wrappedTx, table, headerWithoutQuotes, removeLastComma(placeholdersBuilder.String()), valueArgs); err != nil {
return errorj.Decorate(err, "middle insert %d of %d in batch", operation, operations)
}
placeholdersBuilder.Reset()
placeholdersCounter = 1
valueArgs = make([]interface{}, 0, maxValues)
}
_, _ = placeholdersBuilder.WriteString("(")
for i, column := range headerWithoutQuotes {
value, _ := row[column]
//replace zero byte character for text fields
if table.Columns[column].Type == "text" {
if v, ok := value.(string); ok {
if strings.ContainsRune(v, '\u0000') {
value = strings.ReplaceAll(v, "\u0000", "")
}
}
}
valueArgs = append(valueArgs, value)
castClause := p.getCastClause(column, table.Columns[column])
_, _ = placeholdersBuilder.WriteString("$" + strconv.Itoa(placeholdersCounter) + castClause)
if i < len(headerWithoutQuotes)-1 {
_, _ = placeholdersBuilder.WriteString(",")
}
placeholdersCounter++
}
_, _ = placeholdersBuilder.WriteString("),")
}
if len(valueArgs) > 0 {
operation++
if err := p.executeInsertInTransaction(wrappedTx, table, headerWithoutQuotes, removeLastComma(placeholdersBuilder.String()), valueArgs); err != nil {
return errorj.Decorate(err, "last insert %d of %d in batch", operation, operations)
}
}
return nil
}
// bulkMergeInTransaction creates tmp table without duplicates
// inserts all data into tmp table and using bulkMergeTemplate merges all data to main table
func (p *Postgres) bulkMergeInTransaction(wrappedTx *Transaction, table *Table, objects []map[string]interface{}) error {
tmpTable := &Table{
Name: fmt.Sprintf("jitsu_tmp_%s", uuid.NewLettersNumbers()[:5]),
Columns: table.Columns,
PKFields: map[string]bool{},
DeletePkFields: false,
}
if err := p.createTableInTransaction(wrappedTx, tmpTable); err != nil {
return errorj.Decorate(err, "failed to create temporary table")
}
if err := p.bulkInsertInTransaction(wrappedTx, tmpTable, objects, PostgresValuesLimit); err != nil {
return errorj.Decorate(err, "failed to insert into temporary table")
}
//insert from select
var setValues []string
var headerWithQuotes []string
for _, name := range table.SortedColumnNames() {
setValues = append(setValues, fmt.Sprintf(`"%s"=%s."%s"`, name, bulkMergePrefix, name))
headerWithQuotes = append(headerWithQuotes, fmt.Sprintf(`"%s"`, name))
}
insertFromSelectStatement := fmt.Sprintf(bulkMergeTemplate, p.config.Schema, table.Name, strings.Join(headerWithQuotes, ", "), strings.Join(headerWithQuotes, ", "), p.config.Schema, tmpTable.Name, table.PrimaryKeyName, strings.Join(setValues, ", "))
p.queryLogger.LogQuery(insertFromSelectStatement)
if _, err := wrappedTx.tx.ExecContext(p.ctx, insertFromSelectStatement); err != nil {
err = checkErr(err)
return errorj.BulkMergeError.Wrap(err, "failed to bulk merge").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: insertFromSelectStatement,
})
}
//delete tmp table
if err := p.dropTableInTransaction(wrappedTx, tmpTable, false); err != nil {
return errorj.Decorate(err, "failed to drop temporary table")
}
return nil
}
func (p *Postgres) dropTableInTransaction(wrappedTx *Transaction, table *Table, ifExists bool) error {
ifExs := ""
if ifExists {
ifExs = "IF EXISTS "
}
query := fmt.Sprintf(dropTableTemplate, ifExs, p.config.Schema, table.Name)
p.queryLogger.LogDDL(query)
if _, err := wrappedTx.tx.ExecContext(p.ctx, query); err != nil {
err = checkErr(err)
return errorj.DropError.Wrap(err, "failed to drop table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: query,
})
}
return nil
}
func (p *Postgres) renameTableInTransaction(wrappedTx *Transaction, ifExists bool, tableName, newTableName string) error {
if ifExists {
row := wrappedTx.tx.QueryRowContext(p.ctx, fmt.Sprintf(`SELECT EXISTS (SELECT * FROM information_schema.tables WHERE table_schema = '%s' AND table_name = '%s')`, p.config.Schema, tableName))
exists := false
err := row.Scan(&exists)
if err != nil {
return err
}
if !exists {
return nil
}
}
query := fmt.Sprintf(renameTableTemplate, p.config.Schema, tableName, newTableName)
p.queryLogger.LogDDL(query)
if _, err := wrappedTx.tx.ExecContext(p.ctx, query); err != nil {
err = checkErr(err)
return errorj.RenameError.Wrap(err, "failed to rename table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: tableName,
Statement: query,
})
}
return nil
}
func (p *Postgres) deleteInTransaction(wrappedTx *Transaction, table *Table, deleteConditions *base.DeleteConditions) error {
deleteCondition, values := p.toDeleteQuery(table, deleteConditions)
query := fmt.Sprintf(deleteQueryTemplate, p.config.Schema, table.Name, deleteCondition)
p.queryLogger.LogQueryWithValues(query, values)
if _, err := wrappedTx.tx.ExecContext(p.ctx, query, values...); err != nil {
err = checkErr(err)
return errorj.DeleteFromTableError.Wrap(err, "failed to delete data").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: query,
})
}
return nil
}
func (p *Postgres) toDeleteQuery(table *Table, conditions *base.DeleteConditions) (string, []interface{}) {
var queryConditions []string
var values []interface{}
for i, condition := range conditions.Conditions {
conditionString := condition.Field + " " + condition.Clause + " $" + strconv.Itoa(i+1) + p.getCastClause(condition.Field, table.Columns[condition.Field])
queryConditions = append(queryConditions, conditionString)
values = append(values, typing.ReformatValue(condition.Value))
}
return strings.Join(queryConditions, " "+conditions.JoinCondition+" "), values
}
// executeInsert execute insert with insertTemplate
func (p *Postgres) executeInsertInTransaction(wrappedTx *Transaction, table *Table, headerWithoutQuotes []string,
placeholders string, valueArgs []interface{}) error {
var quotedHeader []string
for _, columnName := range headerWithoutQuotes {
quotedHeader = append(quotedHeader, fmt.Sprintf(`"%s"`, columnName))
}
statement := fmt.Sprintf(insertTemplate, p.config.Schema, table.Name, strings.Join(quotedHeader, ","), placeholders)
p.queryLogger.LogQueryWithValues(statement, valueArgs)
if _, err := wrappedTx.tx.Exec(statement, valueArgs...); err != nil {
err = checkErr(err)
return errorj.ExecuteInsertInBatchError.Wrap(err, "failed to execute insert").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: statement,
ValuesMapString: ObjectValuesToString(headerWithoutQuotes, valueArgs),
})
}
return nil
}
// columnDDL returns column DDL (quoted column name, mapped sql type and 'not null' if pk field)
func (p *Postgres) columnDDL(name string, column typing.SQLColumn, pkFields map[string]bool) string {
var notNullClause string
sqlType := column.DDLType()
if overriddenSQLType, ok := p.sqlTypes[name]; ok {
sqlType = overriddenSQLType.ColumnType
}
//not null
if _, ok := pkFields[name]; ok {
notNullClause = " not null " + p.getDefaultValueStatement(sqlType)
}
return fmt.Sprintf(`"%s" %s%s`, name, sqlType, notNullClause)
}
// getCastClause returns ::SQL_TYPE clause or empty string
// $1::type, $2::type, $3, etc
func (p *Postgres) getCastClause(name string, column typing.SQLColumn) string {
castType, ok := p.sqlTypes[name]
if !ok && column.Override {
castType = column
ok = true
}
if ok {
return "::" + castType.Type
}
return ""
}
// return default value statement for creating column
func (p *Postgres) getDefaultValueStatement(sqlType string) string {
sqlType = strings.ToLower(sqlType)
//get default value based on type
if strings.Contains(sqlType, "var") || strings.Contains(sqlType, "text") {
return "default ''"
}
if strings.Contains(sqlType, "timestamp") {
return "default CURRENT_TIMESTAMP"
}
return "default 0"
}
// Close underlying sql.DB
func (p *Postgres) Close() error {
return p.dataSource.Close()
}
// getPrimaryKey returns primary key name and fields
func (p *Postgres) getPrimaryKey(tableName string) (string, map[string]bool, error) {
primaryKeys := map[string]bool{}
pkFieldsRows, err := p.dataSource.QueryContext(p.ctx, primaryKeyFieldsQuery, p.config.Schema, tableName)
if err != nil {
err = checkErr(err)
return "", nil, errorj.GetPrimaryKeysError.Wrap(err, "failed to get primary key").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: p.config.Schema,
Table: tableName,
Statement: primaryKeyFieldsQuery,
Values: []interface{}{p.config.Schema, tableName},
})
}