-
Notifications
You must be signed in to change notification settings - Fork 2
/
populator.go
178 lines (159 loc) · 4.73 KB
/
populator.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
package main
import (
"database/sql"
"fmt"
"strings"
)
type Populator struct {
DB *sql.DB
Driver string
currentPlaceholderIndex int
}
func (p *Populator) getPlaceholder() string {
if p.Driver == "postgres" {
val := p.currentPlaceholderIndex
p.currentPlaceholderIndex++
return fmt.Sprintf("$%d", val)
}
return "?"
}
func (p *Populator) resetPlaceholder() {
p.currentPlaceholderIndex = 1
}
func (p *Populator) PopulateData(fixtures []Fixture) error {
for _, f := range fixtures {
if err := p.PopulateFixture(f); err != nil {
return err
}
}
return nil
}
func (p *Populator) generateCondition(keys []string, data map[string]interface{}) (string, []interface{}, error) {
var conditions []string
var args []interface{}
for _, key := range keys {
if v, ok := data[key]; ok {
conditions = append(conditions, fmt.Sprintf("%s=%s", surroundKeyWithQuote(key, p.Driver), p.getPlaceholder()))
args = append(args, v)
} else {
return "", nil, fmt.Errorf("key %s not found in record %v", key, data)
}
}
return "(" + strings.Join(conditions, " AND ") + ")", args, nil
}
func (p *Populator) generateSelectQuery(fixture Fixture) (string, []interface{}, error) {
quotedKeys := surroundKeysWithQuotes(fixture.Keys, p.Driver)
query := fmt.Sprintf("SELECT %s FROM %s WHERE ", strings.Join(quotedKeys, ", "), fixture.TableName)
var conditions []string
var args []interface{}
for _, data := range fixture.Data {
condition, cArgs, err := p.generateCondition(fixture.Keys, data)
if err != nil {
return "", nil, err
}
conditions = append(conditions, condition)
args = append(args, cArgs...)
}
return query + strings.Join(conditions, " OR "), args, nil
}
func (p *Populator) makePlaceholders(args []interface{}) string {
var placeholders []string
for _, _ = range args {
placeholders = append(placeholders, p.getPlaceholder())
}
return "(" + strings.Join(placeholders, ",") + ")"
}
func (p *Populator) generateInsertStmt(fixture Fixture, data []map[string]interface{}) (string, []interface{}) {
query := "INSERT INTO %s (%s) VALUES %s"
keys := extractKeys(data)
var args []interface{}
var placeholders []string
for _, record := range data {
var recordArgs []interface{}
for _, k := range keys {
if v, ok := record[k]; ok {
recordArgs = append(recordArgs, v)
}
}
placeholders = append(placeholders, p.makePlaceholders(recordArgs))
args = append(args, recordArgs...)
}
values := strings.Join(placeholders, ",")
quotedKeys := surroundKeysWithQuotes(keys, p.Driver)
return fmt.Sprintf(query, fixture.TableName, strings.Join(quotedKeys, ","), values), args
}
func (p *Populator) getExistingData(fixture Fixture) ([]map[string]interface{}, error) {
query, args, err := p.generateSelectQuery(fixture)
if err != nil {
return nil, err
}
logger.Debugf("executing query %s with args %+v", query, args)
rows, err := p.DB.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
p.resetPlaceholder()
var result []map[string]interface{}
values := make([]interface{}, len(fixture.Keys))
valuesPtr := make([]interface{}, len(fixture.Keys))
for rows.Next() {
row := make(map[string]interface{})
for i := 0; i < len(fixture.Keys); i++ {
valuesPtr[i] = &values[i]
}
rows.Scan(valuesPtr...)
for i, res := range values {
row[fixture.Keys[i]] = res
}
result = append(result, row)
}
return result, nil
}
func (p *Populator) hasRecord(keys []string, existingData []map[string]interface{}, record map[string]interface{}) bool {
CheckEquality:
for _, data := range existingData {
for _, k := range keys {
if !ObjectsAreEqualValues(data[k], record[k]) {
continue CheckEquality
}
}
return true
}
return false
}
func (p *Populator) getNewData(fixture Fixture, existingData []map[string]interface{}) (newData []map[string]interface{}) {
for _, record := range fixture.Data {
if !p.hasRecord(fixture.Keys, existingData, record) {
newData = append(newData, record)
}
}
return newData
}
func (p *Populator) PopulateFixture(fixture Fixture) error {
existingData, err := p.getExistingData(fixture)
if err != nil {
return err
}
newData := p.getNewData(fixture, existingData)
if len(newData) == 0 {
return nil
}
insertStmt, args := p.generateInsertStmt(fixture, newData)
logger.Infof("inserting %d new records in %s", len(newData), fixture.TableName)
logger.Debugf("executing %s with args %+v", insertStmt, args)
_, err = p.DB.Exec(insertStmt, args...)
p.resetPlaceholder()
return err
}
func NewPopulator(dbUrl string) (*Populator, error) {
db, driverName, err := ConnectToDb(dbUrl)
if err != nil {
return nil, err
}
return &Populator{
DB: db,
Driver: driverName,
currentPlaceholderIndex: 1,
}, nil
}