-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
106 lines (91 loc) · 2.83 KB
/
config.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
// Package testsconfig defines the various parameters and methods needed to be
// set up pg and sqlite dbs for tests to run successfully.
package testsconfig
import (
"bufio"
"bytes"
"os"
"path/filepath"
"regexp"
)
const (
// sqliteChartsTestsDb is the default name of the sqlite tests db file that
// will be located.
sqliteChartsTestsDb = "test.sqlt.db"
defaultSqliteBlockRange = "0-199"
)
// Test DB server and database config.
const (
PGChartsTestsHost = "localhost"
PGChartsTestsPort = "5432"
PGChartsTestsUser = "bitumdata"
PGChartsTestsPass = ""
PGChartsTestsDBName = "bitumdata_mainnet_test"
)
// SqliteDbFilePath returns the absolute sqlite db filepath when accessed from
// bitumsqlite package.
func SqliteDbFilePath() (string, error) {
tempDir, err := filepath.Abs("../../testutil/dbload/testsconfig/test.data/")
dbPath := filepath.Join(tempDir, sqliteChartsTestsDb)
return dbPath, err
}
// SqliteDumpDataFilePath returns the path to the sqlite db dump data filepath
// when accessed from bitumsqlite package.
func SqliteDumpDataFilePath() (string, error) {
blockRange := os.Getenv("BLOCK_RANGE")
if blockRange == "" {
blockRange = defaultSqliteBlockRange
}
return filepath.Abs("../../testutil/dbload/testsconfig/test.data/sqlite_" + blockRange + ".sql")
}
// Migrations enables a custom migration runner to be used to load data from a
// given *.sql file dump into the migrations runner method.
type Migrations interface {
// Path returns the filepath to the *.sql dump file.
Path() string
// Runner executes the scanned individual queries one after another.
Runner(query string) error
}
// CustomScanner enables a given *.sql file dump to be scanned and loaded
// into the respectived query runner using the Runner method.
func CustomScanner(m Migrations) error {
file, err := os.Open(m.Path())
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(scanQueries)
for scanner.Scan() {
text := scanner.Text()
if len(text) == 0 {
continue
}
if err = m.Runner(text); err != nil {
return err
}
}
return scanner.Err()
}
// scanQueries scans individual token separated by semi-colons since the queries
// are separated by semi-colons.
func scanQueries(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, bufio.ErrFinalToken
}
if i := bytes.LastIndex(data, []byte(";\n")); i >= 0 {
return i + 1, deleteComments(data[:i]), nil
}
if atEOF {
return len(data), deleteComments(data), nil
}
return 0, nil, nil
}
// Comments start with (--) and end with a new line.
func deleteComments(a []byte) []byte {
re := regexp.MustCompile(`^--[\s*\w*[[:punct:]]*]*$\n`)
a = re.ReplaceAll(a, []byte{})
// delete "test_" from all table references.
re = regexp.MustCompile(`test_`)
return re.ReplaceAll(a, []byte{})
}