Skip to content

Commit fad15ab

Browse files
authored
Merge pull request #1 from webedx-spark/nedyalko/RHM-1969-rollback-alternative
[RHM-1969] Gloat should support rollback of migrations.
2 parents 3bc77a6 + 4e90e00 commit fad15ab

14 files changed

+582
-79
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
.PHONY: build
22
build:
33
@mkdir -p bin
4-
@go build -o bin/gloat github.com/gsamokovarov/gloat/cmd/gloat
4+
@go build -o bin/gloat github.com/webedx-spark/gloat/cmd/gloat
55

66
.PHONY: test
77
test:
88
@go test ./...
99

1010
.PHONY: test.sqlite
1111
test.sqlite:
12-
@env DATABASE_URL=sqlite3://:memory: go test ./...
12+
@env DATABASE_SRC=testdata/migrations/ DATABASE_URL=sqlite3://:memory: go test ./...
1313

1414
.PHONY: test.assets
1515
test.assets:

assets_test.go

Lines changed: 122 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gloat/main.go

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"net/url"
99
"os"
1010
"path/filepath"
11+
"strconv"
1112
"strings"
1213

13-
"github.com/gsamokovarov/gloat"
14+
"github.com/webedx-spark/gloat"
1415

1516
_ "github.com/go-sql-driver/mysql"
1617
_ "github.com/lib/pq"
@@ -22,13 +23,17 @@ const usage = `Usage gloat: [OPTION ...] [COMMAND ...]
2223
Gloat is a Go SQL migration utility.
2324
2425
Commands:
25-
new Create a new migration folder
26-
up Apply new migrations
27-
down Revert the last applied migration
26+
new Create a new migration folder
27+
up Apply new migrations
28+
down Revert the last applied migration
29+
to <version> Migrate to a given version (down to).
30+
latest Latest migration in the source.
31+
current Latest Applied migration.
32+
present List all present versions.
2833
2934
Options:
3035
-src The folder with migrations
31-
(default $DATABASE_SRC or db/migrations)
36+
(default $DATABASE_SRC or database/migrations)
3237
-url The database connection URL
3338
(default $DATABASE_URL)
3439
-help Show this message
@@ -56,6 +61,14 @@ func main() {
5661
err = downCmd(args)
5762
case "new":
5863
err = newCmd(args)
64+
case "to":
65+
err = migrateToCmd(args)
66+
case "latest":
67+
err = latestCmd(args)
68+
case "current":
69+
err = currentCmd(args)
70+
case "present":
71+
err = presentCmd(args)
5972
default:
6073
fmt.Fprintf(os.Stderr, usage)
6174
os.Exit(2)
@@ -97,6 +110,96 @@ func upCmd(args arguments) error {
97110
return nil
98111
}
99112

113+
func latestCmd(args arguments) error {
114+
gl, err := setupGloat(args)
115+
if err != nil {
116+
return err
117+
}
118+
119+
latest, err := gl.Latest()
120+
if err != nil {
121+
return err
122+
}
123+
124+
if latest != nil {
125+
fmt.Printf("%d", latest.Version)
126+
}
127+
return nil
128+
}
129+
130+
func presentCmd(args arguments) error {
131+
gl, err := setupGloat(args)
132+
if err != nil {
133+
return err
134+
}
135+
136+
migrations, err := gl.Present()
137+
if err != nil {
138+
return err
139+
}
140+
141+
migrations.Sort()
142+
143+
for i, m := range migrations {
144+
fmt.Printf("%d", m.Version)
145+
if i != len(migrations)-1 {
146+
fmt.Print(",")
147+
}
148+
}
149+
150+
return nil
151+
}
152+
153+
func currentCmd(args arguments) error {
154+
gl, err := setupGloat(args)
155+
if err != nil {
156+
return err
157+
}
158+
159+
current, err := gl.Current()
160+
if err != nil {
161+
return err
162+
}
163+
164+
if current != nil {
165+
fmt.Printf("%d", current.Version)
166+
}
167+
return nil
168+
}
169+
170+
func migrateToCmd(args arguments) error {
171+
gl, err := setupGloat(args)
172+
if err != nil {
173+
return err
174+
}
175+
if len(args.rest) < 2 {
176+
return errors.New("migrate to requires a version to migrate to")
177+
}
178+
179+
version, err := strconv.ParseInt(args.rest[1], 10, 64)
180+
if err != nil {
181+
return err
182+
}
183+
184+
migrations, err := gl.AppliedAfter(version)
185+
if err != nil {
186+
if err == gloat.ErrNotFound {
187+
return upCmd(args)
188+
}
189+
return err
190+
}
191+
192+
for _, migration := range migrations {
193+
fmt.Printf("\nReverting: %d...\n", migration.Version)
194+
195+
if err := gl.Revert(migration); err != nil {
196+
return err
197+
}
198+
}
199+
200+
return nil
201+
}
202+
100203
func downCmd(args arguments) error {
101204
gl, err := setupGloat(args)
102205
if err != nil {
@@ -109,7 +212,7 @@ func downCmd(args arguments) error {
109212
}
110213

111214
if migration == nil {
112-
fmt.Printf("No migrations to apply\n")
215+
fmt.Printf("No migrations to revert\n")
113216
return nil
114217
}
115218

@@ -163,7 +266,7 @@ func parseArguments() arguments {
163266

164267
srcDefault := os.Getenv("DATABASE_SRC")
165268
if srcDefault == "" {
166-
srcDefault = "db/migrations"
269+
srcDefault = "database/migrations"
167270
}
168271
srcUsage := `the folder with migrations`
169272

executor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"path/filepath"
66
"testing"
77

8-
"github.com/gsamokovarov/assert"
8+
"github.com/stretchr/testify/assert"
99
)
1010

1111
func TestSQLExecutor_Up(t *testing.T) {

0 commit comments

Comments
 (0)