/
migrate_history.go
70 lines (58 loc) · 2.29 KB
/
migrate_history.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
/*
Copyright © 2023 Adharsh M dev@adharsh.in
*/
package migrator
import (
"fmt"
"log"
"strings"
sqlmigrator "github.com/adharshmk96/stk/pkg/sqlMigrator"
"github.com/adharshmk96/stk/pkg/sqlMigrator/dbrepo"
"github.com/spf13/cobra"
)
func displayMigrationHistory(history []*sqlmigrator.MigrationDBEntry) {
// Define max column widths
numberWidth := len("number")
nameWidth := len("name")
directionWidth := len("direction")
createdWidth := len("created")
// Find the maximum width needed for each column based on data
for _, entry := range history {
if len(entry.Name) > nameWidth {
nameWidth = len(entry.Name)
}
if len(entry.Direction) > directionWidth {
directionWidth = len(entry.Direction)
}
if len(entry.Created.String()) > createdWidth {
createdWidth = len(entry.Created.String())
}
}
// Print header
fmt.Println("Migration History")
fmt.Println("-----------------")
fmt.Printf("| %-"+fmt.Sprintf("%d", numberWidth)+"s | %-"+fmt.Sprintf("%d", nameWidth)+"s | %-"+fmt.Sprintf("%d", directionWidth)+"s | %-"+fmt.Sprintf("%d", createdWidth)+"s |\n", "number", "name", "direction", "created")
fmt.Printf("| %-"+fmt.Sprintf("%d", numberWidth)+"s | %-"+fmt.Sprintf("%d", nameWidth)+"s | %-"+fmt.Sprintf("%d", directionWidth)+"s | %-"+fmt.Sprintf("%d", createdWidth)+"s |\n", strings.Repeat("-", numberWidth), strings.Repeat("-", nameWidth), strings.Repeat("-", directionWidth), strings.Repeat("-", createdWidth))
// Print entries
for _, entry := range history {
fmt.Printf("| %-"+fmt.Sprintf("%d", numberWidth)+"d | %-"+fmt.Sprintf("%d", nameWidth)+"s | %-"+fmt.Sprintf("%d", directionWidth)+"s | %-"+fmt.Sprintf("%d", createdWidth)+"s |\n", entry.Number, entry.Name, entry.Direction, entry.Created.String())
}
}
// historyCmd represents the mkconfig command
var HistoryCmd = &cobra.Command{
Use: "history",
Short: "view the migration history of the database.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
workDir, dbType, logFile := sqlmigrator.DefaultContextConfig()
ctx := sqlmigrator.NewContext(workDir, dbType, logFile, false)
dbRepo := dbrepo.SelectDBRepo(dbType)
migrator := sqlmigrator.NewMigrator(dbRepo)
history, err := migrator.MigrationHistory(ctx)
if err != nil {
log.Fatal(err)
return
}
displayMigrationHistory(history)
},
}