Closed arp242 closed 2 months ago
@arp242 The db
you get with sql.Open
is a connection pool. When you do this:
_, err = db.Exec("set search_path to myschema")
f(err)
You're just setting the search path on a connection that may or may not still be around later on. It's not sticky, and no other connections procured will have the setting.
What you want to do is set search_path
as a connection parameter. Here's a working version of your repro code:
diff --git a/main.go b/main.go
index 75f48a8..646536a 100644
--- a/main.go
+++ b/main.go
@@ -19,14 +19,14 @@ func f(err error) {
}
func main() {
- db, err := sql.Open("pgx", "postgresql:///river-schema-test")
+ db, err := sql.Open("pgx", "postgresql:///river-schema-test?search_path=myschema")
f(err)
f(db.Ping())
- _, err = db.Exec("create schema if not exists myschema")
- f(err)
- _, err = db.Exec("set search_path to myschema")
- f(err)
+ // _, err = db.Exec("create schema if not exists myschema")
+ // f(err)
+ // _, err = db.Exec("set search_path to myschema")
+ // f(err)
_, err = rivermigrate.New(riverdatabasesql.New(db), nil).
Migrate(context.Background(), rivermigrate.DirectionUp, nil)
Output:
$ go run main.go
TEST WORKER: &{0x14000126b40 {}}
TEST WORKER: &{0x14000126c60 {}}
It seems that the background worker doesn't work when setting a different schema, and it always uses the public schema. I saw there was a fix for this recently (#505), and the migration does run correctly. After running the code below I have:
Which is expected, because that's what I set the
search_path
to. But actually running jobs doesn't work, and errors out with:It does seem to insert the jobs correctly:
But the rc.Start() background worker uses the wrong schema. If I manually create the tables on the public schema then it doesn't error out (but also doesn't do anything, because there are no jobs).
All of this is with 0.11.2 and ee3e5b37.
Code to reproduce:
With go.mod: