- Fixed test with not-exported method identified as missing: unified the 2 errors into one

- Fixed error message formatting
- Ensuring uniqueness of the version field (added unique index)
This commit is contained in:
dimag 2017-01-07 16:08:09 +02:00
parent 0f28b01383
commit 914c38e379
4 changed files with 23 additions and 18 deletions

View File

@ -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")},
},
},
{
@ -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")},
},
},
{

View File

@ -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 {
@ -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 {

View File

@ -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
@ -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",

View File

@ -59,7 +59,7 @@ func init() {
}
type DbMigration struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Id bson.ObjectId `bson:"_id"`
Version uint64 `bson:"version"`
}
@ -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}
@ -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)
@ -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)})