-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync.go
168 lines (139 loc) · 3.21 KB
/
sync.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
package bloomdb
import (
"bytes"
"database/sql"
"github.com/lib/pq"
"text/template"
"time"
"log"
)
type syncInfo struct {
Table string
Columns []string
CreatedAt string
UpdatedAt string
}
func buildSyncQuery(table string, columns []string, now string) (string, error) {
buf := new(bytes.Buffer)
t, err := template.New("sync.sql.template").Funcs(fns).Parse(syncSql)
if err != nil {
return "", err
}
info := syncInfo{table, columns, now, now}
err = t.Execute(buf, info)
if err != nil {
return "", err
}
return buf.String(), nil
}
func Sync(db *sql.DB, table string, columns []string, rows chan []string) error {
now := time.Now().UTC().Format(time.RFC3339)
query, err := buildSyncQuery(table, columns, now)
if err != nil {
return err
}
_, err = db.Exec("DROP TABLE IF EXISTS " + table + "_temp;")
if err != nil {
return err
}
txn, err := db.Begin()
if err != nil {
return err
}
_, err = txn.Exec("CREATE TABLE " + table + "_temp(LIKE " + table + ");")
if err != nil {
return err
}
columns = append([]string{ "bloom_created_at"}, columns...)
stmt, err := txn.Prepare(pq.CopyIn(table + "_temp", columns...))
if err != nil {
return err
}
for rawRow := range rows {
row := make([]interface{}, len(rawRow) + 1)
row[0] = now
for i, column := range rawRow {
if column == "" {
row[i + 1] = nil
} else {
row[i + 1] = column
}
}
_, err = stmt.Exec(row...)
if err != nil {
log.Println("table", table, "row", row, "columns", columns)
return err
}
}
_, err = stmt.Exec()
if err != nil {
return err
}
err = txn.Commit()
if err != nil {
return err
}
log.Println("Delete Duplicates")
_, err = db.Exec(`
CREATE TABLE ` + table + `_dedup
AS SELECT DISTINCT * FROM ` + table + `_temp;
DROP TABLE ` + table + `_temp;
ALTER TABLE ` + table + `_dedup
RENAME TO ` + table + `_temp;`)
if err != nil {
return err
}
log.Println("Creating Indexes")
_, err = db.Exec("CREATE INDEX ON " + table + "_temp (id); CREATE INDEX ON " + table + "_temp (revision);")
if err != nil {
return err
}
log.Println("Anayzing")
_, err = db.Exec("ANALYZE " + table + "_temp (id, revision)")
if err != nil {
return err
}
_, err = db.Exec("ANALYZE " + table + " (id, revision)")
if err != nil {
return err
}
log.Println("Running Sync Query")
_, err = db.Exec(query)
if err != nil {
return err
}
log.Println("Add indexes to temp")
// NOTE: this currently only supports single column, btree indexes
indexRows, err := db.Query(`SELECT pg_get_indexdef(idx.indexrelid, 1, true)
FROM pg_index as idx
WHERE indrelid = '` + table + `'::regclass`)
if err != nil {
return err
}
defer indexRows.Close()
reindexQuery := ""
for indexRows.Next() {
var column string
if err := indexRows.Scan(&column); err != nil {
return err
}
if column != "id" && column != "revision" {
reindexQuery += "CREATE INDEX ON " + table + "_temp (" + column + ");\n"
}
}
if err := indexRows.Err(); err != nil {
return err
}
_, err = db.Exec(reindexQuery)
if err != nil {
return err
}
log.Println("Swap temp table for real")
_, err = db.Exec(`DROP TABLE ` + table + `;
ALTER TABLE ` + table + `_temp
RENAME TO ` + table + `;`)
if err != nil {
return err
}
return nil
}