pgpass doesn't work with builder methods
Bug Description
PgConnectionOptions tries to get the password from the pgpass file when the struct is created, but the caller can then use the builder API to change options that affect which password is taken from the file, so we can end up with the wrong password.
https://github.com/launchbadge/sqlx/blob/7e7dded8afd93fd74d6d6f65cd9187fea78b4d0f/sqlx-postgres/src/options/mod.rs#L136-L138 https://github.com/launchbadge/sqlx/blob/7e7dded8afd93fd74d6d6f65cd9187fea78b4d0f/sqlx-postgres/src/options/mod.rs#L174-L185
For example, we can select a different host here and it will not read the pgpass file again (and even if it did, it might already have gotten the wrong password and would not change it):
https://github.com/launchbadge/sqlx/blob/7e7dded8afd93fd74d6d6f65cd9187fea78b4d0f/sqlx-postgres/src/options/mod.rs#L203-L206
Info
- SQLx version: 0.7.1
- SQLx features enabled: postgres
- Database server and version: Postgres
I think the pgpass password should be fetched when connecting, not when creating the options. But the connect() method takes an immutable reference to the options, so I am not sure of where this call could be made.
The password is only used in these 3 match cases:
https://github.com/launchbadge/sqlx/blob/7e7dded8afd93fd74d6d6f65cd9187fea78b4d0f/sqlx-postgres/src/connection/establish.rs#L73-L101
So I suppose each of them could have a call to the method that reads the pgpass file if the pgpass option is enabled and the password is empty. And I guess the Pool would have to be able to call a method to fill this password only once, so the file isn't read again every time a new connection is made.
Another possibility would be to make the apply_pgpass() method public and add a clear_password() method to the options, so the user could call it manually when using the builder methods.
Let me know whether a pull request for either of these options would be accepted, or if there are any better alternatives.
Running into this issue myself, and it took me awhile to figure out why. I would be a big fan of exposing the apply_pgpass method publicly!
Just leaving this comment so as to indicate interest in the issue.