-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
50 lines (39 loc) · 951 Bytes
/
main_test.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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"testing"
"github.com/mjl-/sconf"
)
func TestMain(m *testing.M) {
check := func(err error, msg string) {
if err != nil {
if msg == "" {
log.Fatal(err)
}
log.Fatalf("%s: %s", msg, err)
}
}
flag.Parse()
args := flag.Args()
if len(args) != 1 {
check(fmt.Errorf("bad command-line arguments, need 1 config file"), "")
}
err := sconf.ParseFile(args[0], &config)
check(err, "parsing config file")
scripts := parseSQLScripts()
database, err = sql.Open("postgres", config.Database)
check(err, "connecting to database")
tx, err := database.Begin()
check(err, "begin db transaction")
_, err = tx.Exec("drop schema if exists public cascade; create schema public")
check(err, "recreating public schema")
committing := true
runScripts(tx, -1, scripts, committing)
err = tx.Commit()
check(err, "committing initialized database")
os.Exit(m.Run())
}