Skip to content

Commit 11d80e0

Browse files
authored
When getting tables to truncate, panic on unknown version (#1198)
This one's related to code review feedback here [1]. When getting a list of tables that need to be truncated for the `TestSchema` helpers, it was possible to receive migration versions that didn't exist and the function would tolerate it, returning the latest known list of tables. This often works, but even where it does there's some risk of a new table coming in that doesn't end up getting cleaned up properly. Here, panic when receiving an unknown version number so that the problem can be corrected immediately. The panic may seem undesirable, but these functions are only used in tests, so it should be fine. [1] riverqueue/riverpro#281 (comment)
1 parent fd65906 commit 11d80e0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

riverdriver/river_driver_interface.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package riverdriver
1515
import (
1616
"context"
1717
"errors"
18+
"fmt"
1819
"io/fs"
1920
"time"
2021

@@ -865,15 +866,17 @@ type TableTruncateParams struct {
865866
//
866867
// API is not stable. DO NOT USE.
867868
func MigrationLineMainTruncateTables(version int) []string {
869+
// 0 value must be handled and should always point to latest migration version
868870
switch version {
869871
case 1:
870872
return nil // don't truncate `river_migrate`
871873
case 2, 3:
872874
return []string{"river_job", "river_leader"}
873875
case 4:
874876
return []string{"river_job", "river_leader", "river_queue"}
877+
case 0, 5, 6:
878+
return []string{"river_job", "river_leader", "river_queue", "river_client", "river_client_queue"}
875879
}
876880

877-
// 0 (zero value), 5, 6
878-
return []string{"river_job", "river_leader", "river_queue", "river_client", "river_client_queue"}
881+
panic(fmt.Sprintf("unrecognized migration version: %d", version))
879882
}

riverdriver/river_driver_interface_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,19 @@ func TestJobSetStateSnoozed(t *testing.T) { //nolint:dupl
250250
})
251251
}
252252

253+
func TestMigrationLineMainTruncateTables(t *testing.T) {
254+
t.Parallel()
255+
256+
t.Run("ZeroValueDoesNotPanic", func(t *testing.T) {
257+
t.Parallel()
258+
259+
require.NotPanics(t, func() {
260+
tables := MigrationLineMainTruncateTables(0)
261+
require.NotEmpty(t, tables)
262+
})
263+
})
264+
}
265+
253266
func TestJobSetStateSnoozedAvailable(t *testing.T) { //nolint:dupl
254267
t.Parallel()
255268

0 commit comments

Comments
 (0)