MCPcopy
hub / github.com/rubenv/sql-migrate / Run

Method Run

sql-migrate/command_redo.go:33–91  ·  view source on GitHub ↗
(args []string)

Source from the content-addressed store, hash-verified

31}
32
33func (c *RedoCommand) Run(args []string) int {
34 var dryrun bool
35
36 cmdFlags := flag.NewFlagSet("redo", flag.ContinueOnError)
37 cmdFlags.Usage = func() { ui.Output(c.Help()) }
38 cmdFlags.BoolVar(&dryrun, "dryrun", false, "Don't apply migrations, just print them.")
39 ConfigFlags(cmdFlags)
40
41 if err := cmdFlags.Parse(args); err != nil {
42 return 1
43 }
44
45 env, err := GetEnvironment()
46 if err != nil {
47 ui.Error(fmt.Sprintf("Could not parse config: %s", err))
48 return 1
49 }
50
51 db, dialect, err := GetConnection(env)
52 if err != nil {
53 ui.Error(err.Error())
54 return 1
55 }
56 defer db.Close()
57
58 source := migrate.FileMigrationSource{
59 Dir: env.Dir,
60 }
61
62 migrations, _, err := migrate.PlanMigration(db, dialect, source, migrate.Down, 1)
63 if err != nil {
64 ui.Error(fmt.Sprintf("Migration (redo) failed: %v", err))
65 return 1
66 } else if len(migrations) == 0 {
67 ui.Output("Nothing to do!")
68 return 0
69 }
70
71 if dryrun {
72 PrintMigration(migrations[0], migrate.Down)
73 PrintMigration(migrations[0], migrate.Up)
74 } else {
75 _, err := migrate.ExecMax(db, dialect, source, migrate.Down, 1)
76 if err != nil {
77 ui.Error(fmt.Sprintf("Migration (down) failed: %s", err))
78 return 1
79 }
80
81 _, err = migrate.ExecMax(db, dialect, source, migrate.Up, 1)
82 if err != nil {
83 ui.Error(fmt.Sprintf("Migration (up) failed: %s", err))
84 return 1
85 }
86
87 ui.Output(fmt.Sprintf("Reapplied migration %s.", migrations[0].Id))
88 }
89
90 return 0

Callers

nothing calls this directly

Calls 8

HelpMethod · 0.95
ConfigFlagsFunction · 0.85
GetEnvironmentFunction · 0.85
GetConnectionFunction · 0.85
PrintMigrationFunction · 0.85
PlanMigrationMethod · 0.80
ExecMaxMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected