Skip to content

Commit 2c895e2

Browse files
Fix CLI progress updates (#390)
Progress updates showing how many rows have been backfilled during migration start were added in #139. Progress updates were then broken in #289. Fix the progress updates to restore the intended behaviour from #139 and add a test to ensure that callbacks are invoked during migration start.
1 parent eba0332 commit 2c895e2

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

pkg/roll/execute.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (m *Roll) Start(ctx context.Context, migration *migrations.Migration, cbs .
2121
}
2222

2323
// perform backfills for the tables that require it
24-
return m.performBackfills(ctx, tablesToBackfill)
24+
return m.performBackfills(ctx, tablesToBackfill, cbs...)
2525
}
2626

2727
// StartDDLOperations performs the DDL operations for the migration. This does
@@ -265,9 +265,9 @@ func (m *Roll) ensureView(ctx context.Context, version, name string, table schem
265265
return nil
266266
}
267267

268-
func (m *Roll) performBackfills(ctx context.Context, tables []*schema.Table) error {
268+
func (m *Roll) performBackfills(ctx context.Context, tables []*schema.Table, cbs ...migrations.CallbackFn) error {
269269
for _, table := range tables {
270-
if err := migrations.Backfill(ctx, m.pgConn, table); err != nil {
270+
if err := migrations.Backfill(ctx, m.pgConn, table, cbs...); err != nil {
271271
errRollback := m.Rollback(ctx)
272272

273273
return errors.Join(

pkg/roll/execute_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,45 @@ func TestMigrationHooksAreInvoked(t *testing.T) {
637637
})
638638
}
639639

640+
func TestCallbacksAreInvokedOnMigrationStart(t *testing.T) {
641+
t.Parallel()
642+
643+
testutils.WithMigratorAndConnectionToContainer(t, func(mig *roll.Roll, db *sql.DB) {
644+
ctx := context.Background()
645+
646+
// Create a table
647+
_, err := db.ExecContext(ctx, "CREATE TABLE users (id SERIAL PRIMARY KEY, name text)")
648+
require.NoError(t, err)
649+
650+
// Insert some data
651+
_, err = db.ExecContext(ctx,
652+
"INSERT INTO users (id, name) VALUES (1, 'alice'), (2, 'bob')")
653+
require.NoError(t, err)
654+
655+
// Define a mock callback
656+
invoked := false
657+
cb := func(n int64) { invoked = true }
658+
659+
// Start a migration that requires a backfill
660+
err = mig.Start(ctx, &migrations.Migration{
661+
Name: "02_change_type",
662+
Operations: migrations.Operations{
663+
&migrations.OpAlterColumn{
664+
Table: "users",
665+
Column: "name",
666+
Type: ptr("varchar(255)"),
667+
Up: "name",
668+
Down: "name",
669+
},
670+
},
671+
}, cb)
672+
require.NoError(t, err)
673+
674+
// Ensure that the callback was invoked
675+
assert.True(t, invoked)
676+
})
677+
}
678+
640679
func TestRollSchemaMethodReturnsCorrectSchema(t *testing.T) {
641680
t.Parallel()
642681

0 commit comments

Comments
 (0)