Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit 2361413

Browse files
committed
Refactor variable names
1 parent 8e65ca3 commit 2361413

6 files changed

Lines changed: 77 additions & 67 deletions

File tree

commander/commander.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/juju/errors"
77
"github.com/urfave/cli"
8+
"github.com/wallester/migrate/direction"
89
"github.com/wallester/migrate/flag"
910
"github.com/wallester/migrate/migrator"
1011
)
@@ -51,7 +52,7 @@ func (cmd *commander) Up(c *cli.Context) error {
5152
return errors.Annotate(err, "parsing parameters failed")
5253
}
5354

54-
if err := cmd.m.Migrate(args.path, args.url, true, args.steps); err != nil {
55+
if err := cmd.m.Migrate(args.path, args.url, direction.Up, args.steps); err != nil {
5556
return errors.Annotate(err, "migrating up failed")
5657
}
5758

@@ -65,7 +66,7 @@ func (cmd *commander) Down(c *cli.Context) error {
6566
return errors.Annotate(err, "parsing parameters failed")
6667
}
6768

68-
if err := cmd.m.Migrate(args.path, args.url, false, args.steps); err != nil {
69+
if err := cmd.m.Migrate(args.path, args.url, direction.Down, args.steps); err != nil {
6970
return errors.Annotate(err, "migrating down failed")
7071
}
7172

commander/commander_test.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ type CommanderTestSuite struct {
1616
migratorMock *migrator.Mock
1717
instance Commander
1818
expectedErr error
19-
set *flag.FlagSet
20-
c *cli.Context
19+
flagSet *flag.FlagSet
20+
ctx *cli.Context
2121
}
2222

2323
func (suite *CommanderTestSuite) SetupTest() {
2424
suite.migratorMock = &migrator.Mock{}
2525
suite.instance = New(suite.migratorMock)
2626
suite.expectedErr = errors.New("failure")
27-
suite.set = flag.NewFlagSet("test", 0)
28-
suite.c = cli.NewContext(nil, suite.set, nil)
27+
suite.flagSet = flag.NewFlagSet("test", 0)
28+
suite.ctx = cli.NewContext(nil, suite.flagSet, nil)
2929
}
3030

3131
func Test_Commander_TestSuite(t *testing.T) {
@@ -45,7 +45,7 @@ func (suite *CommanderTestSuite) Test_New_ReturnsInstance_InCaseOfSuccess() {
4545

4646
func (suite *CommanderTestSuite) Test_Create_ReturnsError_InCaseOfMissingArgument() {
4747
// Act
48-
err := suite.instance.Create(suite.c)
48+
err := suite.instance.Create(suite.ctx)
4949

5050
// Assert
5151
suite.NotNil(err)
@@ -54,12 +54,12 @@ func (suite *CommanderTestSuite) Test_Create_ReturnsError_InCaseOfMissingArgumen
5454

5555
func (suite *CommanderTestSuite) Test_Create_ReturnsError_InCaseOfMissingFlag() {
5656
// Arrange
57-
if err := suite.set.Parse([]string{"create_table_users"}); err != nil {
57+
if err := suite.flagSet.Parse([]string{"create_table_users"}); err != nil {
5858
suite.FailNow(err.Error())
5959
}
6060

6161
// Act
62-
err := suite.instance.Create(suite.c)
62+
err := suite.instance.Create(suite.ctx)
6363

6464
// Assert
6565
suite.NotNil(err)
@@ -68,15 +68,15 @@ func (suite *CommanderTestSuite) Test_Create_ReturnsError_InCaseOfMissingFlag()
6868

6969
func (suite *CommanderTestSuite) Test_Create_ReturnsError_InCaseOfMigratorError() {
7070
// Arrange
71-
suite.set.String("path", "", "")
72-
if err := suite.set.Parse([]string{"--path", "testdata", "create_table_users"}); err != nil {
71+
suite.flagSet.String("path", "", "")
72+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "create_table_users"}); err != nil {
7373
suite.FailNow(err.Error())
7474
}
7575
pair := &file.Pair{}
7676
suite.migratorMock.On("Create", "create_table_users", "testdata").Return(pair, suite.expectedErr).Once()
7777

7878
// Act
79-
err := suite.instance.Create(suite.c)
79+
err := suite.instance.Create(suite.ctx)
8080

8181
// Assert
8282
suite.NotNil(err)
@@ -85,23 +85,23 @@ func (suite *CommanderTestSuite) Test_Create_ReturnsError_InCaseOfMigratorError(
8585

8686
func (suite *CommanderTestSuite) Test_Create_ReturnsNil_InCaseOfSuccess() {
8787
// Arrange
88-
suite.set.String("path", "", "")
89-
if err := suite.set.Parse([]string{"--path", "testdata", "create_table_users"}); err != nil {
88+
suite.flagSet.String("path", "", "")
89+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "create_table_users"}); err != nil {
9090
suite.FailNow(err.Error())
9191
}
9292
pair := &file.Pair{}
9393
suite.migratorMock.On("Create", "create_table_users", "testdata").Return(pair, nil).Once()
9494

9595
// Act
96-
err := suite.instance.Create(suite.c)
96+
err := suite.instance.Create(suite.ctx)
9797

9898
// Assert
9999
suite.Nil(err)
100100
}
101101

102102
func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfMissingPath() {
103103
// Act
104-
err := suite.instance.Up(suite.c)
104+
err := suite.instance.Up(suite.ctx)
105105

106106
// Assert
107107
suite.NotNil(err)
@@ -110,13 +110,13 @@ func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfMissingPath() {
110110

111111
func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfMissingURL() {
112112
// Arrange
113-
suite.set.String("path", "", "")
114-
if err := suite.set.Parse([]string{"--path", "testdata"}); err != nil {
113+
suite.flagSet.String("path", "", "")
114+
if err := suite.flagSet.Parse([]string{"--path", "testdata"}); err != nil {
115115
suite.FailNow(err.Error())
116116
}
117117

118118
// Act
119-
err := suite.instance.Up(suite.c)
119+
err := suite.instance.Up(suite.ctx)
120120

121121
// Assert
122122
suite.NotNil(err)
@@ -125,15 +125,15 @@ func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfMissingURL() {
125125

126126
func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfMigratorError() {
127127
// Arrange
128-
suite.set.String("path", "", "")
129-
suite.set.String("url", "", "")
130-
if err := suite.set.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
128+
suite.flagSet.String("path", "", "")
129+
suite.flagSet.String("url", "", "")
130+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
131131
suite.FailNow(err.Error())
132132
}
133133
suite.migratorMock.On("Migrate", "testdata", "connectionurl", true, 0).Return(suite.expectedErr).Once()
134134

135135
// Act
136-
err := suite.instance.Up(suite.c)
136+
err := suite.instance.Up(suite.ctx)
137137

138138
// Assert
139139
suite.NotNil(err)
@@ -142,14 +142,14 @@ func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfMigratorError() {
142142

143143
func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfInvalidArgument() {
144144
// Arrange
145-
suite.set.String("path", "", "")
146-
suite.set.String("url", "", "")
147-
if err := suite.set.Parse([]string{"--path", "testdata", "--url", "connectionurl", "foobar"}); err != nil {
145+
suite.flagSet.String("path", "", "")
146+
suite.flagSet.String("url", "", "")
147+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "--url", "connectionurl", "foobar"}); err != nil {
148148
suite.FailNow(err.Error())
149149
}
150150

151151
// Act
152-
err := suite.instance.Up(suite.c)
152+
err := suite.instance.Up(suite.ctx)
153153

154154
// Assert
155155
suite.NotNil(err)
@@ -158,39 +158,39 @@ func (suite *CommanderTestSuite) Test_Up_ReturnError_InCaseOfInvalidArgument() {
158158

159159
func (suite *CommanderTestSuite) Test_Up_ReturnNil_InCaseOfSuccess() {
160160
// Arrange
161-
suite.set.String("path", "", "")
162-
suite.set.String("url", "", "")
163-
if err := suite.set.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
161+
suite.flagSet.String("path", "", "")
162+
suite.flagSet.String("url", "", "")
163+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
164164
suite.FailNow(err.Error())
165165
}
166166
suite.migratorMock.On("Migrate", "testdata", "connectionurl", true, 0).Return(nil).Once()
167167

168168
// Act
169-
err := suite.instance.Up(suite.c)
169+
err := suite.instance.Up(suite.ctx)
170170

171171
// Assert
172172
suite.Nil(err)
173173
}
174174

175175
func (suite *CommanderTestSuite) Test_Up_ReturnNil_InCaseOfArgumentN() {
176176
// Arrange
177-
suite.set.String("path", "", "")
178-
suite.set.String("url", "", "")
179-
if err := suite.set.Parse([]string{"--path", "testdata", "--url", "connectionurl", "10"}); err != nil {
177+
suite.flagSet.String("path", "", "")
178+
suite.flagSet.String("url", "", "")
179+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "--url", "connectionurl", "10"}); err != nil {
180180
suite.FailNow(err.Error())
181181
}
182182
suite.migratorMock.On("Migrate", "testdata", "connectionurl", true, 10).Return(nil).Once()
183183

184184
// Act
185-
err := suite.instance.Up(suite.c)
185+
err := suite.instance.Up(suite.ctx)
186186

187187
// Assert
188188
suite.Nil(err)
189189
}
190190

191191
func (suite *CommanderTestSuite) Test_Down_ReturnError_InCaseOfMissingPath() {
192192
// Act
193-
err := suite.instance.Down(suite.c)
193+
err := suite.instance.Down(suite.ctx)
194194

195195
// Assert
196196
suite.NotNil(err)
@@ -199,13 +199,13 @@ func (suite *CommanderTestSuite) Test_Down_ReturnError_InCaseOfMissingPath() {
199199

200200
func (suite *CommanderTestSuite) Test_Down_ReturnError_InCaseOfMissingURL() {
201201
// Arrange
202-
suite.set.String("path", "", "")
203-
if err := suite.set.Parse([]string{"--path", "testdata"}); err != nil {
202+
suite.flagSet.String("path", "", "")
203+
if err := suite.flagSet.Parse([]string{"--path", "testdata"}); err != nil {
204204
suite.FailNow(err.Error())
205205
}
206206

207207
// Act
208-
err := suite.instance.Down(suite.c)
208+
err := suite.instance.Down(suite.ctx)
209209

210210
// Assert
211211
suite.NotNil(err)
@@ -214,15 +214,15 @@ func (suite *CommanderTestSuite) Test_Down_ReturnError_InCaseOfMissingURL() {
214214

215215
func (suite *CommanderTestSuite) Test_Down_ReturnError_InCaseOfMigratorError() {
216216
// Arrange
217-
suite.set.String("path", "", "")
218-
suite.set.String("url", "", "")
219-
if err := suite.set.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
217+
suite.flagSet.String("path", "", "")
218+
suite.flagSet.String("url", "", "")
219+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
220220
suite.FailNow(err.Error())
221221
}
222222
suite.migratorMock.On("Migrate", "testdata", "connectionurl", false, 0).Return(suite.expectedErr).Once()
223223

224224
// Act
225-
err := suite.instance.Down(suite.c)
225+
err := suite.instance.Down(suite.ctx)
226226

227227
// Assert
228228
suite.NotNil(err)
@@ -231,15 +231,15 @@ func (suite *CommanderTestSuite) Test_Down_ReturnError_InCaseOfMigratorError() {
231231

232232
func (suite *CommanderTestSuite) Test_Down_ReturnNil_InCaseOfSuccess() {
233233
// Arrange
234-
suite.set.String("path", "", "")
235-
suite.set.String("url", "", "")
236-
if err := suite.set.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
234+
suite.flagSet.String("path", "", "")
235+
suite.flagSet.String("url", "", "")
236+
if err := suite.flagSet.Parse([]string{"--path", "testdata", "--url", "connectionurl"}); err != nil {
237237
suite.FailNow(err.Error())
238238
}
239239
suite.migratorMock.On("Migrate", "testdata", "connectionurl", false, 0).Return(nil).Once()
240240

241241
// Act
242-
err := suite.instance.Down(suite.c)
242+
err := suite.instance.Down(suite.ctx)
243243

244244
// Assert
245245
suite.Nil(err)

direction/direction.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package direction
2+
3+
const (
4+
Up = true
5+
Down = false
6+
)

driver/postgres/driver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/juju/errors"
88
_ "github.com/lib/pq" // import driver
9+
"github.com/wallester/migrate/direction"
910
"github.com/wallester/migrate/driver"
1011
"github.com/wallester/migrate/file"
1112
)
@@ -84,8 +85,8 @@ func (db *postgres) CreateMigrationsTable(ctx context.Context) error {
8485
}
8586

8687
var applyMigrationSQL = map[bool]string{
87-
true: "INSERT INTO schema_migrations(version) VALUES($1)",
88-
false: "DELETE FROM schema_migrations WHERE version = $1",
88+
direction.Up: "INSERT INTO schema_migrations(version) VALUES($1)",
89+
direction.Down: "DELETE FROM schema_migrations WHERE version = $1",
8990
}
9091

9192
// ApplyMigrations applies migrations to database

file/file.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/juju/errors"
11+
"github.com/wallester/migrate/direction"
1112
)
1213

1314
// File represents a migration file
@@ -48,11 +49,6 @@ func (a ByBase) Less(i, j int) bool {
4849
return a[i].Base < a[j].Base
4950
}
5051

51-
var filePrefix = map[bool]string{
52-
true: "up",
53-
false: "down",
54-
}
55-
5652
// FindByVersion finds a file from list by version
5753
func FindByVersion(version int64, files []File) *File {
5854
for _, file := range files {
@@ -64,6 +60,11 @@ func FindByVersion(version int64, files []File) *File {
6460
return nil
6561
}
6662

63+
var filePrefix = map[bool]string{
64+
direction.Up: "up",
65+
direction.Down: "down",
66+
}
67+
6768
// ListFiles lists migration files on a given path
6869
func ListFiles(path string, up bool) ([]File, error) {
6970
files, err := filepath.Glob(filepath.Join(path, "*_*."+filePrefix[up]+".sql"))

0 commit comments

Comments
 (0)