Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move backfill options to start and migrate command #660

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Feb 6, 2025

This PR moves the flags backfill-batch-delay and backfill-batch-size to
the commands start and migrate. So these options no longer show up in the list
of global flags.

I created a new struct backfill.Config to store and pass the options down
to the migration runner. I also moved the default values for batch delay and batch size
into the package backfill. I renamed options.go to config.go because it is a better name given the new contents of the file.

Closes #657

@kvch kvch marked this pull request as draft February 6, 2025 15:25
@kvch kvch force-pushed the feature-move-backfill-options branch 2 times, most recently from 08a7668 to ae056d6 Compare February 6, 2025 15:46
@kvch kvch force-pushed the feature-move-backfill-options branch from ae056d6 to 4dcd097 Compare February 6, 2025 16:14
@kvch kvch marked this pull request as ready for review February 6, 2025 16:18
@kvch kvch requested a review from andrew-farries February 6, 2025 16:18
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has broken the flags - they aren't taking effect for me:

Apply the migration:

{
  "name": "01_create_table",
  "operations": [
    {
      "create_table": {
        "name": "items",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "name",
            "type": "text"
          }
        ]
      }
    }
  ]
}

Fill the table with data:

insert into items(name) select 'name_' || i from generate_series(1, 1_000_000) as i

Start a migration with a specified delay:

go run . start --backfill-batch-delay=10s 02_set_unique.json

where the migration is defined:

{
  "name": "02_set_unique",
  "operations": [
    {
      "alter_column": {
        "table": "items",
        "column": "name",
        "unique": {
          "name": "items_name_unique"
        },
        "up": "name",
        "down": "name"
      }
    }
  ]
}

There is no delay between each batch during the backfill.

I think the problem is how the flags are defined as PersistentFlags in the start and migrate commands.

@@ -141,16 +141,6 @@
}
],
"flags": [
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have expected these flags to have moved, not just removed.

I wonder if the tool that generates this CLI description is working correctly.

Comment on lines 58 to 60
if c.callbacks == nil {
c.callbacks = make([]CallbackFn, 0)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed I think - appending to a nil slice works fine.


// WithCallbacks sets the callbacks for the backfill operation.
// Callbacks are invoked after each batch is processed.
func WithCallbacks(cbs ...CallbackFn) OptionFn {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove this and change its only usage to AddCallback instead. Or remove AddCallback and update its usage to WithCallbacks. We don't need both methods IMO.

cmd/migrate.go Outdated
Comment on lines 84 to 85
migrateCmd.PersistentFlags().Int("backfill-batch-size", backfill.DefaultBatchSize, "Number of rows backfilled in each batch")
migrateCmd.PersistentFlags().Duration("backfill-batch-delay", backfill.DefaultDelay, "Duration of delay between batch backfills (eg. 1s, 1000ms)")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't need to be persistent flags (they don't apply to subcommands, migrate doesn't have any). use .Flags() instead, like the --complete flag.

This might explain why the cli-description.json didn't notice these flags.

cmd/start.go Outdated
Comment on lines 47 to 48
startCmd.PersistentFlags().Int("backfill-batch-size", backfill.DefaultBatchSize, "Number of rows backfilled in each batch")
startCmd.PersistentFlags().Duration("backfill-batch-delay", backfill.DefaultDelay, "Duration of delay between batch backfills (eg. 1s, 1000ms)")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about not needing to use PersistentFlags here as for the migrate command.

batchDelay time.Duration
callbacks []CallbackFn
conn db.DB
config *Config
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config struct could be embedded:

Suggested change
config *Config
*Config

I don't mind either way.

@@ -33,7 +34,7 @@ func TestSchemaIsCreatedAfterMigrationStart(t *testing.T) {
ctx := context.Background()
version := "1_create_table"

if err := mig.Start(ctx, &migrations.Migration{Name: version, Operations: migrations.Operations{createTableOp("table1")}}); err != nil {
if err := mig.Start(ctx, &migrations.Migration{Name: version, Operations: migrations.Operations{createTableOp("table1")}}, backfill.NewConfig()); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of Start could change to:

func (m *Roll) Start(ctx context.Context, migration *migrations.Migration, c ...*backfill.Config) error

so that the config becomes optional and we don't have to do this everywhere.

It's arguably an anti-pattern though. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am leaning towards passing backfill.NewConfig always. The reason why I introduced the config struct is to avoid passing multiple parameters around in different functions. Changing the signature to varargs defeats the purpose of it. We still need to pass as many parameters as we want to set in the configuration.

@kvch kvch requested a review from andrew-farries February 11, 2025 14:50
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flags still don't work for me, using the same reproduction as in this comment:

#660 (review)

@kvch
Copy link
Contributor Author

kvch commented Feb 12, 2025

Fixed the issue

@kvch kvch requested a review from andrew-farries February 12, 2025 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move backfill-batch-delay and backfill-batch-size flags to the start command
2 participants