README
¶
MongoDB Driver
- Runs pre-registered Golang methods that receive a single
*mgo.Session
parameter and returnerror
on failure. - Stores migration version details in collection
db_migrations
. This collection will be auto-generated. - Migrations do not run in transactions, there are no built-in transactions in MongoDB. That means that if a migration fails, it will not be rolled back.
- There is no out-of-the-box support for command-line interface via terminal.
Usage in Go
import "gopkg.in/mattes/migrate.v1/migrate"
// Import your migration methods package so that they are registered and available for the MongoDB driver.
// There is no need to import the MongoDB driver explicitly, as it should already be imported by your migration methods package.
import _ "my_mongo_db_migrator"
// use synchronous versions of migration functions ...
allErrors, ok := migrate.UpSync("mongodb://host:port", "./path")
if !ok {
fmt.Println("Oh no ...")
// do sth with allErrors slice
}
// use the asynchronous version of migration functions ...
pipe := migrate.NewPipe()
go migrate.Up(pipe, "mongodb://host:port", "./path")
// pipe is basically just a channel
// write your own channel listener. see writePipe() in main.go as an example.
Migration files format
The migration files should have an ".mgo" extension and contain a list of registered methods names.
Migration methods should satisfy the following:
- They should be exported (their name should start with a capital letter)
- Their type should be
func (*mgo.Session) error
Recommended (but not required) naming conventions for migration methods:
- Prefix with V : for example V001 for version 1.
- Suffix with "_up" or "_down" for up and down migrations correspondingly.
001_first_release.up.mgo
V001_some_migration_operation_up
V001_some_other_operation_up
...
001_first_release.down.mgo
V001_some_other_operation_down
V001_some_migration_operation_down
...
Methods registration
For a detailed example see: sample_mongodb_migrator.go
package my_mongo_db_migrator
import (
"gopkg.in/mattes/migrate.v1/driver/mongodb"
"gopkg.in/mattes/migrate.v1/driver/mongodb/gomethods"
"gopkg.in/mgo.v2"
)
// common boilerplate
type MyMongoDbMigrator struct {
}
func (r *MyMongoDbMigrator) DbName() string {
return "<target_db_name_for_migration>"
}
var _ mongodb.MethodsReceiver = (*MyMongoDbMigrator)(nil)
func init() {
gomethods.RegisterMethodsReceiverForDriver("mongodb", &MyMongoDbMigrator{})
}
// Here goes the application-specific migration logic
func (r *MyMongoDbMigrator) V001_some_migration_operation_up(session *mgo.Session) error {
// do something
return nil
}
func (r *MyMongoDbMigrator) V001_some_migration_operation_down(session *mgo.Session) error {
// revert some_migration_operation_up from above
return nil
}
Authors
- Demitry Gershovich, https://github.com/dimag-jfrog
Documentation
¶
Index ¶
- Constants
- type DbMigration
- type Driver
- func (driver *Driver) Close() error
- func (driver *Driver) FilenameExtension() string
- func (driver *Driver) Initialize(url string) error
- func (driver *Driver) Invoke(methodName string) error
- func (d *Driver) MethodsReceiver() interface{}
- func (driver *Driver) Migrate(f file.File, pipe chan interface{})
- func (d *Driver) SetMethodsReceiver(r interface{}) error
- func (driver *Driver) Validate(methodName string) error
- func (driver *Driver) Version() (uint64, error)
- type MethodsReceiver
- type UnregisteredMethodsReceiverError
- type WrongMethodsReceiverTypeError
Constants ¶
View Source
const DRIVER_NAME = "gomethods.mongodb"
View Source
const MIGRATE_C = "db_migrations"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DbMigration ¶
type Driver ¶
type Driver struct { Session *mgo.Session // contains filtered or unexported fields }
func (*Driver) FilenameExtension ¶
func (*Driver) Initialize ¶
func (*Driver) MethodsReceiver ¶
func (d *Driver) MethodsReceiver() interface{}
func (*Driver) SetMethodsReceiver ¶
type MethodsReceiver ¶
type MethodsReceiver interface {
DbName() string
}
type UnregisteredMethodsReceiverError ¶
type UnregisteredMethodsReceiverError string
func (UnregisteredMethodsReceiverError) Error ¶
func (e UnregisteredMethodsReceiverError) Error() string
type WrongMethodsReceiverTypeError ¶
type WrongMethodsReceiverTypeError string
func (WrongMethodsReceiverTypeError) Error ¶
func (e WrongMethodsReceiverTypeError) Error() string
Click to show internal directories.
Click to hide internal directories.