mongo-migrate: github.com/xakep666/mongo-migrate Index | Files

package migrate

import "github.com/xakep666/mongo-migrate"

Package migrate allows to perform versioned migrations in your MongoDB.

Index

Package Files

global_migrate.go migrate.go migration.go util.go

Constants

const AllAvailable = -1

AllAvailable used in "Up" or "Down" methods to run all available migrations.

func Down Uses

func Down(n int) error

Down performs "down" migration using registered migrations. Detailed description available in Migrate.Down().

func MustRegister Uses

func MustRegister(up, down MigrationFunc)

MustRegister acts like Register but panics on errors.

func Register Uses

func Register(up, down MigrationFunc) error

Register performs migration registration. Use case of this function:

- Create a file called like "1_setup_indexes.go" ("<version>_<comment>.go").

- Use the following template inside:

 package migrations

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

 func init() {
	 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
	 })
 }

func SetDatabase Uses

func SetDatabase(db *mongo.Database)

SetDatabase sets database for global migrate.

func SetMigrationsCollection Uses

func SetMigrationsCollection(name string)

SetMigrationsCollection changes default collection name for migrations history.

func Up Uses

func Up(n int) error

Up performs "up" migration using registered migrations. Detailed description available in Migrate.Up().

func Version Uses

func Version() (uint64, string, error)

Version returns current database version.

type Migrate Uses

type Migrate struct {
    // contains filtered or unexported fields
}

Migrate is type for performing migrations in provided database. Database versioned using dedicated collection. Each migration applying ("up" and "down") adds new document to collection. This document consists migration version, migration description and timestamp. Current database version determined as version in latest added document (biggest "_id") from collection mentioned above.

func NewMigrate Uses

func NewMigrate(db *mongo.Database, migrations ...Migration) *Migrate

func (*Migrate) Down Uses

func (m *Migrate) Down(n int) error

Down performs "down" migration to oldest available version. If n<=0 all "down" migrations with older version will be performed. If n>0 only n migrations with older version will be performed.

func (*Migrate) SetMigrationsCollection Uses

func (m *Migrate) SetMigrationsCollection(name string)

SetMigrationsCollection replaces name of collection for storing migration information. By default it is "migrations".

func (*Migrate) SetVersion Uses

func (m *Migrate) SetVersion(version uint64, description string) error

SetVersion forcibly changes database version to provided.

func (*Migrate) Up Uses

func (m *Migrate) Up(n int) error

Up performs "up" migrations to latest available version. If n<=0 all "up" migrations with newer versions will be performed. If n>0 only n migrations with newer version will be performed.

func (*Migrate) Version Uses

func (m *Migrate) Version() (uint64, string, error)

Version returns current database version and comment.

type Migration Uses

type Migration struct {
    Version     uint64
    Description string
    Up          MigrationFunc
    Down        MigrationFunc
}

Migration represents single database migration. Migration contains:

- version: migration version, must be unique in migration list

- description: text description of migration

- up: callback which will be called in "up" migration process

- down: callback which will be called in "down" migration process for reverting changes

func RegisteredMigrations Uses

func RegisteredMigrations() []Migration

RegisteredMigrations returns all registered migrations.

type MigrationFunc Uses

type MigrationFunc func(db *mongo.Database) error

MigrationFunc used to define actions to be performed for a migration.

Package migrate imports 12 packages (graph) and is imported by 4 packages. Updated 2019-09-17. Refresh now. Tools for package owners.