/
main.go
36 lines (29 loc) · 900 Bytes
/
main.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
package scenarios
import (
"log"
"github.com/jmoiron/sqlx"
)
//go:generate go-bindata -nometadata -ignore (go|rb)$ -pkg scenarios .
// Load executes the sql script at `path` on postgres database at `url`
func Load(url string, path string) {
sql, err := Asset(path)
if err != nil {
log.Panic(err)
}
db, err := sqlx.Open("postgres", url)
if err != nil {
log.Fatalf("could not exec open postgres connection: %v\n", err)
}
defer db.Close()
// clear out existing schema before applying scenario
// otherwise, applying the scenario will result in the following error:
// pq: cannot drop schema public because other objects depend on it
_, err = db.Exec("DROP SCHEMA IF EXISTS public cascade")
if err != nil {
log.Fatalf("could not drop public schema: %v\n", err)
}
_, err = db.Exec(string(sql))
if err != nil {
log.Fatalf("could not exec scenario %v: %v\n", path, err)
}
}