mirror of https://github.com/status-im/migrate.git
Added explicit migration method signature validation
This commit is contained in:
parent
866ff82ffe
commit
834a096bf8
|
@ -17,7 +17,7 @@ func (e MissingMethodError) Error() string { return "Non existing migrate method
|
|||
type WrongMethodSignatureError string
|
||||
|
||||
func (e WrongMethodSignatureError) Error() string {
|
||||
return fmt.Sprintf("Method %s has wrong signature", e)
|
||||
return fmt.Sprintf("Method %s has wrong signature", string(e))
|
||||
}
|
||||
|
||||
type MethodInvocationFailedError struct {
|
||||
|
@ -30,7 +30,7 @@ func (e *MethodInvocationFailedError) Error() string {
|
|||
}
|
||||
|
||||
type MigrationMethodInvoker interface {
|
||||
IsValid(methodName string) bool
|
||||
Validate(methodName string) error
|
||||
Invoke(methodName string) error
|
||||
}
|
||||
|
||||
|
@ -66,8 +66,10 @@ func (m *Migrator) Migrate(f file.File, pipe chan interface{}) error {
|
|||
// on failure, try to rollback methods in this migration
|
||||
for j := i - 1; j >= 0; j-- {
|
||||
rollbackToMethodName := getRollbackToMethod(methods[j])
|
||||
if rollbackToMethodName == "" ||
|
||||
!m.MethodInvoker.IsValid(rollbackToMethodName) {
|
||||
if rollbackToMethodName == "" {
|
||||
continue
|
||||
}
|
||||
if err := m.MethodInvoker.Validate(rollbackToMethodName); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -140,8 +142,8 @@ func (m *Migrator) getMigrationMethods(f file.File) (methods []string, err error
|
|||
}
|
||||
|
||||
methodName := line
|
||||
if !m.MethodInvoker.IsValid(methodName) {
|
||||
return nil, MissingMethodError(methodName)
|
||||
if err := m.MethodInvoker.Validate(methodName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
methods = append(methods, methodName)
|
||||
|
|
|
@ -14,12 +14,12 @@ type FakeGoMethodsInvoker struct {
|
|||
InvokedMethods []string
|
||||
}
|
||||
|
||||
func (invoker *FakeGoMethodsInvoker) IsValid(methodName string) bool {
|
||||
func (invoker *FakeGoMethodsInvoker) Validate(methodName string) error {
|
||||
if methodName == "V001_some_non_existing_method_up" {
|
||||
return false
|
||||
return MissingMethodError(methodName)
|
||||
}
|
||||
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (invoker *FakeGoMethodsInvoker) Invoke(methodName string) error {
|
||||
|
@ -30,9 +30,8 @@ func (invoker *FakeGoMethodsInvoker) Invoke(methodName string) error {
|
|||
MethodName: methodName,
|
||||
Err: SomeError{},
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type SomeError struct{}
|
||||
|
|
|
@ -141,8 +141,19 @@ func (driver *MongoDbGoMethodsDriver) Migrate(f file.File, pipe chan interface{}
|
|||
}
|
||||
}
|
||||
|
||||
func (driver *MongoDbGoMethodsDriver) IsValid(methodName string) bool {
|
||||
return reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName).IsValid()
|
||||
func (driver *MongoDbGoMethodsDriver) Validate(methodName string) error {
|
||||
method := reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName)
|
||||
if !method.IsValid() {
|
||||
return gomethods.MissingMethodError(methodName)
|
||||
}
|
||||
|
||||
methodTemplate := func(*mgo.Session) error { return nil }
|
||||
|
||||
if method.Type() != reflect.TypeOf(methodTemplate) {
|
||||
return gomethods.WrongMethodSignatureError(methodName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver *MongoDbGoMethodsDriver) Invoke(methodName string) error {
|
||||
|
|
|
@ -137,3 +137,12 @@ func (r *MyMgoMethodsReceiver) V002_change_user_cleo_to_cleopatra_down(session *
|
|||
|
||||
return c.Update(colQuerier, change)
|
||||
}
|
||||
|
||||
// Wrong signature methods for testing
|
||||
func (r *MyMgoMethodsReceiver) V001_method_with_wrong_signature_up(s string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MyMgoMethodsReceiver) V001_method_with_wrong_signature_down(session *mgo.Session) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
|
|
@ -215,7 +215,7 @@ func TestMigrate(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "v0 -> v1: with error",
|
||||
name: "v0 -> v1: missing method aborts migration",
|
||||
file: file.File{
|
||||
Path: "/foobar",
|
||||
FileName: "001_foobar.up.gm",
|
||||
|
@ -235,6 +235,48 @@ func TestMigrate(t *testing.T) {
|
|||
Errors: []error{gomethods.MissingMethodError("v001_non_existing_method_up")},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v0 -> v1: wrong signature method aborts migration",
|
||||
file: file.File{
|
||||
Path: "/foobar",
|
||||
FileName: "001_foobar.up.gm",
|
||||
Version: 1,
|
||||
Name: "foobar",
|
||||
Direction: direction.Up,
|
||||
Content: []byte(`
|
||||
V001_init_organizations_up
|
||||
V001_method_with_wrong_signature_up
|
||||
V001_init_users_up
|
||||
`),
|
||||
},
|
||||
expectedResult: ExpectedMigrationResult{
|
||||
Organizations: []Organization{},
|
||||
Organizations_v2: []Organization_v2{},
|
||||
Users: []User{},
|
||||
Errors: []error{gomethods.WrongMethodSignatureError("V001_method_with_wrong_signature_up")},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v1 -> v0: wrong signature method aborts migration",
|
||||
file: file.File{
|
||||
Path: "/foobar",
|
||||
FileName: "001_foobar.down.gm",
|
||||
Version: 1,
|
||||
Name: "foobar",
|
||||
Direction: direction.Down,
|
||||
Content: []byte(`
|
||||
V001_init_users_down
|
||||
V001_method_with_wrong_signature_down
|
||||
V001_init_organizations_down
|
||||
`),
|
||||
},
|
||||
expectedResult: ExpectedMigrationResult{
|
||||
Organizations: []Organization{},
|
||||
Organizations_v2: []Organization_v2{},
|
||||
Users: []User{},
|
||||
Errors: []error{gomethods.WrongMethodSignatureError("V001_method_with_wrong_signature_down")},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, m := range migrations {
|
||||
|
|
Loading…
Reference in New Issue