Run interactively executes the current runner with the provided args. The following commands are supported: - up - applies all migrations - down [n] - reverts the last n (default 1) applied migrations - history-sync - syncs the migrations table with the runner's migrations list
(args ...string)
| 40 | // - down [n] - reverts the last n (default 1) applied migrations |
| 41 | // - history-sync - syncs the migrations table with the runner's migrations list |
| 42 | func (r *MigrationsRunner) Run(args ...string) error { |
| 43 | if err := r.initMigrationsTable(); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | cmd := "up" |
| 48 | if len(args) > 0 { |
| 49 | cmd = args[0] |
| 50 | } |
| 51 | |
| 52 | switch cmd { |
| 53 | case "up": |
| 54 | applied, err := r.Up() |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | if len(applied) == 0 { |
| 60 | color.Green("No new migrations to apply.") |
| 61 | } else { |
| 62 | for _, file := range applied { |
| 63 | color.Green("Applied %s", file) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return nil |
| 68 | case "down": |
| 69 | toRevertCount := 1 |
| 70 | if len(args) > 1 { |
| 71 | toRevertCount = cast.ToInt(args[1]) |
| 72 | if toRevertCount < 0 { |
| 73 | // revert all applied migrations |
| 74 | toRevertCount = len(r.migrationsList.Items()) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | names, err := r.lastAppliedMigrations(toRevertCount) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | |
| 83 | confirm := osutils.YesNoPrompt(fmt.Sprintf( |
| 84 | "\n%v\nDo you really want to revert the last %d applied migration(s)?", |
| 85 | strings.Join(names, "\n"), |
| 86 | toRevertCount, |
| 87 | ), false) |
| 88 | if !confirm { |
| 89 | fmt.Println("The command has been cancelled") |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | reverted, err := r.Down(toRevertCount) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | if len(reverted) == 0 { |
| 99 | color.Green("No migrations to revert.") |