-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand_skip.go
76 lines (59 loc) · 1.59 KB
/
command_skip.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
package main
import (
"flag"
"fmt"
"strings"
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/viper"
)
type SkipCommand struct {
}
func (c *SkipCommand) Help() string {
helpText := `
Usage: evedbtool skip [options] ...
Set the database level to the most recent version available, without actually running the migrations.
Options:
-limit=0 Limit the number of migrations (0 = unlimited).
`
return strings.TrimSpace(helpText)
}
func (c *SkipCommand) Synopsis() string {
return "Sets the database level to the most recent version available, without running the migrations"
}
func (c *SkipCommand) Run(args []string) int {
var limit int
var dryrun bool
migrate.SetTable("migrations")
cmdFlags := flag.NewFlagSet("up", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&limit, "limit", 0, "Max number of migrations to skip.")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
err := SkipMigrations(migrate.Up, dryrun, limit)
if err != nil {
ui.Error(err.Error())
return 1
}
return 0
}
func SkipMigrations(dir migrate.MigrationDirection, dryrun bool, limit int) error {
db := getDB()
dialect := "mysql"
source := migrate.FileMigrationSource{
Dir: viper.GetString("migrations-dir"),
}
n, err := migrate.SkipMax(db, dialect, source, dir, limit)
if err != nil {
return fmt.Errorf("Migration failed: %s", err)
}
switch n {
case 0:
ui.Output("All migrations have already been applied")
case 1:
ui.Output("Skipped 1 migration")
default:
ui.Output(fmt.Sprintf("Skipped %d migrations", n))
}
return nil
}