Checking non-exported migration method

This commit is contained in:
dimag 2016-08-10 21:25:36 +03:00
parent 583921e8bf
commit 6909594c0b
4 changed files with 38 additions and 3 deletions

View File

@ -235,6 +235,27 @@ func TestMigrate(t *testing.T) {
Errors: []error{gomethods.MissingMethodError("v001_non_existing_method_up")},
},
},
{
name: "v0 -> v1: not exported 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_not_exported_method_up
V001_init_users_up
`),
},
expectedResult: ExpectedMigrationResult{
Organizations: []Organization{},
Organizations_v2: []Organization_v2{},
Users: []User{},
Errors: []error{gomethods.MethodNotExportedError("v001_not_exported_method_up")},
},
},
{
name: "v0 -> v1: wrong signature method aborts migration",
file: file.File{

View File

@ -140,6 +140,10 @@ func (r *SampleMongoDbMigrator) V002_change_user_cleo_to_cleopatra_down(session
}
// Wrong signature methods for testing
func (r *SampleMongoDbMigrator) v001_not_exported_method_up(session *mgo.Session) error {
return nil
}
func (r *SampleMongoDbMigrator) V001_method_with_wrong_signature_up(s string) error {
return nil
}

View File

@ -20,6 +20,12 @@ func (e WrongMethodSignatureError) Error() string {
return fmt.Sprintf("Method %s has wrong signature", string(e))
}
type MethodNotExportedError string
func (e MethodNotExportedError) Error() string {
return fmt.Sprintf("Method %s is not exported", string(e))
}
type MethodInvocationFailedError struct {
MethodName string
Err error

View File

@ -142,14 +142,18 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
}
func (driver *Driver) Validate(methodName string) error {
method := reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName)
if !method.IsValid() {
methodWithReceiver, ok := reflect.TypeOf(driver.methodsReceiver).MethodByName(methodName)
if !ok {
return gomethods.MissingMethodError(methodName)
}
if methodWithReceiver.PkgPath != "" {
return gomethods.MethodNotExportedError(methodName)
}
methodFunc := reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName)
methodTemplate := func(*mgo.Session) error { return nil }
if method.Type() != reflect.TypeOf(methodTemplate) {
if methodFunc.Type() != reflect.TypeOf(methodTemplate) {
return gomethods.WrongMethodSignatureError(methodName)
}