This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sqlite.go
393 lines (340 loc) · 8.55 KB
/
sqlite.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
/*
Copyright 2016 The Transicator Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package snapshotserver
import (
"bytes"
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"time"
log "github.com/Sirupsen/logrus"
sqlite "github.com/mattn/go-sqlite3"
)
var sqliteTimestampFormat = sqlite.SQLiteTimestampFormats[0]
/*
WriteSqliteSnapshot is responsible for generating the SQLite format of
the snapshot, and also for streaming the file back to the
caller.
*/
func WriteSqliteSnapshot(scopes []string, db *sql.DB, w http.ResponseWriter, r *http.Request) error {
// Create temporary directory for the temporary DB
dirName, err := ioutil.TempDir(tempSnapshotDir, tempSnapshotPrefix)
if err != nil {
sendAPIError(serverError, err.Error(), w, r)
return err
}
defer func() {
os.RemoveAll(dirName)
}()
// Open and verify the DB
dbFileName := path.Join(dirName, tempSnapshotName)
tdb, err := createDatabase(dbFileName)
if err != nil {
sendAPIError(http.StatusInternalServerError, err.Error(), w, r)
return err
}
defer tdb.Close()
// Because of previous config, this puts us in "repeatable read" mode
pgTx, err := db.Begin()
if err != nil {
sendAPIError(serverError, err.Error(), w, r)
return err
}
defer pgTx.Commit()
tables, err := enumeratePgTables(pgTx)
if err != nil {
sendAPIError(http.StatusInternalServerError, err.Error(), w, r)
return err
}
err = writeMetadata(pgTx, tdb, tables)
if err != nil {
sendAPIError(http.StatusInternalServerError, err.Error(), w, r)
return err
}
// put tx id into header
row := pgTx.QueryRow("select txid_current_snapshot()")
var txId string
err = row.Scan(&txId)
if err == nil {
w.Header().Set("Transicator-Snapshot-TXID", txId)
}
// For each table, update the DB
for tid, pgTable := range tables {
if pgTable.hasSelector {
err = makeSqliteTable(tdb, pgTable)
if err == nil {
err = copyData(pgTx, tdb, scopes, pgTable)
}
if err != nil {
sendAPIError(serverError, err.Error(), w, r)
return err
}
} else {
log.Debugf("Skipping table %s which has no selector", tid)
}
}
pgTx.Commit()
// Close the database. Checkpoint the WAL so that the result
// is a single file, with no additional .wal or .shm file.
_, err = tdb.Exec("pragma wal_checkpoint(TRUNCATE)")
if err != nil {
sendAPIError(serverError, err.Error(), w, r)
return err
}
tdb.Close()
// Stream the result to the client
return streamFile(dbFileName, w)
}
func writeMetadata(pgTx *sql.Tx, tdb *sql.DB, tables map[string]*pgTable) error {
_, err := tdb.Exec(`
create table _transicator_metadata
(key varchar primary key,
value varchar)
`)
if err == nil {
_, err = tdb.Exec(`
create table _transicator_tables
(tableName varchar not null,
columnName varchar not null,
typid integer,
primaryKey bool)
`)
}
if err == nil {
row := pgTx.QueryRow("select txid_current_snapshot()")
var snap string
err = row.Scan(&snap)
if err == nil {
_, err = tdb.Exec(`
insert into _transicator_metadata (key, value) values('snapshot', ?)
`, snap)
}
}
var st *sql.Stmt
if err == nil {
st, err = tdb.Prepare(`
insert into _transicator_tables
(tableName, columnName, typid, primaryKey)
values (?, ?, ?, ?)
`)
defer st.Close()
}
if err == nil {
for _, table := range tables {
for _, col := range table.columns {
_, err = st.Exec(table.schema+"_"+table.name, col.name, col.typid, col.primaryKey)
if err != nil {
return err
}
}
}
}
return err
}
func createDatabase(fileName string) (*sql.DB, error) {
log.Debugf("Opening temporary SQLite database in %s", fileName)
tdb, err := sql.Open("sqlite3", fileName)
if err == nil {
err = tdb.Ping()
}
if err != nil {
return nil, err
}
_, err = tdb.Exec("pragma journal_mode = WAL")
if err != nil {
tdb.Close()
return nil, err
}
_, err = tdb.Exec("pragma synchronous = OFF")
if err != nil {
tdb.Close()
return nil, err
}
return tdb, err
}
func makeSqliteTable(tdb *sql.DB, t *pgTable) error {
sql := makeSqliteTableSQL(t)
log.Debugf("New SQLite table: %s", sql)
_, err := tdb.Exec(sql)
return err
}
// makeSqliteTableSQL turns the parsed table description from Postgres
// into a "create table" statement that works in SQLite.
func makeSqliteTableSQL(t *pgTable) string {
s := &bytes.Buffer{}
s.WriteString(fmt.Sprintf("create table %s (", t.schema+"_"+t.name))
first := true
for _, col := range t.columns {
if first {
first = false
} else {
s.WriteString(",")
}
s.WriteString(fmt.Sprintf("%s %s", col.name, convertPgType(col.typid)))
}
if len(t.primaryKeys) > 0 {
s.WriteString(", primary key (")
for pki, pk := range t.primaryKeys {
if pki > 0 {
s.WriteString(",")
}
s.WriteString(pk)
}
s.WriteString(")")
}
s.WriteString(")")
return s.String()
}
func copyData(pgTx *sql.Tx, tdb *sql.DB, scopes []string, pgTable *pgTable) error {
sql := fmt.Sprintf("select * from %s.%s where %s in %s",
pgTable.schema, pgTable.name, selectorColumn, GetTenants(scopes))
log.Debugf("Postgres query: %s", sql)
pgRows, err := pgTx.Query(sql)
if err != nil {
return err
}
defer pgRows.Close()
// Use column names from the query and not from the table definition.
// That way we are sure that we are getting them in the right order.
colNames, colTypes, err := parseColumnNames(pgRows)
if err != nil {
return err
}
sql = makeInsertSQL(pgTable, colNames)
log.Debugf("Sqlite insert: %s", sql)
stmt, err := tdb.Prepare(sql)
if err != nil {
return err
}
defer stmt.Close()
tx, err := tdb.Begin()
if err != nil {
return err
}
for pgRows.Next() {
cols := make([]interface{}, len(colNames))
for i := range cols {
switch pgToSqliteType(int(colTypes[i])) {
case sqlInteger:
cols[i] = new(*int64)
case sqlReal:
cols[i] = new(*float64)
case sqlBlob:
cols[i] = new([]byte)
case sqlText:
cols[i] = new(*string)
case sqlTimestamp:
cols[i] = new(*time.Time)
default:
panic(fmt.Sprintf("Invalid type code %d", pgToSqliteType(int(colTypes[i]))))
}
}
err = pgRows.Scan(cols...)
if err != nil {
tx.Rollback()
log.Errorf("Postgres scan error %s. cols = %s", err, debugColTypes(cols))
return err
}
patchColTypes(cols)
_, err := tx.Stmt(stmt).Exec(cols...)
if err != nil {
tx.Rollback()
log.Errorf("SQLite insert error %s. SQL = %s. cols = %s", err, sql, debugColTypes(cols))
return err
}
}
tx.Commit()
return nil
}
func debugColTypes(cols []interface{}) string {
s := &bytes.Buffer{}
for i, c := range cols {
if i > 0 {
s.WriteString(",")
}
s.WriteString(fmt.Sprintf("%T", c))
}
return s.String()
}
// patchColTypes looks for column types that are different between PG
// and SQLite.
func patchColTypes(cols []interface{}) {
for i, c := range cols {
switch c.(type) {
case **time.Time:
ts := *(c.(**time.Time))
if ts == nil {
cols[i] = ""
} else {
cols[i] = ts.UTC().Format(sqliteTimestampFormat)
}
}
}
}
func makeInsertSQL(pgTable *pgTable, colNames []string) string {
s := &bytes.Buffer{}
s.WriteString(fmt.Sprintf("insert into %s (", pgTable.schema+"_"+pgTable.name))
for i, cn := range colNames {
if i > 0 {
s.WriteString(",")
}
s.WriteString(cn)
}
s.WriteString(") values(")
for i := range colNames {
if i > 0 {
s.WriteString(",")
}
s.WriteString("?")
}
s.WriteString(")")
return s.String()
}
func generateEtag(srcFile string) (error, string) {
inFileHash, err := os.Open(srcFile)
if err != nil {
return err, ""
}
defer inFileHash.Close()
hasher := sha256.New()
_, err = io.Copy(hasher, inFileHash)
if err != nil {
return err, ""
}
etag := &bytes.Buffer{}
etag.WriteString("\"")
etag.WriteString(hex.EncodeToString(hasher.Sum(nil)))
etag.WriteString("\"")
return nil, etag.String()
}
func streamFile(srcFile string, w http.ResponseWriter) error {
err, etag := generateEtag(srcFile)
if err != nil {
return err
}
w.Header().Set("ETag", etag)
w.Header().Set("Content-Type", sqlMediaType)
inFile, err := os.Open(srcFile)
if err != nil {
return err
}
defer inFile.Close()
_, err = io.Copy(w, inFile)
return err
}