This package allows to perform versioned migrations on your MongoDB using mgo driver. It depends only on standard library and mgo driver. Inspired by go-pg migrations.
- Golang >= 1.10 or Vgo
go get -v -u github.com/xakep666/mongo-migrate
- Create a package with migration files.
File name should be like
<version>_<description>.go
.
1_add-my-index.go
package migrations
import (
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
migrate "github.com/xakep666/mongo-migrate"
)
func init() {
migrate.Register(func(db *mgo.Database) error {
return db.C("my-coll").Insert(EnsureIndex(mgo.Index{Name: "my-index", Key: []string{"my-key"}}))
}, func(db *mgo.Database) error {
return db.C("my-coll").DropIndexName("my-index")
})
}
- Import it in your application.
import (
...
migrate "github.com/xakep666/mongo-migrate"
_ "path/to/migrations_package" // database migrations
...
)
- Run migrations.
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
}
- Just define it anywhere you want and run it.
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
}
This package creates a special collection (by default it`s name is "migrations") for versioning. In this collection stored documents like
{
"_id": "<mongodb-generated id>",
"version": 1,
"description": "add my-index",
"timestamp": "<when applied>"
}
Current database version determined as version from latest inserted document.
You can change collection name using SetMigrationsCollection
methods.
Remember that if you want to use custom collection name you need to set it before running migrations.
mongo-migrate project is licensed under the terms of the MIT license. Please see LICENSE in this repository for more details.