Documentation ¶
Overview ¶
Package migrate allows to perform versioned migrations in your MongoDB.
Index ¶
- Constants
- func Down(n int) error
- func MustRegister(implementation IMigration)
- func Register(implementation IMigration) error
- func SetClient(client *mongo.Client)
- func SetDatabase(db *mongo.Database)
- func SetMigrationsCollection(name string)
- func Up(n int) error
- func Version() (uint64, string, error)
- type IMigration
- type Migrate
- type Migration
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 MustRegister ¶
func MustRegister(implementation IMigration)
MustRegister acts like Register but panics on errors.
func Register ¶
func Register(implementation IMigration) 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 ¶
SetDatabase sets database for global migrate.
func SetMigrationsCollection ¶
func SetMigrationsCollection(name string)
SetMigrationsCollection changes default collection name for migrations history.
Types ¶
type IMigration ¶
type IMigration interface { Up(client *mongo.Client, db *mongo.Database) error Down(client *mongo.Client, db *mongo.Database) error }
IMigration represents the interface that each migration needs to implement
type Migrate ¶
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 ¶
NewMigrate creates a migration
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) SetMigrationsCollection ¶
SetMigrationsCollection replaces name of collection for storing migration information. By default it is "migrations".
func (*Migrate) SetVersion ¶
SetVersion forcibly changes database version to provided.
type Migration ¶
type Migration struct { Version uint64 Description string Implementation IMigration }
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.