-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (138 loc) · 3.55 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package migrate
import (
"database/sql"
"fmt"
"github.com/fatih/color"
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/postgres"
_ "github.com/golang-migrate/migrate/source/file" // needed for golang-migrate
_ "github.com/lib/pq" // needed for golang-migrate
"github.com/manifoldco/promptui"
"io"
"os"
)
const (
optionUp = "Up - all versions"
optionDown = "Down - all versions"
optionDrop = "Drop - all tables/indexes"
optionForce = "Force - specific version"
optionFullReset = "Reset - force first version & drop all tables/indexes"
optionNothing = "Do nothing - exit"
)
type migrationInstance interface {
Up() error
Down() error
Drop() error
Force(int) error
Version() (uint, bool, error)
Close() (error, error)
}
type settings struct {
connection *sql.DB
dbName string
migrationsFolder string
}
func handleError(err error) {
if err != nil {
color.Red(err.Error())
}
}
// CLI starts the cli with the specified settings
func CLI(connection *sql.DB, dbName string, migrationsFolder string) {
s := settings{connection, dbName, migrationsFolder}
m, err := getMigrateInstance(s)
defer m.Close()
handleError(err)
err = printVersion(m)
handleError(err)
startPrompt(m)
err = printVersion(m)
handleError(err)
os.Exit(0)
}
func executeOption(r io.Reader, m migrationInstance, optionKey string) error {
var err error
switch optionKey {
case optionUp:
err = m.Up()
if err == migrate.ErrNoChange {
msgOnSuccess(nil, "Already up-to-date")
return nil
}
msgOnSuccess(err, "Successfully migrated to latest version")
case optionDown:
err = m.Down()
if err == migrate.ErrNoChange {
msgOnSuccess(nil, "Already on lowest version")
return nil
}
msgOnSuccess(err, "Successfully migrated to lowest version")
case optionDrop:
err = m.Drop()
msgOnSuccess(err, "Successfully dropped tables and indexes")
case optionForce:
var v int
fmt.Print("Migrate to which version? ")
_, err = fmt.Fscanf(r, "%d", &v)
if err != nil {
return err
}
err = m.Force(v)
msgOnSuccess(err, "Successfully migrated to forced version")
case optionFullReset:
err = m.Force(0)
if err != nil {
return err
}
err = m.Drop()
msgOnSuccess(err, "Successfully dropped tables/indexes and forced version")
case optionNothing:
return nil
}
return err
}
func getMigrateInstance(s settings) (migrationInstance, error) {
driver, err := postgres.WithInstance(s.connection, &postgres.Config{})
if err != nil {
return nil, err
}
m, err := migrate.NewWithDatabaseInstance("file://"+s.migrationsFolder, s.dbName, driver)
if err != nil {
return nil, err
}
return m, nil
}
func startPrompt(m migrationInstance) {
selectedOption, err := promptSelect()
if err = executeOption(os.Stdin, m, selectedOption); err != nil {
color.Red(err.Error())
startPrompt(m)
}
}
func promptSelect() (string, error) {
prompt := promptui.Select{
Label: "Choose option",
Items: []string{optionUp, optionDown, optionDrop, optionForce, optionFullReset, optionNothing},
}
_, result, err := prompt.Run()
if err != nil {
return "", err
}
return result, nil
}
func printVersion(m migrationInstance) error {
version, _, err := m.Version()
if err != nil && err != migrate.ErrNilVersion {
return err
}
msg := fmt.Sprintf("Schema is at v%d", version)
cyan := color.New(color.FgCyan).SprintFunc()
fmt.Printf("%s %s\n\n", cyan("Status:"), msg)
return nil
}
func msgOnSuccess(err error, msg string) {
if err != nil {
return
}
color.Green(msg)
}