mongodb-migration

module
v0.0.0-...-e0c2b11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2020 License: Apache-2.0

README ΒΆ

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

Directories ΒΆ

Path Synopsis
example
mocks

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL