-
Notifications
You must be signed in to change notification settings - Fork 246
/
db.go
72 lines (59 loc) · 1.49 KB
/
db.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
package helpers
import (
"database/sql"
"io/ioutil"
"os"
"github.com/status-im/status-go/common/dbsetup"
)
const kdfIterationsNumberForTests = 1
// SetupTestSQLDB creates a temporary sqlite database file, initialises and then returns with a teardown func
func SetupTestSQLDB(dbInit dbsetup.DatabaseInitializer, prefix string) (*sql.DB, func() error, error) {
tmpfile, err := ioutil.TempFile("", prefix)
if err != nil {
return nil, nil, err
}
db, err := dbInit.Initialize(tmpfile.Name(), "password", kdfIterationsNumberForTests)
if err != nil {
return nil, nil, err
}
return db, func() error {
err := db.Close()
if err != nil {
return err
}
return os.Remove(tmpfile.Name())
}, nil
}
func SetupTestMemorySQLDB(dbInit dbsetup.DatabaseInitializer) (*sql.DB, error) {
db, err := dbInit.Initialize(dbsetup.InMemoryPath, "password", kdfIterationsNumberForTests)
if err != nil {
return nil, err
}
return db, nil
}
func ColumnExists(db *sql.DB, tableName string, columnName string) (bool, error) {
rows, err := db.Query("PRAGMA table_info(" + tableName + ")")
if err != nil {
return false, err
}
defer rows.Close()
var cid int
var name string
var dataType string
var notNull bool
var dFLTValue sql.NullString
var pk int
for rows.Next() {
err := rows.Scan(&cid, &name, &dataType, ¬Null, &dFLTValue, &pk)
if err != nil {
return false, err
}
if name == columnName {
return true, nil
}
}
if rows.Err() != nil {
return false, rows.Err()
}
return false, nil
}