Skip to content

Commit e4c1df6

Browse files
committed
rivercli: restore ability to set custom DriverProcurer
1 parent 99e45ec commit e4c1df6

5 files changed

Lines changed: 69 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed a riverpro CLI integration point broken in v0.23.0. [PR #945](https://github.com/riverqueue/river/pull/945)
13+
1014
## [0.23.0] - 2025-06-04
1115

1216
⚠️ Internal APIs used for communication between River and River Pro have changed. If using River Pro, make sure to update River and River Pro to latest at the same time to get compatible versions. River v0.23.0 is compatible with River Pro v0.15.0.

cmd/river/rivercli/command.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ type CommandOpts interface {
4646

4747
// RunCommandBundle is a bundle of utilities for RunCommand.
4848
type RunCommandBundle struct {
49-
DatabaseURL *string
50-
Logger *slog.Logger
51-
OutStd io.Writer
52-
Schema string
49+
DatabaseURL *string
50+
DriverProcurer DriverProcurer
51+
Logger *slog.Logger
52+
OutStd io.Writer
53+
Schema string
5354
}
5455

5556
// RunCommand bootstraps and runs a River CLI subcommand.
@@ -76,8 +77,8 @@ func RunCommand[TOpts CommandOpts](ctx context.Context, bundle *RunCommandBundle
7677
}
7778
}
7879

79-
var driverProcurer DriverProcurer
80-
if databaseURL != nil {
80+
driverProcurer := bundle.DriverProcurer
81+
if driverProcurer == nil && databaseURL != nil {
8182
switch protocol {
8283
case "postgres", "postgresql":
8384
dbPool, err := openPgxV5DBPool(ctx, *databaseURL)

cmd/river/rivercli/river_cli.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import (
2929
)
3030

3131
type Config struct {
32+
// DriverProcurer is used to procure a driver for the database. If not
33+
// specified, a default one will be initialized based on the database URL
34+
// scheme.
35+
DriverProcurer DriverProcurer
3236
// Name is the human-friendly named of the executable, used while showing
3337
// version output. Usually this is just "River", but it could be "River
3438
// Pro".
@@ -37,14 +41,16 @@ type Config struct {
3741

3842
// CLI provides a common base of commands for the River CLI.
3943
type CLI struct {
40-
name string
41-
out io.Writer
44+
driverProcurer DriverProcurer
45+
name string
46+
out io.Writer
4247
}
4348

4449
func NewCLI(config *Config) *CLI {
4550
return &CLI{
46-
name: config.Name,
47-
out: os.Stdout,
51+
driverProcurer: config.DriverProcurer,
52+
name: config.Name,
53+
out: os.Stdout,
4854
}
4955
}
5056

@@ -72,10 +78,11 @@ func (c *CLI) BaseCommandSet() *cobra.Command {
7278
// Make a bundle for RunCommand. Takes a database URL pointer because not every command is required to take a database URL.
7379
makeCommandBundle := func(databaseURL *string, schema string) *RunCommandBundle {
7480
return &RunCommandBundle{
75-
DatabaseURL: databaseURL,
76-
Logger: makeLogger(),
77-
OutStd: c.out,
78-
Schema: schema,
81+
DatabaseURL: databaseURL,
82+
DriverProcurer: c.driverProcurer,
83+
Logger: makeLogger(),
84+
OutStd: c.out,
85+
Schema: schema,
7986
}
8087
}
8188

cmd/river/rivercli/river_cli_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212
"time"
1313

14-
"github.com/jackc/pgx/v5"
1514
"github.com/jackc/pgx/v5/pgxpool"
1615
"github.com/spf13/cobra"
1716
"github.com/stretchr/testify/require"
@@ -110,12 +109,6 @@ var (
110109
testMigrationAll = []rivermigrate.Migration{testMigration01, testMigration02, testMigration03} //nolint:gochecknoglobals
111110
)
112111

113-
type TestDriverProcurer struct{}
114-
115-
func (p *TestDriverProcurer) ProcurePgxV5(pool *pgxpool.Pool) riverdriver.Driver[pgx.Tx] {
116-
return riverpgxv5.New(pool)
117-
}
118-
119112
// High level integration tests that operate on the Cobra command directly. This
120113
// isn't always appropriate because there's no way to inject a test transaction.
121114
func TestBaseCommandSetIntegration(t *testing.T) {
@@ -259,6 +252,48 @@ func TestBaseCommandSetNonParallel(t *testing.T) {
259252
})
260253
}
261254

255+
func TestBaseCommandSetDriverProcurer(t *testing.T) {
256+
t.Parallel()
257+
258+
calledStub := false
259+
260+
migratorStub := &MigratorStub{}
261+
migratorStub.allVersionsStub = func() []rivermigrate.Migration { return []rivermigrate.Migration{testMigration01} }
262+
migratorStub.getVersionStub = func(version int) (rivermigrate.Migration, error) {
263+
calledStub = true
264+
if version == 1 {
265+
return testMigration01, nil
266+
}
267+
268+
return rivermigrate.Migration{}, fmt.Errorf("unknown version: %d", version)
269+
}
270+
migratorStub.existingVersionsStub = func(ctx context.Context) ([]rivermigrate.Migration, error) { return nil, nil }
271+
272+
cli := NewCLI(&Config{
273+
DriverProcurer: &DriverProcurerStub{
274+
getMigratorStub: func(config *rivermigrate.Config) (MigratorInterface, error) {
275+
calledStub = true
276+
return migratorStub, nil
277+
},
278+
},
279+
Name: "River",
280+
})
281+
282+
var out bytes.Buffer
283+
cli.SetOut(&out)
284+
285+
cmd := cli.BaseCommandSet()
286+
cmd.SetArgs([]string{"migrate-get", "--up", "--version", "1"})
287+
require.NoError(t, cmd.Execute())
288+
289+
require.True(t, calledStub)
290+
291+
require.Equal(t, strings.TrimSpace(`
292+
-- River main migration 001 [up]
293+
SELECT 'up 1' FROM river_table
294+
`), strings.TrimSpace(out.String()))
295+
}
296+
262297
func TestMigrateGet(t *testing.T) {
263298
t.Parallel()
264299

internal/riverinternaltest/riverdrivertest/riverdrivertest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,7 @@ func Exercise[TTx any](ctx context.Context, t *testing.T,
31053105

31063106
now := time.Now().UTC()
31073107

3108-
leader := testfactory.Leader(ctx, t, exec, &testfactory.LeaderOpts{
3108+
_ = testfactory.Leader(ctx, t, exec, &testfactory.LeaderOpts{
31093109
LeaderID: ptrutil.Ptr(clientID),
31103110
Now: &now,
31113111
})

0 commit comments

Comments
 (0)