Skip to content

Commit

Permalink
Merge pull request #4 from nangsan/feat-official-mongo-driver
Browse files Browse the repository at this point in the history
official mongo driver
  • Loading branch information
xakep666 authored Jun 3, 2019
2 parents 52160aa + 86bd2be commit 3f7ed27
Show file tree
Hide file tree
Showing 12 changed files with 590 additions and 212 deletions.
18 changes: 9 additions & 9 deletions 1_global_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package migrate
import (
"testing"

"github.com/globalsign/mgo"
"go.mongodb.org/mongo-driver/mongo"
)

func TestGlobalMigrateSetGet(t *testing.T) {
oldMigrate := globalMigrate
defer func() {
globalMigrate = oldMigrate
}()
db := &mgo.Database{}
db := &mongo.Database{}
globalMigrate = NewMigrate(db)

if globalMigrate.db != db {
t.Errorf("Unexpected non-equal dbs")
}
db2 := &mgo.Database{}
db2 := &mongo.Database{}
SetDatabase(db2)
if globalMigrate.db != db2 {
t.Errorf("Unexpected non-equal dbs")
Expand All @@ -35,9 +35,9 @@ func TestMigrationsRegistration(t *testing.T) {
}()
globalMigrate = NewMigrate(nil)

err := Register(func(db *mgo.Database) error {
err := Register(func(db *mongo.Database) error {
return nil
}, func(db *mgo.Database) error {
}, func(db *mongo.Database) error {
return nil
})
if err != nil {
Expand All @@ -53,9 +53,9 @@ func TestMigrationsRegistration(t *testing.T) {
t.Errorf("Unexpected version/description: %d %s", registered[0].Version, registered[0].Description)
}

err = Register(func(db *mgo.Database) error {
err = Register(func(db *mongo.Database) error {
return nil
}, func(db *mgo.Database) error {
}, func(db *mongo.Database) error {
return nil
})
if err == nil {
Expand All @@ -72,9 +72,9 @@ func TestMigrationMustRegistration(t *testing.T) {
}
}()
globalMigrate = NewMigrate(nil)
MustRegister(func(db *mgo.Database) error {
MustRegister(func(db *mongo.Database) error {
return nil
}, func(db *mgo.Database) error {
}, func(db *mongo.Database) error {
return nil
})
registered := RegisteredMigrations()
Expand Down
22 changes: 16 additions & 6 deletions 1_sample_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
package migrate

import (
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

const globalTestCollection = "test-global"

func init() {
Register(func(db *mgo.Database) error {
return db.C(globalTestCollection).Insert(bson.M{"a": "b"})
}, func(db *mgo.Database) error {
return db.C(globalTestCollection).Remove(bson.M{"a": "b"})
Register(func(db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).InsertOne(context.TODO(), bson.D{{"a", "b"}})
if err != nil {
return err
}
return nil
}, func(db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).DeleteOne(context.TODO(), bson.D{{"a", "b"}})
if err != nil {
return err
}
return nil
})
}
25 changes: 20 additions & 5 deletions 2_sample_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
package migrate

import (
"github.com/globalsign/mgo"
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

const globalTestIndexName = "test_idx_2"

func init() {
Register(func(db *mgo.Database) error {
return db.C(globalTestCollection).EnsureIndex(mgo.Index{Name: globalTestIndexName, Key: []string{"a"}})
}, func(db *mgo.Database) error {
return db.C(globalTestCollection).DropIndexName(globalTestIndexName)
Register(func(db *mongo.Database) error {
keys := bson.D{{"a", 1}}
opt := options.Index().SetName(globalTestIndexName)
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection(globalTestCollection).Indexes().CreateOne(context.TODO(), model)
if err != nil {
return err
}
return nil
}, func(db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).Indexes().DropOne(context.TODO(), globalTestIndexName)
if err != nil {
return err
}
return nil
})
}
128 changes: 79 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
[![GoDoc](https://godoc.org/github.com/xakep666/mongo-migrate?status.svg)](https://godoc.org/github.com/xakep666/mongo-migrate)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

This package allows to perform versioned migrations on your MongoDB using [mgo driver](https://github.com/globalsign/mgo).
It depends only on standard library and mgo driver.
This package allows to perform versioned migrations on your MongoDB using [mongo-go-driver](https://github.com/mongodb/mongo-go-driver).
Inspired by [go-pg migrations](https://github.com/go-pg/migrations).

Table of Contents
Expand Down Expand Up @@ -40,16 +39,29 @@ File name should be like `<version>_<description>.go`.
package migrations

import (
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
migrate "github.com/xakep666/mongo-migrate"
)

func init() {
migrate.Register(func(db *mgo.Database) error {
return db.C("my-coll").EnsureIndex(mgo.Index{Name: "my-index", Key: []string{"my-key"}}))
}, func(db *mgo.Database) error {
return db.C("my-coll").DropIndexName("my-index")
migrate.Register(func(db *mongo.Database) error {
opt := options.Index().SetName("my-index")
keys := bson.D{{"my-key", 1}}
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
if err != nil {
return err
}

return nil
}, func(db *mongo.Database) error {
_, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
if err != nil {
return err
}
return nil
})
}
```
Expand All @@ -66,53 +78,71 @@ import (

* Run migrations.
```go
func MongoConnect(host, user, password, database string) (*mgo.Database, error) {
session, err := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{host},
Database: database,
Username: user,
Password: password,
})
if err != nil {
return nil, err
}
db := session.DB("")
migrate.SetDatabase(db)
if err := migrate.Up(migrate.AllAvailable); err != nil {
return nil, err
}
return db, nil
func MongoConnect(host, user, password, database string) (*mongo.Database, error) {
uri := fmt.Sprintf("mongodb://%s:%s@%s:27017", user, password, host)
opt := options.Client().ApplyURI(uri)
client, err := mongo.NewClient(opt)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
return nil, err
}
db = client.Database(database)
migrate.SetDatabase(db)
if err := migrate.Up(migrate.AllAvailable); err != nil {
return nil, err
}
return db, nil
}
```

### Use case #2. Migrations in application code.
* Just define it anywhere you want and run it.
```go
func MongoConnect(host, user, password, database string) (*mgo.Database, error) {
session, err := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{host},
Database: database,
Username: user,
Password: password,
})
if err != nil {
return nil, err
}
db := session.DB("")
m := migrate.NewMigrate(db, migrate.Migration{
Version: 1,
Description: "add my-index",
Up: func(db *mgo.Database) error {
return db.C("my-coll").EnsureIndex(mgo.Index{Name: "my-index", Key: []string{"my-key"}})
},
Down: func(db *mgo.Database) error {
return db.C("my-coll").DropIndexName("my-index")
},
})
if err := m.Up(migrate.AllAvailable); err != nil {
return nil, err
}
return db, nil
func MongoConnect(host, user, password, database string) (*mongo.Database, error) {
uri := fmt.Sprintf("mongodb://%s:%s@%s:27017", user, password, host)
opt := options.Client().ApplyURI(uri)
client, err := mongo.NewClient(opt)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
return nil, err
}
db = client.Database(database)
m := migrate.NewMigrate(db, migrate.Migration{
Version: 1,
Description: "add my-index",
Up: func(db *mongo.Database) error {
opt := options.Index().SetName("my-index")
keys := bson.D{{"my-key", 1}}
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
if err != nil {
return err
}

return nil
},
Down: func(db *mongo.Database) error {
_, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
if err != nil {
return err
}
return nil
},
})
if err := m.Up(migrate.AllAvailable); err != nil {
return nil, err
}
return db, nil
}
```

Expand Down
10 changes: 5 additions & 5 deletions bad_migration_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package migrate
import (
"testing"

"github.com/globalsign/mgo"
"go.mongodb.org/mongo-driver/mongo"
)

func TestBadMigrationFile(t *testing.T) {
Expand All @@ -13,9 +13,9 @@ func TestBadMigrationFile(t *testing.T) {
}()
globalMigrate = NewMigrate(nil)

err := Register(func(db *mgo.Database) error {
err := Register(func(db *mongo.Database) error {
return nil
}, func(db *mgo.Database) error {
}, func(db *mongo.Database) error {
return nil
})
if err == nil {
Expand All @@ -32,9 +32,9 @@ func TestBadMigrationFilePanic(t *testing.T) {
}
}()
globalMigrate = NewMigrate(nil)
MustRegister(func(db *mgo.Database) error {
MustRegister(func(db *mongo.Database) error {
return nil
}, func(db *mgo.Database) error {
}, func(db *mongo.Database) error {
return nil
})
}
42 changes: 28 additions & 14 deletions global_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"runtime"

"github.com/globalsign/mgo"
"go.mongodb.org/mongo-driver/mongo"
)

var globalMigrate = NewMigrate(nil)
Expand Down Expand Up @@ -34,19 +34,33 @@ func internalRegister(up, down MigrationFunc, skip int) error {
//
// - Use the following template inside:
//
// package migrations
// import (
// "github.com/globalsign/mgo"
// "github.com/xakep666/mongo-migrate"
// )
// package migrations
//
// func init() {
// migrate.Register(func (db *mgo.Database) error {
// return db.C(collection).EnsureIndex(index)
// }, func (db *mgo.Database) error {
// return db.C(collection).DropIndexName(index.Name)
// })
// }
// import (
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
// "github.com/xakep666/mongo-migrate"
// )
//
// func init() {
// Register(func(db *mongo.Database) error {
// opt := options.Index().SetName("my-index")
// keys := bson.D{{"my-key", 1}}
// model := mongo.IndexModel{Keys: keys, Options: opt}
// _, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
// if err != nil {
// return err
// }
// return nil
// }, func(db *mongo.Database) error {
// _, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
// if err != nil {
// return err
// }
// return nil
// })
// }
func Register(up, down MigrationFunc) error {
return internalRegister(up, down, 2)
}
Expand All @@ -66,7 +80,7 @@ func RegisteredMigrations() []Migration {
}

// SetDatabase sets database for global migrate.
func SetDatabase(db *mgo.Database) {
func SetDatabase(db *mongo.Database) {
globalMigrate.db = db
}

Expand Down
Loading

0 comments on commit 3f7ed27

Please sign in to comment.