Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0eecb74

Browse files
committed
Added explicit migration method signature validation
1 parent b7b75f3 commit 0eecb74

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

driver/gomethods/gomethods_migrator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (e MissingMethodError) Error() string { return "Non existing migrate method
1717
type WrongMethodSignatureError string
1818

1919
func (e WrongMethodSignatureError) Error() string {
20-
return fmt.Sprintf("Method %s has wrong signature", e)
20+
return fmt.Sprintf("Method %s has wrong signature", string(e))
2121
}
2222

2323
type MethodInvocationFailedError struct {
@@ -30,7 +30,7 @@ func (e *MethodInvocationFailedError) Error() string {
3030
}
3131

3232
type MigrationMethodInvoker interface {
33-
IsValid(methodName string) bool
33+
Validate(methodName string) error
3434
Invoke(methodName string) error
3535
}
3636

@@ -66,8 +66,10 @@ func (m *Migrator) Migrate(f file.File, pipe chan interface{}) error {
6666
// on failure, try to rollback methods in this migration
6767
for j := i - 1; j >= 0; j-- {
6868
rollbackToMethodName := getRollbackToMethod(methods[j])
69-
if rollbackToMethodName == "" ||
70-
!m.MethodInvoker.IsValid(rollbackToMethodName) {
69+
if rollbackToMethodName == "" {
70+
continue
71+
}
72+
if err := m.MethodInvoker.Validate(rollbackToMethodName); err != nil {
7173
continue
7274
}
7375

@@ -140,8 +142,8 @@ func (m *Migrator) getMigrationMethods(f file.File) (methods []string, err error
140142
}
141143

142144
methodName := line
143-
if !m.MethodInvoker.IsValid(methodName) {
144-
return nil, MissingMethodError(methodName)
145+
if err := m.MethodInvoker.Validate(methodName); err != nil {
146+
return nil, err
145147
}
146148

147149
methods = append(methods, methodName)

driver/gomethods/gomethods_migrator_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ type FakeGoMethodsInvoker struct {
1414
InvokedMethods []string
1515
}
1616

17-
func (invoker *FakeGoMethodsInvoker) IsValid(methodName string) bool {
17+
func (invoker *FakeGoMethodsInvoker) Validate(methodName string) error {
1818
if methodName == "V001_some_non_existing_method_up" {
19-
return false
19+
return MissingMethodError(methodName)
2020
}
2121

22-
return true
22+
return nil
2323
}
2424

2525
func (invoker *FakeGoMethodsInvoker) Invoke(methodName string) error {
@@ -30,9 +30,8 @@ func (invoker *FakeGoMethodsInvoker) Invoke(methodName string) error {
3030
MethodName: methodName,
3131
Err: SomeError{},
3232
}
33-
} else {
34-
return nil
3533
}
34+
return nil
3635
}
3736

3837
type SomeError struct{}

driver/gomethods/mongodb/mongodb.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,19 @@ func (driver *MongoDbGoMethodsDriver) Migrate(f file.File, pipe chan interface{}
141141
}
142142
}
143143

144-
func (driver *MongoDbGoMethodsDriver) IsValid(methodName string) bool {
145-
return reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName).IsValid()
144+
func (driver *MongoDbGoMethodsDriver) Validate(methodName string) error {
145+
method := reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName)
146+
if !method.IsValid() {
147+
return gomethods.MissingMethodError(methodName)
148+
}
149+
150+
methodTemplate := func(*mgo.Session) error { return nil }
151+
152+
if method.Type() != reflect.TypeOf(methodTemplate) {
153+
return gomethods.WrongMethodSignatureError(methodName)
154+
}
155+
156+
return nil
146157
}
147158

148159
func (driver *MongoDbGoMethodsDriver) Invoke(methodName string) error {

driver/gomethods/mongodb_example/methods_receiver.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,12 @@ func (r *MyMgoMethodsReceiver) V002_change_user_cleo_to_cleopatra_down(session *
137137

138138
return c.Update(colQuerier, change)
139139
}
140+
141+
// Wrong signature methods for testing
142+
func (r *MyMgoMethodsReceiver) V001_method_with_wrong_signature_up(s string) error {
143+
return nil
144+
}
145+
146+
func (r *MyMgoMethodsReceiver) V001_method_with_wrong_signature_down(session *mgo.Session) (bool, error) {
147+
return true, nil
148+
}

driver/gomethods/mongodb_example/mongodb_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func TestMigrate(t *testing.T) {
215215
},
216216
},
217217
{
218-
name: "v0 -> v1: with error",
218+
name: "v0 -> v1: missing method aborts migration",
219219
file: file.File{
220220
Path: "/foobar",
221221
FileName: "001_foobar.up.gm",
@@ -235,6 +235,48 @@ func TestMigrate(t *testing.T) {
235235
Errors: []error{gomethods.MissingMethodError("v001_non_existing_method_up")},
236236
},
237237
},
238+
{
239+
name: "v0 -> v1: wrong signature method aborts migration",
240+
file: file.File{
241+
Path: "/foobar",
242+
FileName: "001_foobar.up.gm",
243+
Version: 1,
244+
Name: "foobar",
245+
Direction: direction.Up,
246+
Content: []byte(`
247+
V001_init_organizations_up
248+
V001_method_with_wrong_signature_up
249+
V001_init_users_up
250+
`),
251+
},
252+
expectedResult: ExpectedMigrationResult{
253+
Organizations: []Organization{},
254+
Organizations_v2: []Organization_v2{},
255+
Users: []User{},
256+
Errors: []error{gomethods.WrongMethodSignatureError("V001_method_with_wrong_signature_up")},
257+
},
258+
},
259+
{
260+
name: "v1 -> v0: wrong signature method aborts migration",
261+
file: file.File{
262+
Path: "/foobar",
263+
FileName: "001_foobar.down.gm",
264+
Version: 1,
265+
Name: "foobar",
266+
Direction: direction.Down,
267+
Content: []byte(`
268+
V001_init_users_down
269+
V001_method_with_wrong_signature_down
270+
V001_init_organizations_down
271+
`),
272+
},
273+
expectedResult: ExpectedMigrationResult{
274+
Organizations: []Organization{},
275+
Organizations_v2: []Organization_v2{},
276+
Users: []User{},
277+
Errors: []error{gomethods.WrongMethodSignatureError("V001_method_with_wrong_signature_down")},
278+
},
279+
},
238280
}
239281

240282
for _, m := range migrations {

0 commit comments

Comments
 (0)