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

Method Run

sql-migrate/command_status.go:36–113  ·  view source on GitHub ↗
(args []string)

Source from the content-addressed store, hash-verified

34}
35
36func (c *StatusCommand) Run(args []string) int {
37 cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
38 cmdFlags.Usage = func() { ui.Output(c.Help()) }
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 migrations, err := source.FindMigrations()
62 if err != nil {
63 ui.Error(err.Error())
64 return 1
65 }
66
67 records, err := migrate.GetMigrationRecords(db, dialect)
68 if err != nil {
69 ui.Error(err.Error())
70 return 1
71 }
72
73 table := tablewriter.NewWriter(os.Stdout)
74 table.SetHeader([]string{"Migration", "Applied"})
75 table.SetColWidth(60)
76
77 rows := make(map[string]*statusRow)
78
79 for _, m := range migrations {
80 rows[m.Id] = &statusRow{
81 Id: m.Id,
82 Migrated: false,
83 }
84 }
85
86 for _, r := range records {
87 if rows[r.Id] == nil {
88 ui.Warn(fmt.Sprintf("Could not find migration file: %v", r.Id))
89 continue
90 }
91
92 rows[r.Id].Migrated = true
93 rows[r.Id].AppliedAt = r.AppliedAt

Callers

nothing calls this directly

Calls 7

HelpMethod · 0.95
FindMigrationsMethod · 0.95
ConfigFlagsFunction · 0.85
GetEnvironmentFunction · 0.85
GetConnectionFunction · 0.85
GetMigrationRecordsMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected