Skip to content

Commit 9e8fc69

Browse files
authored
Makefile make db/reset target + rename everything to use river_test convention (#450)
A couple small ones: * I often find myself wanting to reset my dev database from scratch, and it's quite inconvenient to do because you want to use the current code (rather than installed River CLI), and doing so requires cding into the `./cmd/river` directory because `go run` won't let you run a directory that's not in the current project. Here, add a `make db/reset` that'll easily reset the dev database, test databases, or both, all at once. * While we're at it, change over to the naming convention of `river_test` established in the River Ruby and River Python projects.
1 parent 0eba96b commit 9e8fc69

28 files changed

+60
-45
lines changed

Diff for: .github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ env:
88
DATABASE_URL: postgres://postgres:[email protected]:5432/river_dev?sslmode=disable
99

1010
# Test database.
11-
TEST_DATABASE_URL: postgres://postgres:[email protected]:5432/river_testdb?sslmode=disable
11+
TEST_DATABASE_URL: postgres://postgres:[email protected]:5432/river_test?sslmode=disable
1212

1313
on:
1414
push:

Diff for: Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ generate:
33
generate: generate/migrations
44
generate: generate/sqlc
55

6+
.PHONY: db/reset
7+
db/reset: ## drop, create, and migrate dev and test databases
8+
db/reset: db/reset/dev
9+
db/reset: db/reset/test
10+
11+
.PHONY: db/reset/dev
12+
db/reset/dev: ## drop, create, and migrate dev database
13+
dropdb river_dev --force --if-exists
14+
createdb river_dev
15+
cd cmd/river && go run . migrate-up --database-url "postgres://localhost/river_dev"
16+
17+
.PHONY: db/reset/test
18+
db/reset/test: ## drop, create, and migrate test databases
19+
go run ./internal/cmd/testdbman reset
20+
621
.PHONY: generate/migrations
722
generate/migrations: ## sync changes of pgxv5 migrations to database/sql
823
rsync -au --delete "riverdriver/riverpgxv5/migration/" "riverdriver/riverdatabasesql/migration/"

Diff for: example_batch_insert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (w *BatchInsertWorker) Work(ctx context.Context, job *river.Job[BatchInsert
3333
func Example_batchInsert() {
3434
ctx := context.Background()
3535

36-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
36+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
3737
if err != nil {
3838
panic(err)
3939
}

Diff for: example_complete_job_within_tx_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (w *TransactionalWorker) Work(ctx context.Context, job *river.Job[Transacti
5858
func Example_completeJobWithinTx() {
5959
ctx := context.Background()
6060

61-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
61+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
6262
if err != nil {
6363
panic(err)
6464
}

Diff for: example_cron_job_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (w *CronJobWorker) Work(ctx context.Context, job *river.Job[CronJobArgs]) e
3535
func Example_cronJob() {
3636
ctx := context.Background()
3737

38-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
38+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
3939
if err != nil {
4040
panic(err)
4141
}

Diff for: example_custom_insert_opts_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (w *SometimesHighPriorityWorker) Work(ctx context.Context, job *river.Job[S
5656
func Example_customInsertOpts() {
5757
ctx := context.Background()
5858

59-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
59+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
6060
if err != nil {
6161
panic(err)
6262
}

Diff for: example_error_handler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (w *ErroringWorker) Work(ctx context.Context, job *river.Job[ErroringArgs])
6161
func Example_errorHandler() {
6262
ctx := context.Background()
6363

64-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
64+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
6565
if err != nil {
6666
panic(err)
6767
}

Diff for: example_graceful_shutdown_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (w *WaitsForCancelOnlyWorker) Work(ctx context.Context, job *river.Job[Wait
5151
func Example_gracefulShutdown() {
5252
ctx := context.Background()
5353

54-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
54+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
5555
if err != nil {
5656
panic(err)
5757
}

Diff for: example_insert_and_work_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (w *SortWorker) Work(ctx context.Context, job *river.Job[SortArgs]) error {
3636
func Example_insertAndWork() {
3737
ctx := context.Background()
3838

39-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
39+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
4040
if err != nil {
4141
panic(err)
4242
}

Diff for: example_job_cancel_from_client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (w *SleepingWorker) Work(ctx context.Context, job *river.Job[CancellingArgs
3838
func Example_jobCancelFromClient() {
3939
ctx := context.Background()
4040

41-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
41+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
4242
if err != nil {
4343
panic(err)
4444
}

Diff for: example_job_cancel_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (w *CancellingWorker) Work(ctx context.Context, job *river.Job[CancellingAr
3737
func Example_jobCancel() { //nolint:dupl
3838
ctx := context.Background()
3939

40-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
40+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
4141
if err != nil {
4242
panic(err)
4343
}

Diff for: example_job_snooze_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (w *SnoozingWorker) Work(ctx context.Context, job *river.Job[SnoozingArgs])
3939
func Example_jobSnooze() { //nolint:dupl
4040
ctx := context.Background()
4141

42-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
42+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
4343
if err != nil {
4444
panic(err)
4545
}

Diff for: example_periodic_job_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (w *PeriodicJobWorker) Work(ctx context.Context, job *river.Job[PeriodicJob
3333
func Example_periodicJob() {
3434
ctx := context.Background()
3535

36-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
36+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
3737
if err != nil {
3838
panic(err)
3939
}

Diff for: example_queue_pause_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (w *ReportingWorker) Work(ctx context.Context, job *river.Job[ReportingArgs
3737
func Example_queuePause() {
3838
ctx := context.Background()
3939

40-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
40+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
4141
if err != nil {
4242
panic(err)
4343
}

Diff for: example_scheduled_job_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (w *ScheduledWorker) Work(ctx context.Context, job *river.Job[ScheduledArgs
3434
func Example_scheduledJob() {
3535
ctx := context.Background()
3636

37-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
37+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
3838
if err != nil {
3939
panic(err)
4040
}

Diff for: example_subscription_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (w *SubscriptionWorker) Work(ctx context.Context, job *river.Job[Subscripti
4242
func Example_subscription() {
4343
ctx := context.Background()
4444

45-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
45+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
4646
if err != nil {
4747
panic(err)
4848
}

Diff for: example_unique_job_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (w *ReconcileAccountWorker) Work(ctx context.Context, job *river.Job[Reconc
6464
func Example_uniqueJob() {
6565
ctx := context.Background()
6666

67-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
67+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
6868
if err != nil {
6969
panic(err)
7070
}

Diff for: example_work_func_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (WorkFuncArgs) Kind() string { return "work_func" }
2525
func Example_workFunc() {
2626
ctx := context.Background()
2727

28-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
28+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
2929
if err != nil {
3030
panic(err)
3131
}

Diff for: internal/cmd/producersample/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func getDatabaseURL() string {
193193
if envURL := os.Getenv("DATABASE_URL"); envURL != "" {
194194
return envURL
195195
}
196-
return "postgres:///river_testdb_example?sslmode=disable"
196+
return "postgres:///river_test_example?sslmode=disable"
197197
}
198198

199199
type MyJobArgs struct {

Diff for: internal/cmd/testdbman/main.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ test run.
4545
Creates the test databases used by parallel tests and the sample applications.
4646
Each is migrated with River's current schema.
4747
48-
The sample application DB is named river_testdb, while the DBs for parallel
49-
tests are named river_testdb_0, river_testdb_1, etc. up to the larger of 4 or
48+
The sample application DB is named river_test, while the DBs for parallel
49+
tests are named river_test_0, river_test_1, etc. up to the larger of 4 or
5050
runtime.NumCPU() (a choice that comes from pgx's default connection pool size).
5151
`,
5252
createTestDatabases,
@@ -60,8 +60,8 @@ runtime.NumCPU() (a choice that comes from pgx's default connection pool size).
6060
"Drop test databases",
6161
`
6262
Drops all test databases. Any test database matching the base name
63-
(river_testdb) or the base name with an underscore followed by any other token
64-
(river_testdb_example, river_testdb_0, river_testdb_1, etc.) will be dropped.
63+
(river_test) or the base name with an underscore followed by any other token
64+
(river_test_example, river_test_0, river_test_1, etc.) will be dropped.
6565
`,
6666
dropTestDatabases,
6767
)
@@ -152,12 +152,12 @@ func createTestDatabases(ctx context.Context, out io.Writer) error {
152152

153153
func generateTestDBNames(numDBs int) []string {
154154
dbNames := []string{
155-
"river_testdb",
156-
"river_testdb_example",
155+
"river_test",
156+
"river_test_example",
157157
}
158158

159159
for i := 0; i < numDBs; i++ {
160-
dbNames = append(dbNames, fmt.Sprintf("river_testdb_%d", i))
160+
dbNames = append(dbNames, fmt.Sprintf("river_test_%d", i))
161161
}
162162

163163
return dbNames
@@ -188,7 +188,7 @@ func dropTestDatabases(ctx context.Context, out io.Writer) error {
188188
rows.Close()
189189

190190
for _, dbName := range allDBNames {
191-
if strings.HasPrefix(dbName, "river_testdb") {
191+
if strings.HasPrefix(dbName, "river_test") {
192192
if _, err := mgmtConn.Exec(ctx, "DROP DATABASE "+dbName); err != nil {
193193
return fmt.Errorf("error dropping database %q: %w", dbName, err)
194194
}

Diff for: internal/cmd/testdbman/main_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ func TestGenerateTestDBNames(t *testing.T) {
1616
t.Parallel()
1717

1818
require.Equal(t, []string{
19-
"river_testdb",
20-
"river_testdb_example",
21-
"river_testdb_0",
22-
"river_testdb_1",
23-
"river_testdb_2",
24-
"river_testdb_3",
19+
"river_test",
20+
"river_test_example",
21+
"river_test_0",
22+
"river_test_1",
23+
"river_test_2",
24+
"river_test_3",
2525
}, generateTestDBNames(4))
2626
}
2727

Diff for: internal/riverinternaltest/riverinternaltest.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func DatabaseConfig(databaseName string) *pgxpool.Config {
6060
}
6161

6262
// DatabaseURL gets a test database URL from TEST_DATABASE_URL or falls back on
63-
// a default pointing to `river_testdb`. If databaseName is set, it replaces the
63+
// a default pointing to `river_test`. If databaseName is set, it replaces the
6464
// database in the URL, although the host and other parameters are preserved.
6565
//
6666
// Most of the time DatabaseConfig should be used instead of this function, but
@@ -69,7 +69,7 @@ func DatabaseConfig(databaseName string) *pgxpool.Config {
6969
func DatabaseURL(databaseName string) string {
7070
parsedURL, err := url.Parse(valutil.ValOrDefault(
7171
os.Getenv("TEST_DATABASE_URL"),
72-
"postgres://localhost/river_testdb?sslmode=disable"),
72+
"postgres://localhost/river_test?sslmode=disable"),
7373
)
7474
if err != nil {
7575
panic(err)
@@ -215,7 +215,7 @@ func TestTx(ctx context.Context, tb testing.TB) pgx.Tx {
215215
}
216216

217217
var err error
218-
dbPool, err = pgxpool.NewWithConfig(ctx, DatabaseConfig("river_testdb"))
218+
dbPool, err = pgxpool.NewWithConfig(ctx, DatabaseConfig("river_test"))
219219
require.NoError(tb, err)
220220

221221
return dbPool
@@ -283,7 +283,7 @@ func TruncateRiverTables(ctx context.Context, pool *pgxpool.Pool) error {
283283
// and checks for no goroutine leaks on teardown.
284284
func WrapTestMain(m *testing.M) {
285285
var err error
286-
dbManager, err = testdb.NewManager(DatabaseConfig("river_testdb"), dbPoolMaxConns, nil, TruncateRiverTables)
286+
dbManager, err = testdb.NewManager(DatabaseConfig("river_test"), dbPoolMaxConns, nil, TruncateRiverTables)
287287
if err != nil {
288288
log.Fatal(err)
289289
}

Diff for: internal/testdb/manager_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func getTestDatabaseURL() string {
1616
if envURL := os.Getenv("TEST_DATABASE_URL"); envURL != "" {
1717
return envURL
1818
}
19-
return "postgres:///river_testdb?sslmode=disable"
19+
return "postgres:///river_test?sslmode=disable"
2020
}
2121

2222
func testConfig(t *testing.T) *pgxpool.Config {
@@ -47,15 +47,15 @@ func TestManager_AcquireMultiple(t *testing.T) {
4747
}
4848
defer pool0.Release()
4949

50-
checkDBNameForPool(ctx, t, pool0, "river_testdb_")
50+
checkDBNameForPool(ctx, t, pool0, "river_test_")
5151

5252
pool1, err := manager.Acquire(ctx)
5353
if err != nil {
5454
t.Fatal(err)
5555
}
5656
defer pool1.Release()
5757

58-
checkDBNameForPool(ctx, t, pool1, "river_testdb_")
58+
checkDBNameForPool(ctx, t, pool1, "river_test_")
5959
pool0.Release()
6060

6161
// ensure we get db 0 back on subsequent acquire since it was released to the pool:
@@ -65,7 +65,7 @@ func TestManager_AcquireMultiple(t *testing.T) {
6565
}
6666
defer pool0Again.Release()
6767

68-
checkDBNameForPool(ctx, t, pool0Again, "river_testdb_")
68+
checkDBNameForPool(ctx, t, pool0Again, "river_test_")
6969
pool0Again.Release()
7070
pool1.Release()
7171

Diff for: riverdriver/riverpgxv5/river_pgx_v5_driver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (c *connStub) Close() error {
118118
}
119119

120120
func testPoolConfig() *pgxpool.Config {
121-
databaseURL := "postgres://localhost/river_testdb?sslmode=disable"
121+
databaseURL := "postgres://localhost/river_test?sslmode=disable"
122122
if url := os.Getenv("TEST_DATABASE_URL"); url != "" {
123123
databaseURL = url
124124
}

Diff for: rivermigrate/example_migrate_database_sql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func Example_migrateDatabaseSQL() {
1919
ctx := context.Background()
2020

21-
dbPool, err := sql.Open("pgx", riverinternaltest.DatabaseURL("river_testdb_example"))
21+
dbPool, err := sql.Open("pgx", riverinternaltest.DatabaseURL("river_test_example"))
2222
if err != nil {
2323
panic(err)
2424
}

Diff for: rivermigrate/example_migrate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func Example_migrate() {
1818
ctx := context.Background()
1919

20-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
20+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
2121
if err != nil {
2222
panic(err)
2323
}

Diff for: rivertest/example_require_inserted_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (w *RequiredWorker) Work(ctx context.Context, job *river.Job[RequiredArgs])
3232
func Example_requireInserted() {
3333
ctx := context.Background()
3434

35-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
35+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
3636
if err != nil {
3737
panic(err)
3838
}

Diff for: rivertest/example_require_many_inserted_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (w *SecondRequiredWorker) Work(ctx context.Context, job *river.Job[SecondRe
4949
func Example_requireManyInserted() {
5050
ctx := context.Background()
5151

52-
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
52+
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_test_example"))
5353
if err != nil {
5454
panic(err)
5555
}

0 commit comments

Comments
 (0)