-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
62 lines (52 loc) · 982 Bytes
/
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
package main
import (
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/rubenv/sql-migrate"
"os"
"path/filepath"
"github.com/symbiont-io/detsys-testkit/src/lib"
)
var version = "unknown"
func help() {
fmt.Printf(`
Usage:
detsys-db [up|down]
Flags:
-v, --version version for detsys-db
`)
os.Exit(1)
}
func main() {
if len(os.Args) != 2 {
help()
}
var dir migrate.MigrationDirection
switch os.Args[1] {
case "up":
dir = migrate.Up
case "down":
dir = migrate.Down
case "-v":
fmt.Println(version)
os.Exit(0)
case "--version":
fmt.Println(version)
os.Exit(0)
default:
help()
}
path, err := os.Executable()
if err != nil {
panic(err)
}
migrations := &migrate.FileMigrationSource{
Dir: fmt.Sprintf("%s/../migrations", filepath.Dir(path)),
}
db := lib.OpenDB()
n, err := migrate.Exec(db, "sqlite3", migrations, dir)
if err != nil {
panic(fmt.Sprintf("%s\n%s", err, migrations))
}
fmt.Printf("Applied %d migrations!\n", n)
}