MongoDB migration library for Golang.
- Migrate up or down all migration files or specific file
- Create history collection as migration files tracker
- Implement interface as template of the library
- Atomic migrations
- Use json migration file as
*.sql
for sql migration file - Implement
db.RunCommand()
. You can check here
There is no specific prerequisities, just need Golang :)
Install the library by running the following command
go get -u github.com/naufalfmm/mongodb-migration
Example of usage of the library in your project
package main
import (
"context"
"github.com/naufalfmm/mongodb-migration/client"
"github.com/naufalfmm/mongodb-migration/constants"
"github.com/naufalfmm/mongodb-migration/config"
"github.com/naufalfmm/mongodb-migration/constants/direction"
"github.com/naufalfmm/mongodb-migration/constants/steps"
"github.com/naufalfmm/mongodb-migration/driver"
"github.com/naufalfmm/mongodb-migration/history"
"github.com/naufalfmm/mongodb-migration/migration"
)
func main() {
var (
DBName = "db_migration_trial"
DBUser = "user"
DBPassword = "123456789"
DBHost = "localhost"
DBPort = "27017"
cfg = config.Config{
Name: DBName,
User: DBUser,
Password: DBPassword,
Host: DBHost,
Port: DBPort,
}
client = client.MongoClient{}
migr = migration.MongoMigration{}
mongoDriver = driver.MongoDriver{}
)
ctx := context.TODO()
cfg.SetURI()
mongoDriver.Client = &client
mongoDriver.SetClientWithContext(ctx, &cfg)
historyRecord := history.MigrationRecord{
DB: mongoDriver.GetDB(),
CollectionName: constants.DEFAULT_MIGRATION_HISTORY_COLLECTION,
}
err := migr.StartMigrationWithDriver(ctx, "./example/migrate/migrations/", &mongoDriver, &historyRecord)
if err != nil {
panic(err)
}
err = migr.Run(ctx, direction.UP, steps.ALL)
if err != nil {
panic(err)
}
}
or you can open the example here
You can check the example here
You can name your json migration file as your need. If you want to follow the standard, you name the file by
<timestamp>_name_of_your_migration_file.json
For example
20200716152127_create_full_name_of_individual_of_client.json
The content of migration file is the command - formed as document or string - of the db.runCommand()
. Please refer the list of commands here.
Example of the content
{
"up": {
"update": "Customer",
"updates": [
{
"q": { "individual": { "$ne": null } },
"u": [
{
"$set": {
"individual.full_name": {
"$concat": [
"$individual.first_name",
" ",
"$individual.last_name"
]
}
}
}
],
"multi": true
}
]
},
"down": {
"update": "Customer",
"updates": [
{
"q": {},
"u": [
{
"$unset": "individual.full_name"
}
],
"multi": true
}
]
}
}
up
is for migrate up and down
is for migrate down.
- Golang - Language
- @naufalfmm - Idea & Initial work
If you interest to join as contributor, please contact me on email or just create PR :)
- Hat tip to anyone whose code was used