-
Notifications
You must be signed in to change notification settings - Fork 84
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
base: main
Are you sure you want to change the base?
Conversation
08a7668
to
ae056d6
Compare
ae056d6
to
4dcd097
Compare
There was a problem hiding this 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": [ | |||
{ |
There was a problem hiding this comment.
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.
pkg/backfill/config.go
Outdated
if c.callbacks == nil { | ||
c.callbacks = make([]CallbackFn, 0) | ||
} |
There was a problem hiding this comment.
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.
pkg/backfill/config.go
Outdated
|
||
// WithCallbacks sets the callbacks for the backfill operation. | ||
// Callbacks are invoked after each batch is processed. | ||
func WithCallbacks(cbs ...CallbackFn) OptionFn { |
There was a problem hiding this comment.
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
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)") |
There was a problem hiding this comment.
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
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)") |
There was a problem hiding this comment.
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.
pkg/backfill/backfill.go
Outdated
batchDelay time.Duration | ||
callbacks []CallbackFn | ||
conn db.DB | ||
config *Config |
There was a problem hiding this comment.
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:
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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:
Fixed the issue |
This PR moves the flags
backfill-batch-delay
andbackfill-batch-size
tothe commands
start
andmigrate
. So these options no longer show up in the listof global flags.
I created a new struct
backfill.Config
to store and pass the options downto the migration runner. I also moved the default values for batch delay and batch size
into the package
backfill
. I renamedoptions.go
toconfig.go
because it is a better name given the new contents of the file.Closes #657