Documentation ¶
Index ¶
- Variables
- type DuplicatedIDError
- type InitSchemaFunc
- type MigrateFunc
- type Migration
- type MigrationDoc
- type Migrator
- func (m *Migrator) CheckPendingMigrations(ctx context.Context) (bool, error)
- func (m *Migrator) Migrate(ctx context.Context) error
- func (m *Migrator) MigrateTo(ctx context.Context, migrationID string) error
- func (m *Migrator) RollbackLast(ctx context.Context) error
- func (m *Migrator) RollbackTo(ctx context.Context, migrationID string) error
- type Options
- type RollbackFunc
Constants ¶
This section is empty.
Variables ¶
View Source
var ( DefaultOptions = &Options{ DatabaseName: "convoy", CollectionName: "data_migrations", ValidateUnknownMigrations: true, UseTransaction: true, } // ErrRollbackImpossible is returned when trying to rollback a migration // that has no rollback function. ErrRollbackImpossible = errors.New("migrate: It's impossible to rollback this migration") // ErrNoMigrationDefined is returned when no migration is defined. ErrNoMigrationDefined = errors.New("migrate: No migration defined") // ErrMissingID is returned when the ID of the migration is equal to "" ErrMissingID = errors.New("migrate: Missing ID in migration") // ErrNoRunMigration is returned when any run migration was found while // running RollbackLast ErrNoRunMigration = errors.New("migrate: Could not find last run migration") // ErrUnknownPastMigration is returned if a migration exists in the DB that doesn't exist in the code ErrUnknownPastMigration = errors.New("migrate: Found migration in DB that does not exist in code") // ErrMigrationIDDoesNotExist is returned when migrating or rolling back to a migration ID that // does not exist in the list of migrations ErrMigrationIDDoesNotExist = errors.New("migrate: Tried to migrate to an ID that doesn't exist") // ErrPendingMigrationsFound is used to indicate there exist pending migrations yet to be run // if the user proceeds without running migrations it can lead to data integrity issues. ErrPendingMigrationsFound = errors.New("migrate: Pending migrations exist, please run convoy migrate first") )
View Source
var Migrations = []*Migration{ { ID: "20220901162904_change_group_rate_limit_configuration", Migrate: func(db *mongo.Database) error { type RTConfig struct { Duration string `json:"duration"` } type Config struct { RateLimit RTConfig `json:"ratelimit"` } type Group struct { UID string `json:"uid" bson:"uid"` Config Config `json:"config" bson:"config"` DocumentStatus datastore.DocumentStatus `json:"-" bson:"document_status"` } store := datastore.New(db) ctx := context.WithValue(context.Background(), datastore.CollectionCtx, datastore.GroupCollection) var groups []*Group err := store.FindAll(ctx, nil, nil, nil, &groups) if err != nil { return err } var newDuration uint64 for _, group := range groups { duration, err := time.ParseDuration(group.Config.RateLimit.Duration) if err != nil { newDuration = datastore.DefaultRateLimitConfig.Duration } else { newDuration = uint64(duration.Seconds()) } update := bson.M{ "$set": bson.M{ "config.ratelimit.duration": newDuration, }, } err = store.UpdateByID(ctx, group.UID, update) if err != nil { log.WithError(err).Fatalf("Failed migration") return err } } return nil }, Rollback: func(db *mongo.Database) error { store := datastore.New(db) ctx := context.WithValue(context.Background(), datastore.CollectionCtx, datastore.GroupCollection) var groups []*datastore.Group err := store.FindAll(ctx, nil, nil, nil, &groups) if err != nil { return err } var newDuration time.Duration for _, group := range groups { duration := fmt.Sprintf("%ds", group.Config.RateLimit.Duration) newDuration, err = time.ParseDuration(duration) if err != nil { return err } update := bson.M{ "$set": bson.M{ "config.ratelimit.duration": newDuration, }, } err = store.UpdateByID(ctx, group.UID, update) if err != nil { log.WithError(err).Fatalf("Failed migration") return err } } return nil }, }, { ID: "20220906166248_change_subscription_retry_configuration", Migrate: func(db *mongo.Database) error { type RetryConfig struct { Duration string `json:"duration"` } type Subscription struct { UID string `json:"uid" bson:"uid"` RetryConfig RetryConfig `json:"retry_config" bson:"retry_config"` DocumentStatus datastore.DocumentStatus `json:"-" bson:"document_status"` } store := datastore.New(db) ctx := context.WithValue(context.Background(), datastore.CollectionCtx, datastore.SubscriptionCollection) var subscriptions []*Subscription err := store.FindAll(ctx, nil, nil, nil, &subscriptions) if err != nil { return err } var newDuration uint64 for _, subscription := range subscriptions { duration, err := time.ParseDuration(subscription.RetryConfig.Duration) if err != nil { newDuration = datastore.DefaultStrategyConfig.Duration } else { newDuration = uint64(duration.Seconds()) } update := bson.M{ "$set": bson.M{ "retry_config.duration": newDuration, }, } err = store.UpdateByID(ctx, subscription.UID, update) if err != nil { log.WithError(err).Fatalf("Failed migration") return err } } return nil }, Rollback: func(db *mongo.Database) error { store := datastore.New(db) ctx := context.WithValue(context.Background(), datastore.CollectionCtx, datastore.SubscriptionCollection) var subscriptions []*datastore.Subscription err := store.FindAll(ctx, nil, nil, nil, &subscriptions) if err != nil { return err } var newDuration time.Duration for _, subscription := range subscriptions { duration := fmt.Sprintf("%ds", subscription.RetryConfig.Duration) newDuration, err = time.ParseDuration(duration) if err != nil { return err } update := bson.M{ "$set": bson.M{ "retry_config.duration": newDuration.String(), }, } err = store.UpdateByID(ctx, subscription.UID, update) if err != nil { log.WithError(err).Fatalf("Failed migration") return err } } return nil }, }, { ID: "20220919100029_add_default_group_configuration", Migrate: func(db *mongo.Database) error { store := datastore.New(db) ctx := context.WithValue(context.Background(), datastore.CollectionCtx, datastore.GroupCollection) var groups []*datastore.Group err := store.FindAll(ctx, nil, nil, nil, &groups) if err != nil { return err } for _, group := range groups { config := group.Config if config != nil { continue } config = &datastore.GroupConfig{ Signature: &datastore.DefaultSignatureConfig, Strategy: &datastore.DefaultStrategyConfig, RateLimit: &datastore.DefaultRateLimitConfig, RetentionPolicy: &datastore.DefaultRetentionPolicy, } update := bson.M{ "$set": bson.M{ "config": config, }, } err = store.UpdateByID(ctx, group.UID, update) if err != nil { log.WithError(err).Fatalf("Failed migration") return err } } return nil }, Rollback: func(db *mongo.Database) error { store := datastore.New(db) ctx := context.WithValue(context.Background(), datastore.CollectionCtx, datastore.GroupCollection) var groups []*datastore.Group err := store.FindAll(ctx, nil, nil, nil, &groups) if err != nil { return err } for _, group := range groups { config := group.Config if config == nil { continue } update := bson.M{ "$set": bson.M{ "config": nil, }, } err = store.UpdateByID(ctx, group.UID, update) if err != nil { log.WithError(err).Fatalf("Failed migration") return err } } return nil }, }, }
Functions ¶
This section is empty.
Types ¶
type DuplicatedIDError ¶
type DuplicatedIDError struct {
ID string
}
DuplicatedIDError is returned when more than one migration have the same ID
func (*DuplicatedIDError) Error ¶
func (e *DuplicatedIDError) Error() string
type MigrateFunc ¶
type Migration ¶
type Migration struct { // ID is the migration identifier. Usually a timestamp like "201601021504". ID string // Migrate is a function that will br executed while running this migration. Migrate MigrateFunc // Rollback will be executed on rollback. Can be nil. Rollback RollbackFunc }
type MigrationDoc ¶
type MigrationDoc struct { ID string `json:"id" bson:"id"` DocumentStatus datastore.DocumentStatus `json:"document_status" bson:"document_status"` }
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
func NewMigrator ¶
func (*Migrator) CheckPendingMigrations ¶
func (*Migrator) MigrateTo ¶
MigrateTo executes all migrations that did not run yet up to the migration that matches `migrationID`.
func (*Migrator) RollbackLast ¶
RollbackLast undo the last migration
type Options ¶
type Options struct { // DatabaseName is the name of the database to connect. DatabaseName string // CollectionName is the migration table. CollectionName string // ValidateUnknownMigrations will cause migrate to fail if there's unknown migration // IDs in the database ValidateUnknownMigrations bool // UseTransaction makes Gormigrate execute migrations inside a single transaction. UseTransaction bool }
type RollbackFunc ¶
Click to show internal directories.
Click to hide internal directories.