-
-
Notifications
You must be signed in to change notification settings - Fork 415
BeforeConnect Hook #1875
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
base: v10
Are you sure you want to change the base?
BeforeConnect Hook #1875
Changes from 1 commit
68e8bb8
7b867af
98f3ff4
6186baf
22a4c8d
5a91137
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .idea/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,12 @@ func (db *baseDB) initConn(ctx context.Context, cn *pool.Conn) error { | |
| } | ||
| } | ||
|
|
||
| if db.opt.BeforeConnect != nil { | ||
| if err := db.opt.BeforeConnect(ctx, db.opt); err != nil { | ||
|
||
| return err | ||
| } | ||
| } | ||
|
|
||
| err := db.startup(ctx, cn, db.opt.User, db.opt.Password, db.opt.Database, db.opt.ApplicationName) | ||
| if err != nil { | ||
| return err | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,30 @@ func TestDBConnectWithStartupNotice(t *testing.T) { | |
| require.NoError(t, db.Ping(context.Background()), "must successfully ping database with long application name") | ||
| } | ||
|
|
||
| func TestBeforeConnect(t *testing.T) { | ||
|
Collaborator
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. I would add another test that tests the before connect with multiple go routines to make sure that a race condition is not triggered by accident. A race condition in a before connection hook for an ORM could cripple an application.
Collaborator
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. I think a good way to do this would be to set the options to have a max pool of 1. And then try to send multiple queries concurrently in a few go routines. This should trigger a concurrent call of the BeforeConnect method; and given the |
||
| pwd := "dynamic-passwords-from-xkcd" | ||
| opt := pgOptions() | ||
| opt.BeforeConnect = func(ctx context.Context, o *pg.Options) error { | ||
| o.Password = pwd | ||
| return nil | ||
| } | ||
|
|
||
| db := pg.Connect(opt) | ||
| defer db.Close() | ||
|
|
||
| var val int | ||
| _, err := db.QueryOne(pg.Scan(&val), "SELECT 1") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if val != 1 { | ||
| t.Fatalf(`got %q, wanted 1`, val) | ||
| } | ||
| if pwd != opt.Password { | ||
| t.Fatalf(`got %s, wanted %s`, opt.Password, pwd) | ||
| } | ||
| } | ||
|
|
||
| func TestOnConnect(t *testing.T) { | ||
| opt := pgOptions() | ||
| opt.OnConnect = func(ctx context.Context, db *pg.Conn) error { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.