forked from ProlificLabs/datapasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres_adapter.go
288 lines (256 loc) · 8.02 KB
/
postgres_adapter.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
package sqlclone
import (
"database/sql"
"fmt"
)
type References map[string][]TableReference
type postgresDB struct {
*sql.DB
}
type database interface {
getRows(string, string, string) ([]map[string]string, error)
insertRow(*sql.Tx, string, []string, []string, string) (int, error)
getTables() ([]string, error)
getReferences() (References, error)
getPrimaryKeys() (map[string][]string, error)
getDependencyOrder() ([]string, error)
newTransaction() (*sql.Tx, error)
}
func NewPostgresAdapter(host string, port int, user string, pw string, db_name string) (database, error) {
postgres_db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, pw, db_name))
if err != nil {
return nil, fmt.Errorf("could not connect to PostgreSQL database")
}
return postgresDB{postgres_db}, nil
}
func (db postgresDB) newTransaction() (*sql.Tx, error) {
tx, err := db.Begin()
if err != nil {
return nil, fmt.Errorf("could not start transaction %s", err)
}
return tx, nil
}
// get rows from a table where a column has a certain value
func (db postgresDB) getRows(table_name string, col string, val string) ([]map[string]string, error) {
ret := make([]map[string]string, 0)
if val != "<nil>" {
query := "SELECT * FROM " + table_name + " WHERE \"" + col + "\"='" + val + "'"
fmt.Println(query)
rows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("query %s could not be executed: %s", query, err)
}
defer rows.Close()
columns, _ := rows.Columns()
values := make([]sql.NullString, len(columns))
for rows.Next() {
scanArgs := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
if err := rows.Scan(scanArgs...); err != nil {
return nil, fmt.Errorf("values could not be extracted from row: %s", err)
}
resultMap := make(map[string]string)
for i, value := range values {
if !value.Valid { // value was NULL
resultMap[columns[i]] = "<nil>"
} else {
resultMap[columns[i]] = value.String
}
}
ret = append(ret, resultMap)
}
}
return ret, nil
}
// insert a row with given column names and values into a database.
// if the table has a column with an automatically generated value,
// return that value after insertion, return -1 otherwise
func (db postgresDB) insertRow(tx *sql.Tx, table_name string, columns []string, values []string, primary_key string) (int, error) {
cols := ""
vals := ""
for i, c := range columns {
if c != primary_key {
cols += "\"" + c + "\", "
if values[i] != "<nil>" {
vals += "'" + values[i] + "', "
} else {
vals += "NULL, "
}
}
}
cols = cols[:len(cols)-2]
vals = vals[:len(vals)-2]
query := "INSERT INTO " + table_name + " (" + cols + ") VALUES (" + vals + ")"
lastInsertId := -1
if primary_key != "" {
query += " RETURNING " + primary_key
fmt.Println(query)
err := tx.QueryRow(query).Scan(&lastInsertId)
if err != nil {
return -1, fmt.Errorf("query %s could not be executed: %s", query, err)
}
} else {
fmt.Println(query)
_, err := tx.Exec(query)
if err != nil {
return -1, fmt.Errorf("query %s could not be executed: %s", query, err)
}
}
return lastInsertId, nil
}
// get list of tables in the database
func (db postgresDB) getTables() ([]string, error) {
var query = "" +
"SELECT table_name " +
"FROM information_schema.tables " +
"WHERE table_schema = 'public'"
rows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("query %s could not be executed: %s", query, err)
}
defer rows.Close()
tables := make([]string, 0)
for rows.Next() {
var t string
if err := rows.Scan(&t); err != nil {
return nil, fmt.Errorf("error extracting table name from result set: %s", err)
}
tables = append(tables, t)
}
return tables, nil
}
// get all references from all tables
func (db postgresDB) getReferences() (References, error) {
var query = "" +
"SELECT " +
"conrelid::regclass table_name, " +
"a1.attname column_name, " +
"confrelid::regclass referenced_table, " +
"a2.attname referenced_column_name " +
"FROM (" +
"select conrelid::regclass, confrelid::regclass, col, fcol " +
"from pg_constraint, " +
"lateral unnest(conkey) col, " +
"lateral unnest(confkey) fcol " +
"where contype = 'f'" +
") s " +
"JOIN pg_attribute a1 ON a1.attrelid = conrelid AND a1.attnum = col " +
"JOIN pg_attribute a2 ON a2.attrelid = confrelid AND a2.attnum = fcol;"
rows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("query %s could not be executed: %s", query, err)
}
defer rows.Close()
references := make(References)
for rows.Next() {
var t, tc, rt, rtc string
if err := rows.Scan(&t, &tc, &rt, &rtc); err != nil {
return nil, fmt.Errorf("error extracting table reference from result set: %s", err)
}
references[t] = append(references[t], TableReference{table_name: t, column_name: tc, referenced_table_name: rt, referenced_column_name: rtc})
}
return references, nil
}
// get all tables that have primary keys and their primary keys
func (db postgresDB) getPrimaryKeys() (map[string][]string, error) {
var query = "" +
"SELECT tc.table_name, kc.column_name " +
"FROM " +
"information_schema.table_constraints tc, " +
"information_schema.key_column_usage kc " +
"WHERE " +
"tc.constraint_type = 'PRIMARY KEY' " +
"AND tc.table_schema = 'public' " +
"AND kc.table_name = tc.table_name and kc.table_schema = tc.table_schema " +
"AND kc.constraint_name = tc.constraint_name"
rows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("query %s could not be executed: %s", query, err)
}
defer rows.Close()
primary_keys := make(map[string][]string, 0)
for rows.Next() {
var t, c string
if err := rows.Scan(&t, &c); err != nil {
return nil, fmt.Errorf("error extracting primary key from result set: %s", err)
}
primary_keys[t] = append(primary_keys[t], c)
}
return primary_keys, nil
}
// returns the list of tables after a topological sort following Kahn's algorithm.
// this list will be used to perform cloning so that data is inserted into the target database
// before it is needed by referencing rows later on
func (db postgresDB) getDependencyOrder() ([]string, error) {
references, err := db.getReferences()
if err != nil {
return nil, err
}
tables, err := db.getTables()
if err != nil {
return nil, err
}
visited := make([]string, 0)
order := make([]string, 0)
S := make([]string, 0)
out_degrees := make(map[string]int, 0)
for _, table := range tables {
ref_tables := getReferencesFromTable(references, table)
out_degrees[table] = len(ref_tables)
self_referencing, _ := isTableSelfReferencing(references, table)
if self_referencing {
out_degrees[table]--
}
if out_degrees[table] == 0 {
S = append(S, table)
}
}
for len(S) != 0 {
table := S[len(S)-1]
order = append(order, table)
visited = append(visited, table)
S = S[:len(S)-1] // remove table from S
edges := getReferencesToTable(references, table)
for _, r := range edges {
out_degrees[r.table_name]--
if out_degrees[r.table_name] == 0 && !sliceContains(visited, r.table_name) {
S = append(S, r.table_name)
}
}
}
return order, nil
}
func isTableSelfReferencing(references References, table_name string) (bool, string) {
from := getReferencesFromTable(references, table_name)
for _, d := range from {
if d.referenced_table_name == table_name {
return true, d.referenced_column_name
}
}
return false, ""
}
func getReferencesToTable(references References, table_name string) []TableReference {
var ret = make([]TableReference, 0)
for _, value := range references {
for _, d := range value {
if d.referenced_table_name == table_name {
ret = append(ret, d)
}
}
}
return ret
}
func getReferencesFromTable(references References, table_name string) []TableReference {
return references[table_name]
}
func getReference(input []TableReference, searchString string) (TableReference, bool) {
var ret TableReference
for _, d := range input {
if d.column_name == searchString {
return d, true
}
}
return ret, false
}