Skip to content

Commit

Permalink
- Reset all changes to the upstream branch
Browse files Browse the repository at this point in the history
- Changed logic not to use custom filename parser:
  -Supporting up and down files only, no both direction files
  -Using method names as is
- Added complete test to the mongo db migration scenarios
  • Loading branch information
dimag-jfrog committed Aug 8, 2016
1 parent 64101a1 commit 5f7b2ca
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 431 deletions.
11 changes: 1 addition & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
.DS_Store
test.db
*.iml
.DS_Store
.idea
*.zip
*.tar.*
*.nuget
*.jar
*.war

test.db
9 changes: 0 additions & 9 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ type Driver interface {
Version() (uint64, error)
}

// Driver that has some custom migration file format
// and wants to use a different parsing strategy.
type DriverWithFilenameParser interface {

Driver

FilenameParser() file.FilenameParser
}

// New returns Driver and calls Initialize on it
func New(url string) (Driver, error) {
u, err := neturl.Parse(url)
Expand Down
35 changes: 13 additions & 22 deletions driver/gomethods/gomethods_migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bufio"
"github.com/dimag-jfrog/migrate/driver"
"github.com/dimag-jfrog/migrate/file"
"github.com/dimag-jfrog/migrate/migrate/direction"
)


Expand All @@ -32,7 +31,7 @@ func (e *MethodInvocationFailedError) Error() string {


type Migrator struct {
Driver driver.DriverWithFilenameParser
Driver driver.Driver
RollbackOnFailure bool
}

Expand All @@ -55,6 +54,10 @@ func (m *Migrator) Migrate(f file.File, pipe chan interface{}) error {
// on failure, try to rollback methods in this migration
for j := i-1; j >= 0; j-- {
rollbackToMethodName := getRollbackToMethod(methods[j])
if rollbackToMethodName == "" || !m.IsValid(rollbackToMethodName) {
continue
}

pipe <- rollbackToMethodName
err = m.Invoke(rollbackToMethodName)
if err != nil {
Expand Down Expand Up @@ -106,8 +109,10 @@ func reverseInPlace(a []string) {
func getRollbackToMethod(methodName string) string {
if strings.HasSuffix(methodName, "_up") {
return strings.TrimSuffix(methodName, "_up") + "_down"
} else {
} else if strings.HasSuffix(methodName, "_down") {
return strings.TrimSuffix(methodName, "_down") + "_up"
} else {
return ""
}
}

Expand Down Expand Up @@ -143,34 +148,20 @@ func (m *Migrator) getMigrationMethods(f file.File) ([]string, error) {
}

for _, line := range lines {
operationName := strings.TrimSpace(line)
methodName := strings.TrimSpace(line)

if operationName == "" || strings.HasPrefix(operationName, "--") {
if methodName == "" || strings.HasPrefix(methodName, "--") {
// an empty line or a comment, ignore
continue
}

upMethodName := operationName + "_up"
downMethodName := operationName + "_down"

if !m.IsValid(upMethodName) {
return nil, MissingMethodError(upMethodName)
}
if !m.IsValid(downMethodName) {
return nil, MissingMethodError(downMethodName)
if !m.IsValid(methodName) {
return nil, MissingMethodError(methodName)
}

if f.Direction == direction.Up {
methods = append(methods, upMethodName)
} else {
methods = append(methods, downMethodName)
}
methods = append(methods, methodName)
}

_,_,fileType,_ := m.Driver.FilenameParser().Parse(f.FileName)
if fileType == direction.Both && f.Direction == direction.Down {
reverseInPlace(methods)
}
return methods, nil

}
95 changes: 30 additions & 65 deletions driver/gomethods/gomethods_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ func (driver *FakeGoMethodsDriver) Close() error {
return nil
}

func (driver *FakeGoMethodsDriver) FilenameParser() file.FilenameParser {
return file.UpDownAndBothFilenameParser{ FilenameExtension: driver.FilenameExtension() }
}

func (driver *FakeGoMethodsDriver) FilenameExtension() string {
return "gm"
}
Expand Down Expand Up @@ -84,81 +80,49 @@ func TestMigrate(t *testing.T) {
expectRollback bool
}{
{
name: "up migration, both directions-file: invokes up methods in order",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
V001_init_organizations
V001_init_users
`),
},
expectedInvokedMethods: []string{"V001_init_organizations_up", "V001_init_users_up"},
expectedErrors: []error{},
},
{
name: "down migration, both-directions-file: reverts direction of invoked down methods",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
V001_init_organizations
V001_init_users
`),
},
expectedInvokedMethods: []string{"V001_init_users_down", "V001_init_organizations_down"},
expectedErrors: []error{},
},
{
name: "up migration, up direction-file: invokes up methods in order",
name: "up migration invokes up methods",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.up.gm",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
V001_init_organizations
V001_init_users
V001_init_organizations_up
V001_init_users_up
`),
},
expectedInvokedMethods: []string{"V001_init_organizations_up", "V001_init_users_up"},
expectedErrors: []error{},
},
{
name: "down migration, down directions-file: keeps order of invoked down methods",
name: "down migration invoked down methods",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.down.gm",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
V001_init_organizations
V001_init_users
V001_init_users_down
V001_init_organizations_down
`),
},
expectedInvokedMethods: []string{"V001_init_organizations_down", "V001_init_users_down"},
expectedInvokedMethods: []string{"V001_init_users_down", "V001_init_organizations_down"},
expectedErrors: []error{},
},
{
name: "up migration: non-existing method causes migration not to execute",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
FileName: "001_foobar.up.gm",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
V001_init_organizations
V001_init_users
V001_some_non_existing_method
V001_init_organizations_up
V001_init_users_up
V001_some_non_existing_method_up
`),
},
expectedInvokedMethods: []string{},
Expand All @@ -168,14 +132,14 @@ func TestMigrate(t *testing.T) {
name: "up migration: failing method stops execution",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
FileName: "001_foobar.up.gm",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
V001_init_organizations
V001_some_failing_method
V001_init_users
V001_init_organizations_up
V001_some_failing_method_up
V001_init_users_up
`),
},
expectedInvokedMethods: []string{
Expand All @@ -188,17 +152,17 @@ func TestMigrate(t *testing.T) {
}},
},
{
name: "down migration, both-directions-file: failing method stops migration",
name: "down migration: failing method stops migration",
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
FileName: "001_foobar.down.gm",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
V001_init_organizations
V001_some_failing_method
V001_init_users
V001_init_users_down
V001_some_failing_method_down
V001_init_organizations_down
`),
},
expectedInvokedMethods: []string{
Expand All @@ -215,14 +179,14 @@ func TestMigrate(t *testing.T) {
expectRollback: true,
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
FileName: "001_foobar.up.gm",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
V001_init_organizations
V001_init_users
V001_some_failing_method
V001_init_organizations_up
V001_init_users_up
V001_some_failing_method_up
`),
},
expectedInvokedMethods: []string{
Expand All @@ -238,18 +202,18 @@ func TestMigrate(t *testing.T) {
}},
},
{
name: "down migration, both-directions-file: failing method causes rollback in rollback mode",
name: "down migration: failing method causes rollback in rollback mode",
expectRollback: true,
file: file.File {
Path: "/foobar",
FileName: "001_foobar.gm",
FileName: "001_foobar.down.gm",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
V001_init_organizations
V001_some_failing_method
V001_init_users
V001_init_users_down
V001_some_failing_method_down
V001_init_organizations_down
`),
},
expectedInvokedMethods: []string{
Expand Down Expand Up @@ -306,6 +270,7 @@ func TestGetRollbackToMethod(t *testing.T) {
{"up_down_up", "up_down_down"},
{"down_up", "down_down"},
{"down_down", "down_up"},
{"some_method", ""},
}

for _, c := range cases {
Expand Down
4 changes: 0 additions & 4 deletions driver/gomethods/mongodb/mongodb_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func (driver *DriverTemplate) Close() error {
return nil
}

func (driver *DriverTemplate) FilenameParser() file.FilenameParser {
return file.UpDownAndBothFilenameParser{FilenameExtension: driver.FilenameExtension()}
}

func (driver *DriverTemplate) FilenameExtension() string {
return "mgo"
}
Expand Down
31 changes: 25 additions & 6 deletions driver/gomethods/usage_examples/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,39 @@ type Organization struct {
DateFounded time.Time `bson:"date_founded"`
}

type Organization_v2 struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"name"`
Headquarters string `bson:"headquarters"`
DateFounded time.Time `bson:"date_founded"`
}

type User struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
}

var OrganizationIds []bson.ObjectId = []bson.ObjectId{
bson.NewObjectId(),
bson.NewObjectId(),
bson.NewObjectId(),
}

var UserIds []bson.ObjectId = []bson.ObjectId{
bson.NewObjectId(),
bson.NewObjectId(),
bson.NewObjectId(),
}

func (m *GoMethodsMongoDbDriver) V001_init_organizations_up() 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")

orgs := []Organization{
{Id: bson.NewObjectId(), Name: "Amazon", Location:"Seattle", DateFounded: date1},
{Id: bson.NewObjectId(), Name: "Google", Location:"Mountain View", DateFounded: date2},
{Id: bson.NewObjectId(), Name: "JFrog", Location:"Santa Clara", DateFounded: date3},
{Id: OrganizationIds[0], Name: "Amazon", Location:"Seattle", DateFounded: date1},
{Id: OrganizationIds[1], Name: "Google", Location:"Mountain View", DateFounded: date2},
{Id: OrganizationIds[2], Name: "JFrog", Location:"Santa Clara", DateFounded: date3},
}

for _, org := range orgs {
Expand All @@ -70,9 +89,9 @@ func (m *GoMethodsMongoDbDriver) V001_init_organizations_down() error {

func (m *GoMethodsMongoDbDriver) V001_init_users_up() error {
users := []User{
{Id: bson.NewObjectId(), Name: "Alex"},
{Id: bson.NewObjectId(), Name: "Beatrice"},
{Id: bson.NewObjectId(), Name: "Cleo"},
{Id: UserIds[0], Name: "Alex"},
{Id: UserIds[1], Name: "Beatrice"},
{Id: UserIds[2], Name: "Cleo"},
}

for _, user := range users {
Expand Down
Loading

0 comments on commit 5f7b2ca

Please sign in to comment.