- Refactored project structure to be consistent with original project

- Added docker support
This commit is contained in:
dimag 2016-08-10 17:15:17 +03:00
parent 0eecb74230
commit 61ecf2c096
7 changed files with 48 additions and 74 deletions

View File

@ -1,6 +1,6 @@
go: &go
image: golang
working_dir: /go/src/github.com/mattes/migrate
working_dir: /go/src/github.com/dimag-jfrog/migrate
volumes:
- $GOPATH:/go
go-test:
@ -11,6 +11,7 @@ go-test:
- mysql
- cassandra
- crate
- mongo
go-build:
<<: *go
command: sh -c 'go get -v && go build -ldflags ''-s'' -o migrater'
@ -27,3 +28,6 @@ cassandra:
image: cassandra:2.2
crate:
image: crate
mongo:
image: mongo

View File

@ -1,4 +1,4 @@
package mongodb_example
package example
import (
"testing"
@ -7,9 +7,10 @@ import (
"github.com/dimag-jfrog/migrate/migrate/direction"
"github.com/dimag-jfrog/migrate/driver"
"github.com/dimag-jfrog/migrate/driver/gomethods"
"github.com/dimag-jfrog/migrate/driver/gomethods/mongodb"
"github.com/dimag-jfrog/migrate/driver/mongodb"
"github.com/dimag-jfrog/migrate/driver/mongodb/gomethods"
pipep "github.com/dimag-jfrog/migrate/pipe"
"os"
"reflect"
"time"
)
@ -24,7 +25,7 @@ type ExpectedMigrationResult struct {
func RunMigrationAndAssertResult(
t *testing.T,
title string,
d *mongodb.MongoDbGoMethodsDriver,
d *mongodb.Driver,
file file.File,
expected *ExpectedMigrationResult) {
@ -70,7 +71,7 @@ func RunMigrationAndAssertResult(
t.Fatalf("Migration '%s': FAILED\nexpected users %v\nbut got %v", title, expected.Users, actualUsers)
}
t.Logf("Migration '%s': PASSED", title)
// t.Logf("Migration '%s': PASSED", title)
}
func TestMigrate(t *testing.T) {
@ -80,14 +81,13 @@ func TestMigrate(t *testing.T) {
}
}()
//host := os.Getenv("MONGODB_PORT_27017_TCP_ADDR")
//port := os.Getenv("MONGODB_PORT_27017_TCP_PORT")
host := "127.0.0.1"
port := "27017"
host := os.Getenv("MONGO_PORT_27017_TCP_ADDR")
port := os.Getenv("MONGO_PORT_27017_TCP_PORT")
driverUrl := "mongodb://" + host + ":" + port
d0 := driver.GetDriver("mongodb")
d, ok := d0.(*mongodb.MongoDbGoMethodsDriver)
d, ok := d0.(*mongodb.Driver)
if !ok {
t.Fatal("MongoDbGoMethodsDriver has not registered")
}

View File

@ -1,25 +1,26 @@
package mongodb_example
package example
import (
"github.com/dimag-jfrog/migrate/driver/gomethods"
_ "github.com/dimag-jfrog/migrate/driver/gomethods"
"github.com/dimag-jfrog/migrate/driver/gomethods/mongodb"
"github.com/dimag-jfrog/migrate/driver/mongodb/gomethods"
_ "github.com/dimag-jfrog/migrate/driver/mongodb/gomethods"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"time"
"github.com/dimag-jfrog/migrate/driver/mongodb"
)
type MyMgoMethodsReceiver struct {
type SampleMongoDbMigrator struct {
}
func (r *MyMgoMethodsReceiver) DbName() string {
func (r *SampleMongoDbMigrator) DbName() string {
return DB_NAME
}
var _ mongodb.MethodsReceiver = (*MyMgoMethodsReceiver)(nil)
var _ mongodb.MethodsReceiver = (*SampleMongoDbMigrator)(nil)
func init() {
gomethods.RegisterMethodsReceiverForDriver("mongodb", &MyMgoMethodsReceiver{})
gomethods.RegisterMethodsReceiverForDriver("mongodb", &SampleMongoDbMigrator{})
}
// Here goes the specific mongodb golang methods driver logic
@ -62,7 +63,7 @@ var UserIds []bson.ObjectId = []bson.ObjectId{
bson.NewObjectId(),
}
func (r *MyMgoMethodsReceiver) V001_init_organizations_up(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V001_init_organizations_up(session *mgo.Session) error {
date1, _ := time.Parse(SHORT_DATE_LAYOUT, "1994-Jul-05")
date2, _ := time.Parse(SHORT_DATE_LAYOUT, "1998-Sep-04")
date3, _ := time.Parse(SHORT_DATE_LAYOUT, "2008-Apr-28")
@ -82,11 +83,11 @@ func (r *MyMgoMethodsReceiver) V001_init_organizations_up(session *mgo.Session)
return nil
}
func (r *MyMgoMethodsReceiver) V001_init_organizations_down(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V001_init_organizations_down(session *mgo.Session) error {
return session.DB(DB_NAME).C(ORGANIZATIONS_C).DropCollection()
}
func (r *MyMgoMethodsReceiver) V001_init_users_up(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V001_init_users_up(session *mgo.Session) error {
users := []User{
{Id: UserIds[0], Name: "Alex"},
{Id: UserIds[1], Name: "Beatrice"},
@ -102,25 +103,25 @@ func (r *MyMgoMethodsReceiver) V001_init_users_up(session *mgo.Session) error {
return nil
}
func (r *MyMgoMethodsReceiver) V001_init_users_down(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V001_init_users_down(session *mgo.Session) error {
return session.DB(DB_NAME).C(USERS_C).DropCollection()
}
func (r *MyMgoMethodsReceiver) V002_organizations_rename_location_field_to_headquarters_up(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V002_organizations_rename_location_field_to_headquarters_up(session *mgo.Session) error {
c := session.DB(DB_NAME).C(ORGANIZATIONS_C)
_, err := c.UpdateAll(nil, bson.M{"$rename": bson.M{"location": "headquarters"}})
return err
}
func (r *MyMgoMethodsReceiver) V002_organizations_rename_location_field_to_headquarters_down(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V002_organizations_rename_location_field_to_headquarters_down(session *mgo.Session) error {
c := session.DB(DB_NAME).C(ORGANIZATIONS_C)
_, err := c.UpdateAll(nil, bson.M{"$rename": bson.M{"headquarters": "location"}})
return err
}
func (r *MyMgoMethodsReceiver) V002_change_user_cleo_to_cleopatra_up(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V002_change_user_cleo_to_cleopatra_up(session *mgo.Session) error {
c := session.DB(DB_NAME).C(USERS_C)
colQuerier := bson.M{"name": "Cleo"}
@ -129,7 +130,7 @@ func (r *MyMgoMethodsReceiver) V002_change_user_cleo_to_cleopatra_up(session *mg
return c.Update(colQuerier, change)
}
func (r *MyMgoMethodsReceiver) V002_change_user_cleo_to_cleopatra_down(session *mgo.Session) error {
func (r *SampleMongoDbMigrator) V002_change_user_cleo_to_cleopatra_down(session *mgo.Session) error {
c := session.DB(DB_NAME).C(USERS_C)
colQuerier := bson.M{"name": "Cleopatra"}
@ -139,10 +140,10 @@ func (r *MyMgoMethodsReceiver) V002_change_user_cleo_to_cleopatra_down(session *
}
// Wrong signature methods for testing
func (r *MyMgoMethodsReceiver) V001_method_with_wrong_signature_up(s string) error {
func (r *SampleMongoDbMigrator) 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) {
func (r *SampleMongoDbMigrator) V001_method_with_wrong_signature_down(session *mgo.Session) (bool, error) {
return true, nil
}

View File

@ -87,13 +87,6 @@ func (m *Migrator) Migrate(f file.File, pipe chan interface{}) error {
return nil
}
func reverseInPlace(a []string) {
for i := 0; i < len(a)/2; i++ {
j := len(a) - i - 1
a[i], a[j] = a[j], a[i]
}
}
func getRollbackToMethod(methodName string) string {
if strings.HasSuffix(methodName, "_up") {
return strings.TrimSuffix(methodName, "_up") + "_down"

View File

@ -220,7 +220,7 @@ func TestMigrate(t *testing.T) {
}
if !failed {
t.Logf("case '%s': PASSED", c.name)
//t.Logf("case '%s': PASSED", c.name)
}
}
}
@ -245,27 +245,3 @@ func TestGetRollbackToMethod(t *testing.T) {
}
}
}
func TestReverseInPlace(t *testing.T) {
methods := []string{
"method1_down",
"method2_down",
"method3_down",
"method4_down",
"method5_down",
}
expectedReversedMethods := []string{
"method5_down",
"method4_down",
"method3_down",
"method2_down",
"method1_down",
}
reverseInPlace(methods)
if !reflect.DeepEqual(methods, expectedReversedMethods) {
t.Errorf("Expected reverse methods %v but got %v", expectedReversedMethods, methods)
}
}

View File

@ -3,7 +3,7 @@ package mongodb
import (
"errors"
"github.com/dimag-jfrog/migrate/driver"
"github.com/dimag-jfrog/migrate/driver/gomethods"
"github.com/dimag-jfrog/migrate/driver/mongodb/gomethods"
"github.com/dimag-jfrog/migrate/file"
"github.com/dimag-jfrog/migrate/migrate/direction"
"gopkg.in/mgo.v2"
@ -27,24 +27,24 @@ func (e WrongMethodsReceiverTypeError) Error() string {
const MIGRATE_C = "db_migrations"
const DRIVER_NAME = "gomethods.mongodb"
type MongoDbGoMethodsDriver struct {
type Driver struct {
Session *mgo.Session
methodsReceiver MethodsReceiver
migrator gomethods.Migrator
}
var _ gomethods.GoMethodsDriver = (*MongoDbGoMethodsDriver)(nil)
var _ gomethods.GoMethodsDriver = (*Driver)(nil)
type MethodsReceiver interface {
DbName() string
}
func (d *MongoDbGoMethodsDriver) MethodsReceiver() interface{} {
func (d *Driver) MethodsReceiver() interface{} {
return d.methodsReceiver
}
func (d *MongoDbGoMethodsDriver) SetMethodsReceiver(r interface{}) error {
func (d *Driver) SetMethodsReceiver(r interface{}) error {
r1, ok := r.(MethodsReceiver)
if !ok {
return WrongMethodsReceiverTypeError(DRIVER_NAME)
@ -55,7 +55,7 @@ func (d *MongoDbGoMethodsDriver) SetMethodsReceiver(r interface{}) error {
}
func init() {
driver.RegisterDriver("mongodb", &MongoDbGoMethodsDriver{})
driver.RegisterDriver("mongodb", &Driver{})
}
type DbMigration struct {
@ -63,7 +63,7 @@ type DbMigration struct {
Version uint64 `bson:"version"`
}
func (driver *MongoDbGoMethodsDriver) Initialize(url string) error {
func (driver *Driver) Initialize(url string) error {
if driver.methodsReceiver == nil {
return UnregisteredMethodsReceiverError(DRIVER_NAME)
}
@ -85,18 +85,18 @@ func (driver *MongoDbGoMethodsDriver) Initialize(url string) error {
return nil
}
func (driver *MongoDbGoMethodsDriver) Close() error {
func (driver *Driver) Close() error {
if driver.Session != nil {
driver.Session.Close()
}
return nil
}
func (driver *MongoDbGoMethodsDriver) FilenameExtension() string {
func (driver *Driver) FilenameExtension() string {
return "mgo"
}
func (driver *MongoDbGoMethodsDriver) Version() (uint64, error) {
func (driver *Driver) Version() (uint64, error) {
var latestMigration DbMigration
c := driver.Session.DB(driver.methodsReceiver.DbName()).C(MIGRATE_C)
@ -111,7 +111,7 @@ func (driver *MongoDbGoMethodsDriver) Version() (uint64, error) {
return latestMigration.Version, nil
}
}
func (driver *MongoDbGoMethodsDriver) Migrate(f file.File, pipe chan interface{}) {
func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
defer close(pipe)
pipe <- f
@ -141,7 +141,7 @@ func (driver *MongoDbGoMethodsDriver) Migrate(f file.File, pipe chan interface{}
}
}
func (driver *MongoDbGoMethodsDriver) Validate(methodName string) error {
func (driver *Driver) Validate(methodName string) error {
method := reflect.ValueOf(driver.methodsReceiver).MethodByName(methodName)
if !method.IsValid() {
return gomethods.MissingMethodError(methodName)
@ -156,7 +156,7 @@ func (driver *MongoDbGoMethodsDriver) Validate(methodName string) error {
return nil
}
func (driver *MongoDbGoMethodsDriver) Invoke(methodName string) error {
func (driver *Driver) Invoke(methodName string) error {
name := methodName
migrateMethod := reflect.ValueOf(driver.methodsReceiver).MethodByName(name)
if !migrateMethod.IsValid() {