All Projects → xakep666 → mongo-migrate

xakep666 / mongo-migrate

Licence: MIT license
Versioned migrations for MongoDB.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to mongo-migrate

upscheme
Database migrations and schema updates made easy
Stars: ✭ 737 (+832.91%)
Mutual labels:  migrations, database-migrations, database-management
Phoenix
Framework agnostic database migrations for PHP.
Stars: ✭ 81 (+2.53%)
Mutual labels:  migrations, database-migrations
Migrate Mongo
A database migration tool for MongoDB in Node
Stars: ✭ 481 (+508.86%)
Mutual labels:  migrations, database-migrations
Postgres Migrations
🐦 A Stack Overflow-inspired PostgreSQL migration library with strict ordering and immutable migrations
Stars: ✭ 161 (+103.8%)
Mutual labels:  migrations, database-migrations
sync-db
Utility to synchronize relational database objects across databases.
Stars: ✭ 15 (-81.01%)
Mutual labels:  migrations, database-migrations
Goose
A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+2573.42%)
Mutual labels:  migrations, database-migrations
Dbmate
🚀 A lightweight, framework-agnostic database migration tool.
Stars: ✭ 2,228 (+2720.25%)
Mutual labels:  migrations, database-migrations
Compalex
Lightweight script to compare two database
Stars: ✭ 318 (+302.53%)
Mutual labels:  database-migrations, database-management
r2dbc-migrate
R2DBC database migration tool & library
Stars: ✭ 83 (+5.06%)
Mutual labels:  database-migrations, database-management
migrations
Migrations is a database migration tool that uses go's database/sql from the standard library
Stars: ✭ 17 (-78.48%)
Mutual labels:  migrations, database-migrations
migrant
Migration management for PostgreSQL/SQLite/MySQL
Stars: ✭ 85 (+7.59%)
Mutual labels:  migrations, database-migrations
Liquibase
Main Liquibase Source
Stars: ✭ 2,910 (+3583.54%)
Mutual labels:  database-migrations, database-management
Obevo
Obevo is a database deployment tool that handles enterprise scale schemas and complexity
Stars: ✭ 192 (+143.04%)
Mutual labels:  database-migrations, database-management
Phinx
PHP Database Migrations for Everyone
Stars: ✭ 4,245 (+5273.42%)
Mutual labels:  migrations, database-migrations
Prana
Golang Database Management and Code Generation
Stars: ✭ 89 (+12.66%)
Mutual labels:  database-migrations, database-management
Lol dba
lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts.
Stars: ✭ 1,363 (+1625.32%)
Mutual labels:  migrations, database-migrations
cl-migratum
Database Schema Migration System for Common Lisp
Stars: ✭ 29 (-63.29%)
Mutual labels:  database-migrations, database-management
Erd
A Rails engine for drawing your app's ER diagram
Stars: ✭ 296 (+274.68%)
Mutual labels:  database-migrations, database-management
Secondbase
Seamless second database integration for Rails.
Stars: ✭ 216 (+173.42%)
Mutual labels:  migrations, database-management
db-migrator.go
DB migrations. CLI and Golang
Stars: ✭ 13 (-83.54%)
Mutual labels:  migrations, database-migrations

Versioned migrations for MongoDB

Build Status codecov Go Report Card GoDoc License: MIT

This package allows to perform versioned migrations on your MongoDB using mongo-go-driver. Inspired by go-pg migrations.

Table of Contents

Prerequisites

  • Golang >= 1.10 or Vgo

Installation

go get -v -u github.com/xakep666/mongo-migrate

Usage

Use case #1. Migrations in files.

  • Create a package with migration files. File name should be like <version>_<description>.go.

1_add-my-index.go

package migrations

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	migrate "github.com/xakep666/mongo-migrate"
)

func init() {
	migrate.Register(func(db *mongo.Database) error {
		opt := options.Index().SetName("my-index")
		keys := bson.D{{"my-key", 1}}
		model := mongo.IndexModel{Keys: keys, Options: opt}
		_, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
		if err != nil {
			return err
		}

		return nil
	}, func(db *mongo.Database) error {
		_, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
		if err != nil {
			return err
		}
		return nil
	})
}
  • Import it in your application.
import (
    ...
    migrate "github.com/xakep666/mongo-migrate"
    _ "path/to/migrations_package" // database migrations
    ...
)
  • Run migrations.
func MongoConnect(host, user, password, database string) (*mongo.Database, error) {
	uri := fmt.Sprintf("mongodb://%s:%s@%s:27017", user, password, host)
	opt := options.Client().ApplyURI(uri)
	client, err := mongo.NewClient(opt)
	if err != nil {
		return nil, err
	}
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer cancel()
	err = client.Connect(ctx)
	if err != nil {
		return nil, err
	}
	db = client.Database(database)
	migrate.SetDatabase(db)
	if err := migrate.Up(migrate.AllAvailable); err != nil {
		return nil, err
	}
	return db, nil
}

Use case #2. Migrations in application code.

  • Just define it anywhere you want and run it.
func MongoConnect(host, user, password, database string) (*mongo.Database, error) {
	uri := fmt.Sprintf("mongodb://%s:%s@%s:27017", user, password, host)
	opt := options.Client().ApplyURI(uri)
	client, err := mongo.NewClient(opt)
	if err != nil {
		return nil, err
	}
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer cancel()
	err = client.Connect(ctx)
	if err != nil {
		return nil, err
	}
	db = client.Database(database)
	m := migrate.NewMigrate(db, migrate.Migration{
		Version: 1,
		Description: "add my-index",
		Up: func(db *mongo.Database) error {
			opt := options.Index().SetName("my-index")
			keys := bson.D{{"my-key", 1}}
			model := mongo.IndexModel{Keys: keys, Options: opt}
			_, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
			if err != nil {
				return err
			}

			return nil
		},
		Down: func(db *mongo.Database) error {
			_, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
			if err != nil {
				return err
			}
			return nil
		},
	})
	if err := m.Up(migrate.AllAvailable); err != nil {
		return nil, err
	}
	return db, nil
}

How it works?

This package creates a special collection (by default it`s name is "migrations") for versioning. In this collection stored documents like

{
    "_id": "<mongodb-generated id>",
    "version": 1,
    "description": "add my-index",
    "timestamp": "<when applied>"
}

Current database version determined as version from latest inserted document.

You can change collection name using SetMigrationsCollection methods. Remember that if you want to use custom collection name you need to set it before running migrations.

License

mongo-migrate project is licensed under the terms of the MIT license. Please see LICENSE in this repository for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].