-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow
PG*
env vars as an alternative to DATABASE_URL
Accept the standard set of `PG*` env vars as an alternative to database configuration (e.g. `PGHOST`, `PGDATABASE`, etc.). This is mostly driven by having done something similar for the CLI in [1], but was also requested in #249. This turns out to be quite easy to do because pgx does all the heavy lifting. As noted in [1], a bonus of this is that it adds some additional configuration options that aren't very easily doable right now, for example around the use of an SSL certificate to connect to Postgres. We get automatic support for these vars: * `PGSSLCERT` * `PGSSLKEY` * `PGSSLROOTCERT` * `PGSSLPASSWORD` As part of this I also ended up rearranging some things in `main.go`. Not strongly married to this design, but the idea is to get it into a place where we can write tests for it, which previously wasn't possible. [1] riverqueue/river#702
- Loading branch information
Showing
3 changed files
with
146 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/riverqueue/river/rivershared/riversharedtest" | ||
) | ||
|
||
func TestInitServer(t *testing.T) { | ||
var ( | ||
ctx = context.Background() | ||
databaseURL = cmp.Or(os.Getenv("TEST_DATABASE_URL"), "postgres://localhost/river_test") | ||
) | ||
|
||
t.Setenv("DEV", "true") | ||
|
||
type testBundle struct{} | ||
|
||
setup := func(t *testing.T) (*initServerResult, *testBundle) { | ||
t.Helper() | ||
|
||
initRes, err := initServer(ctx, riversharedtest.Logger(t), "/") | ||
require.NoError(t, err) | ||
t.Cleanup(initRes.dbPool.Close) | ||
|
||
return initRes, &testBundle{} | ||
} | ||
|
||
t.Run("WithDatabaseURL", func(t *testing.T) { | ||
t.Setenv("DATABASE_URL", databaseURL) | ||
|
||
initRes, _ := setup(t) | ||
|
||
_, err := initRes.dbPool.Exec(ctx, "SELECT 1") | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("WithPGEnvVars", func(t *testing.T) { | ||
parsedURL, err := url.Parse(databaseURL) | ||
require.NoError(t, err) | ||
|
||
t.Setenv("PGDATABASE", parsedURL.Path[1:]) | ||
t.Setenv("PGHOST", parsedURL.Hostname()) | ||
pass, _ := parsedURL.User.Password() | ||
t.Setenv("PGPASSWORD", pass) | ||
t.Setenv("PGPORT", cmp.Or(parsedURL.Port(), "5432")) | ||
t.Setenv("PGSSLMODE", parsedURL.Query().Get("sslmode")) | ||
t.Setenv("PGUSER", parsedURL.User.Username()) | ||
|
||
initRes, _ := setup(t) | ||
|
||
_, err = initRes.dbPool.Exec(ctx, "SELECT 1") | ||
require.NoError(t, err) | ||
}) | ||
} |