Skip to content

naufalfmm/mongodb-migration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Migration

Status GitHub Release License


MongoDB migration library for Golang.

📝 Table of Contents

🧐 Features

  • 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

🏁 Getting Started

Prerequisites

There is no specific prerequisities, just need Golang :)

Installing

Install the library by running the following command

go get -u github.com/naufalfmm/mongodb-migration

🎈 Usage

Example

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

Migration File

You can check the example here

Naming

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

Content

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.

⛏️ Built Using

✍️ Authors

If you interest to join as contributor, please contact me on email or just create PR :)

🎉 Acknowledgements

  • Hat tip to anyone whose code was used

About

Migration for mongodb

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages