-
Notifications
You must be signed in to change notification settings - Fork 92
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
Allow River client to be created with nil database pool #30
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,8 +309,9 @@ func (ts *clientTestSignals) Init() { | |
} | ||
|
||
var ( | ||
errMissingConfig = errors.New("missing config") | ||
errMissingDriver = errors.New("missing database driver (try wrapping a Pgx pool with river/riverdriver/riverpgxv5.New)") | ||
errMissingConfig = errors.New("missing config") | ||
errMissingDatabasePoolWithQueues = errors.New("must have a non-nil database pool to execute jobs (either use a driver with database pool or don't configure Queues)") | ||
errMissingDriver = errors.New("missing database driver (try wrapping a Pgx pool with river/riverdriver/riverpgxv5.New)") | ||
) | ||
|
||
// NewClient creates a new Client with the given database driver and | ||
|
@@ -434,6 +435,10 @@ func NewClient[TTx any](driver riverdriver.Driver[TTx], config *Config) (*Client | |
// we're actually going to be working jobs (as opposed to just enqueueing | ||
// them): | ||
if config.willExecuteJobs() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also make it a config error to specify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, so that occurred to me as well, but I was 50/50 on the best option. The slight downside of erroring on Let's try it out though. Pushed. |
||
if driver.GetDBPool() == nil { | ||
return nil, errMissingDatabasePoolWithQueues | ||
} | ||
|
||
// TODO: for now we only support a single instance per database/schema. | ||
// If we want to provide isolation within a single database/schema, | ||
// we'll need to add a config for this. | ||
|
@@ -967,6 +972,8 @@ func insertParamsFromArgsAndOptions(args JobArgs, insertOpts *InsertOpts) (*dbad | |
return insertParams, nil | ||
} | ||
|
||
var errInsertNoDriverDBPool = fmt.Errorf("driver must have non-nil database pool to use Insert and InsertMany (try InsertTx or InsertManyTx instead") | ||
|
||
// Insert inserts a new job with the provided args. Job opts can be used to | ||
// override any defaults that may have been provided by an implementation of | ||
// JobArgsWithInsertOpts.InsertOpts, as well as any global defaults. The | ||
|
@@ -978,6 +985,10 @@ func insertParamsFromArgsAndOptions(args JobArgs, insertOpts *InsertOpts) (*dbad | |
// // handle error | ||
// } | ||
func (c *Client[TTx]) Insert(ctx context.Context, args JobArgs, opts *InsertOpts) (*JobRow, error) { | ||
if c.driver.GetDBPool() == nil { | ||
return nil, errInsertNoDriverDBPool | ||
} | ||
|
||
if err := c.validateJobArgs(args); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -1053,6 +1064,10 @@ type InsertManyParams struct { | |
// // handle error | ||
// } | ||
func (c *Client[TTx]) InsertMany(ctx context.Context, params []InsertManyParams) (int64, error) { | ||
if c.driver.GetDBPool() == nil { | ||
return 0, errInsertNoDriverDBPool | ||
} | ||
|
||
insertParams, err := c.insertManyParams(params) | ||
if err != nil { | ||
return 0, err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package riverpgxv5 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
t.Run("AllowsNilDatabasePool", func(t *testing.T) { | ||
dbPool := &pgxpool.Pool{} | ||
driver := New(dbPool) | ||
require.Equal(t, dbPool, driver.dbPool) | ||
}) | ||
|
||
t.Run("AllowsNilDatabasePool", func(t *testing.T) { | ||
driver := New(nil) | ||
require.Nil(t, driver.dbPool) | ||
}) | ||
} |
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.
TIL
go test ./...
will not run submodule tests and evengo test ./riverdriver/riverpgxv5
will refuse to work.