Skip to content
This repository was archived by the owner on Jul 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Migrate Changelog

## v1.3.2 - unreleased

* Mongodb support (credits: @dimag-jfrog)

## v1.3.1 - 2016-08-16

* make mysql driver aware of SSL certificates for TLS connection by scanning ENV variables (https://github.com/mattes/migrate/pull/117/files)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ __Features__
* [Cassandra](https://github.com/gemnasium/migrate/tree/master/driver/cassandra)
* [SQLite](https://github.com/gemnasium/migrate/tree/master/driver/sqlite3)
* [MySQL](https://github.com/gemnasium/migrate/tree/master/driver/mysql) ([experimental](https://github.com/mattes/migrate/issues/1#issuecomment-58728186))
* [MongoDB](https://github.com/gemnasium/migrate/tree/master/driver/mongodb)
* Bash (planned)

Need another driver? Just implement the [Driver interface](http://godoc.org/github.com/gemnasium/migrate/driver#Driver) and open a PR.
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go-test:
- postgres
- mysql
- cassandra
- mongo
go-build:
<<: *go
command: sh -c 'go get -v && go build -ldflags ''-s'' -o migrater'
Expand All @@ -24,3 +25,5 @@ mysql:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
cassandra:
image: cassandra:2.2
mongo:
image: mongo:3.2.6
104 changes: 104 additions & 0 deletions driver/mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# MongoDB Driver

* Runs pre-registered Golang methods that receive a single `*mgo.Session` parameter and return `error` 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

```go
import "github.com/gemnasium/migrate/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<version> : 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](https://github.com/gemnasium/migrate/blob/master/driver/mongodb/example/sample_mongdb_migrator.go)

```go
package my_mongo_db_migrator

import (
"github.com/gemnasium/migrate/driver/mongodb"
"github.com/gemnasium/migrate/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

Loading