diff --git a/driver/mongodb/example/mongodb_test.go b/driver/mongodb/example/mongodb_test.go index 33cecb9bb..c4371bf6d 100644 --- a/driver/mongodb/example/mongodb_test.go +++ b/driver/mongodb/example/mongodb_test.go @@ -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")}, }, }, { diff --git a/driver/mongodb/gomethods/gomethods_migrator.go b/driver/mongodb/gomethods/gomethods_migrator.go index 225f757d2..96fc1d50b 100644 --- a/driver/mongodb/gomethods/gomethods_migrator.go +++ b/driver/mongodb/gomethods/gomethods_migrator.go @@ -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 { diff --git a/driver/mongodb/gomethods/gomethods_migrator_test.go b/driver/mongodb/gomethods/gomethods_migrator_test.go index fe6865e0b..298f803a6 100644 --- a/driver/mongodb/gomethods/gomethods_migrator_test.go +++ b/driver/mongodb/gomethods/gomethods_migrator_test.go @@ -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", diff --git a/driver/mongodb/mongodb.go b/driver/mongodb/mongodb.go index d077e98bf..026e68626 100644 --- a/driver/mongodb/mongodb.go +++ b/driver/mongodb/mongodb.go @@ -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)})