Documentation
¶
Overview ¶
Package migrate allows to perform versioned migrations in your MongoDB.
Index ¶
Constants ¶
const AllAvailable = -1
AllAvailable used in "Up" or "Down" methods to run all available migrations.
Variables ¶
This section is empty.
Functions ¶
func Down ¶
Down performs "down" migration using registered migrations. Detailed description available in Migrate.Down().
func IsMigrated ¶
IsMigrated returns true if all migrations have been migrated, false otherwise.
func MustRegister ¶
func MustRegister(up, down MigrationFunc)
MustRegister acts like Register but panics on errors.
func Register ¶
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/SoftSwiss/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 ¶
SetDatabase sets database for global migrate.
func SetMigrationsCollection ¶
func SetMigrationsCollection(name string)
SetMigrationsCollection changes default collection name for migrations history.
Types ¶
type Migrate ¶
type Migrate struct {
// contains filtered or unexported fields
}
Migrate is type for performing migrations in provided database. Information about which migrations have been applied is being stored in a dedicated collection. Each migration applying ("up") adds new document to the collection. This document consists of migration version, migration description and timestamp. Each migration rollback ("down") removes a corresponding document from the collection.
db - database connection migrations - list of all registered migrations migrationsCollection - name of a collection where migrations are stored migratedVersions - list of migration versions stored in the database isLoadedMigratedVersions - whether loading of migratedVersions has already been done
func (*Migrate) Down ¶
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) IsMigrated ¶
IsMigrated returns true if all migrations have been migrated, false otherwise.
func (*Migrate) SetMigrationsCollection ¶
SetMigrationsCollection replaces name of collection for storing migration information. By default it is "migrations".
type Migration ¶
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 ¶
func RegisteredMigrations() []Migration
RegisteredMigrations returns all registered migrations.
type MigrationFunc ¶
MigrationFunc used to define actions to be performed for a migration.