Skip to content

Commit

Permalink
- Fixed test with not-exported method identified as missing: unified …
Browse files Browse the repository at this point in the history
…the 2 errors into one

- Fixed error message formatting
- Ensuring uniqueness of the version field (added unique index)
  • Loading branch information
dimag-jfrog committed Jan 7, 2017
1 parent 32cb825 commit 8865262
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions driver/mongodb/example/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestMigrate(t *testing.T) {
Organizations: []Organization{},
Organizations_v2: []Organization_v2{},
Users: []User{},
Errors: []error{gomethods.MissingMethodError("v001_non_existing_method_up")},
Errors: []error{gomethods.MethodNotFoundError("v001_non_existing_method_up")},
},
},
{
Expand All @@ -253,7 +253,7 @@ func TestMigrate(t *testing.T) {
Organizations: []Organization{},
Organizations_v2: []Organization_v2{},
Users: []User{},
Errors: []error{gomethods.MethodNotExportedError("v001_not_exported_method_up")},
Errors: []error{gomethods.MethodNotFoundError("v001_not_exported_method_up")},
},
},
{
Expand Down
16 changes: 6 additions & 10 deletions driver/mongodb/gomethods/gomethods_migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ import (
"strings"
)

type MissingMethodError string
type MethodNotFoundError string

func (e MissingMethodError) Error() string { return "Non existing migrate method: " + string(e) }
func (e MethodNotFoundError) Error() string {
return fmt.Sprintf("Method '%s' was not found. It is either not existing or has not been exported (starts with lowercase).", string(e))
}

type WrongMethodSignatureError string

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))
return fmt.Sprintf("Method '%s' has wrong signature", string(e))
}

type MethodInvocationFailedError struct {
Expand All @@ -32,7 +28,7 @@ type MethodInvocationFailedError struct {
}

func (e *MethodInvocationFailedError) Error() string {
return fmt.Sprintf("Method %s returned an error: %v", e.MethodName, e.Error)
return fmt.Sprintf("Method '%s' returned an error: %v", e.MethodName, e.Err)
}

type MigrationMethodInvoker interface {
Expand Down
4 changes: 2 additions & 2 deletions driver/mongodb/gomethods/gomethods_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type FakeGoMethodsInvoker struct {

func (invoker *FakeGoMethodsInvoker) Validate(methodName string) error {
if methodName == "V001_some_non_existing_method_up" {
return MissingMethodError(methodName)
return MethodNotFoundError(methodName)
}

return nil
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestMigrate(t *testing.T) {
`),
},
expectedInvokedMethods: []string{},
expectedErrors: []error{MissingMethodError("V001_some_non_existing_method_up")},
expectedErrors: []error{MethodNotFoundError("V001_some_non_existing_method_up")},
},
{
name: "up migration: failing method stops execution",
Expand Down
17 changes: 13 additions & 4 deletions driver/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func init() {
}

type DbMigration struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Id bson.ObjectId `bson:"_id"`
Version uint64 `bson:"version"`
}

Expand All @@ -79,6 +79,15 @@ func (driver *Driver) Initialize(url string) error {
}
session.SetMode(mgo.Monotonic, true)

c := session.DB(driver.methodsReceiver.DbName()).C(MIGRATE_C)
err = c.EnsureIndex(mgo.Index{
Key: []string{"version"},
Unique: true,
})
if err != nil {
return err
}

driver.Session = session
driver.migrator = gomethods.Migrator{MethodInvoker: driver}

Expand Down Expand Up @@ -144,10 +153,10 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
func (driver *Driver) Validate(methodName string) error {
methodWithReceiver, ok := reflect.TypeOf(driver.methodsReceiver).MethodByName(methodName)
if !ok {
return gomethods.MissingMethodError(methodName)
return gomethods.MethodNotFoundError(methodName)
}
if methodWithReceiver.PkgPath != "" {
return gomethods.MethodNotExportedError(methodName)
return gomethods.MethodNotFoundError(methodName)
}

methodFunc := reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName)
Expand All @@ -164,7 +173,7 @@ func (driver *Driver) Invoke(methodName string) error {
name := methodName
migrateMethod := reflect.ValueOf(driver.methodsReceiver).MethodByName(name)
if !migrateMethod.IsValid() {
return gomethods.MissingMethodError(methodName)
return gomethods.MethodNotFoundError(methodName)
}

retValues := migrateMethod.Call([]reflect.Value{reflect.ValueOf(driver.Session)})
Expand Down

0 comments on commit 8865262

Please sign in to comment.