/
table.go
180 lines (153 loc) · 4.35 KB
/
table.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
package sql
import (
"fmt"
"github.com/jitsucom/bulker/bulkerlib/types"
"github.com/jitsucom/bulker/jitsubase/utils"
"github.com/jitsucom/bulker/jitsubase/uuid"
"sort"
"strings"
)
const BulkerManagedPkConstraintPrefix = "jitsu_pk_"
// Columns is a list of columns representation
type Columns map[string]types.SQLColumn
// TableField is a table column representation
type TableField struct {
Field string `json:"field,omitempty"`
Type string `json:"type,omitempty"`
Value any `json:"value,omitempty"`
}
// Table is a dto for DWH Table representation
type Table struct {
Name string
Temporary bool
Cached bool
Columns Columns
PKFields utils.Set[string]
PrimaryKeyName string
TimestampColumn string
Partition DatePartition
DeletePkFields bool
}
// Exists returns true if there is at least one column
func (t *Table) Exists() bool {
if t == nil {
return false
}
return len(t.Columns) > 0 || len(t.PKFields) > 0 || t.DeletePkFields
}
// SortedColumnNames return column names sorted in alphabetical order
func (t *Table) SortedColumnNames() []string {
columns := make([]string, 0, len(t.Columns))
for name := range t.Columns {
columns = append(columns, name)
}
sort.Strings(columns)
return columns
}
// Clone returns clone of current table
func (t *Table) Clone() *Table {
clonedColumns := Columns{}
for k, v := range t.Columns {
clonedColumns[k] = v
}
clonedPkFields := t.PKFields.Clone()
return &Table{
Name: t.Name,
Columns: clonedColumns,
PKFields: clonedPkFields,
PrimaryKeyName: t.PrimaryKeyName,
Temporary: t.Temporary,
TimestampColumn: t.TimestampColumn,
Partition: t.Partition,
Cached: t.Cached,
DeletePkFields: t.DeletePkFields,
}
}
// GetPKFields returns primary keys list
func (t *Table) GetPKFields() []string {
if t.PKFields != nil {
return t.PKFields.ToSlice()
} else {
return []string{}
}
}
// GetPKFieldsSet returns primary keys set
func (t *Table) GetPKFieldsSet() utils.Set[string] {
if t.PKFields != nil {
return t.PKFields
} else {
return utils.Set[string]{}
}
}
// Diff calculates diff between current schema and another one.
// Return schema to add to current schema (for being equal) or empty if
// 1) another one is empty
// 2) all fields from another schema exist in current schema
// NOTE: Diff method doesn't take types into account
func (t *Table) Diff(another *Table) *Table {
diff := &Table{Name: t.Name, Columns: map[string]types.SQLColumn{}, PKFields: utils.Set[string]{}}
if !another.Exists() {
return diff
}
for name, column := range another.Columns {
_, ok := t.Columns[name]
if !ok {
diff.Columns[name] = column
}
}
jitsuPrimaryKeyName := BuildConstraintName(t.Name)
//check if primary key is maintained by Jitsu (for Postgres and Redshift)
if t.PrimaryKeyName != "" && !strings.HasPrefix(strings.ToLower(t.PrimaryKeyName), BulkerManagedPkConstraintPrefix) {
//primary key isn't maintained by Jitsu: do nothing
return diff
}
//primary keys logic
if len(t.PKFields) > 0 {
if !t.PKFields.Equals(another.PKFields) {
//re-create or delete if another.PKFields is empty
diff.DeletePkFields = true
diff.PKFields = another.PKFields
diff.PrimaryKeyName = jitsuPrimaryKeyName
}
} else if len(another.PKFields) > 0 {
//create
diff.PKFields = another.PKFields
diff.PrimaryKeyName = jitsuPrimaryKeyName
}
return diff
}
// FitsToTable checks that current table fits to the destination table column-wise (doesn't have new columns)
func (t *Table) FitsToTable(destination *Table) bool {
for name := range t.Columns {
_, ok := destination.Columns[name]
if !ok {
return false
}
}
return true
}
func BuildConstraintName(tableName string) string {
return fmt.Sprintf("%s%s", BulkerManagedPkConstraintPrefix, uuid.NewLettersNumbers())
}
func (c Columns) Clone() Columns {
cloned := Columns{}
for k, v := range c {
cloned[k] = v
}
return cloned
}
func (c Columns) ToSimpleMap() map[string]string {
simple := make(map[string]string, len(c))
for name, column := range c {
simple[name] = column.Type
}
return simple
}
func (t *Table) ColumnsWithSQLTypes() [][]string {
sortedColumnNames := t.SortedColumnNames()
simple := make([][]string, 0, len(sortedColumnNames))
for _, name := range sortedColumnNames {
simple = append(simple, []string{name, t.Columns[name].Type})
}
return simple
}