From e3b83a7c80fd26db3815c0a07e51ead84375df6e Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 6 Nov 2024 07:18:56 +0000 Subject: [PATCH 1/2] Add `--skip-validation` flag Add support for the `--skip-validation` flag to skip migration validation. The flag can also be set using the `PGROLL_SKIP_VALIDATION` environment variable. --- cmd/flags/flags.go | 2 ++ cmd/root.go | 2 ++ cmd/start.go | 4 ++++ pkg/roll/options.go | 11 +++++++++++ pkg/roll/roll.go | 2 ++ 5 files changed, 21 insertions(+) diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index 705a2e58..b50d4bb9 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -28,6 +28,8 @@ func BackfillBatchSize() int { return viper.GetInt("BACKFILL_BATCH_SIZE") } func BackfillBatchDelay() time.Duration { return viper.GetDuration("BACKFILL_BATCH_DELAY") } +func SkipValidation() bool { return viper.GetBool("SKIP_VALIDATION") } + func Role() string { return viper.GetString("ROLE") } diff --git a/cmd/root.go b/cmd/root.go index 1b01a1a5..5cab48d8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -51,6 +51,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) { role := flags.Role() backfillBatchSize := flags.BackfillBatchSize() backfillBatchDelay := flags.BackfillBatchDelay() + skipValidation := flags.SkipValidation() state, err := state.New(ctx, pgURL, stateSchema) if err != nil { @@ -62,6 +63,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) { roll.WithRole(role), roll.WithBackfillBatchSize(backfillBatchSize), roll.WithBackfillBatchDelay(backfillBatchDelay), + roll.WithSkipValidation(skipValidation), ) } diff --git a/cmd/start.go b/cmd/start.go index c26152e5..415c9476 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -11,6 +11,7 @@ import ( "github.com/pterm/pterm" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/xataio/pgroll/cmd/flags" "github.com/xataio/pgroll/pkg/migrations" @@ -39,6 +40,9 @@ func startCmd() *cobra.Command { startCmd.Flags().BoolVarP(&complete, "complete", "c", false, "Mark the migration as complete") + startCmd.Flags().BoolP("skip-validation", "s", false, "skip migration validation") + viper.BindPFlag("SKIP_VALIDATION", startCmd.Flags().Lookup("skip-validation")) + return startCmd } diff --git a/pkg/roll/options.go b/pkg/roll/options.go index efd08ce4..ea76c357 100644 --- a/pkg/roll/options.go +++ b/pkg/roll/options.go @@ -33,6 +33,9 @@ type options struct { // the duration to delay after each batch is run backfillBatchDelay time.Duration + // whether to skip validation + skipValidation bool + migrationHooks MigrationHooks } @@ -121,3 +124,11 @@ func WithBackfillBatchDelay(delay time.Duration) Option { o.backfillBatchDelay = delay } } + +// WithSkipValidation controls whether or not to perform validation on +// migrations. If set to true, validation will be skipped. +func WithSkipValidation(skip bool) Option { + return func(o *options) { + o.skipValidation = skip + } +} diff --git a/pkg/roll/roll.go b/pkg/roll/roll.go index e559119b..3d2581c7 100644 --- a/pkg/roll/roll.go +++ b/pkg/roll/roll.go @@ -43,6 +43,7 @@ type Roll struct { backfillBatchSize int backfillBatchDelay time.Duration + skipValidation bool } // New creates a new Roll instance @@ -84,6 +85,7 @@ func New(ctx context.Context, pgURL, schema string, state *state.State, opts ... migrationHooks: rollOpts.migrationHooks, backfillBatchSize: rollOpts.backfillBatchSize, backfillBatchDelay: rollOpts.backfillBatchDelay, + skipValidation: rollOpts.skipValidation, }, nil } From 9a814e8114872e369793f216f59a2a2f9b60317c Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 6 Nov 2024 07:21:03 +0000 Subject: [PATCH 2/2] Skip validation if flag is set --- pkg/roll/execute.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/roll/execute.go b/pkg/roll/execute.go index ce27e77d..23211eff 100644 --- a/pkg/roll/execute.go +++ b/pkg/roll/execute.go @@ -43,12 +43,14 @@ func (m *Roll) StartDDLOperations(ctx context.Context, migration *migrations.Mig } // validate migration - err = migration.Validate(ctx, newSchema) - if err != nil { - if err := m.state.Rollback(ctx, m.schema, migration.Name); err != nil { - fmt.Printf("failed to rollback migration: %s\n", err) + if !m.skipValidation { + err = migration.Validate(ctx, newSchema) + if err != nil { + if err := m.state.Rollback(ctx, m.schema, migration.Name); err != nil { + fmt.Printf("failed to rollback migration: %s\n", err) + } + return nil, fmt.Errorf("migration is invalid: %w", err) } - return nil, fmt.Errorf("migration is invalid: %w", err) } // run any BeforeStartDDL hooks