-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand_install.go
61 lines (51 loc) · 1.47 KB
/
command_install.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
package main
import (
"flag"
"strings"
migrate "github.com/rubenv/sql-migrate"
)
type InstallCommand struct {
}
func (c *InstallCommand) Help() string {
helpText := `
Usage: evedbtool install [options] ...
Installs the base database and migrates to the most recent version available.
Options:
-limit=0 Limit the number of migrations (0 = unlimited).
-dryrun Don't apply migrations, just print them.
`
return strings.TrimSpace(helpText)
}
func (c *InstallCommand) Synopsis() string {
return "Installs the base database and migrates to the most recent version available."
}
func (c *InstallCommand) Run(args []string) int {
var limit int
var dryrun bool
cmdFlags := flag.NewFlagSet("up", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&limit, "limit", 0, "Max number of migrations to apply.")
cmdFlags.BoolVar(&dryrun, "dryrun", false, "Don't apply migrations, just print them.")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if !dryrun {
tables := GetNumberOfTables()
log.Info("Number of tables in DB: ", tables)
if tables == 0 {
log.Info("Database not initialized, installing...")
InstallBase()
} else {
log.Info("Base database already installed. Won't overwrite.")
}
} else {
log.Info("Dry run, not installing base.")
}
migrate.SetTable("migrations")
err := ApplyMigrations(migrate.Up, dryrun, limit)
if err != nil {
ui.Error(err.Error())
return 1
}
return 0
}