-
Notifications
You must be signed in to change notification settings - Fork 848
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
pgxpool.Pool / pgxpool.Conn minimal interfaces #644
Comments
It is very useful. I typically implement my DB code in terms of the following interface: type dbconn interface {
Begin(ctx context.Context) (pgx.Tx, error)
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row
} It's also useful because the application code will work with a pgxpool.Pool, pgxpool.Conn, pgx.Conn, or a pgx.Tx. The pgx.Tx is particularly useful for testing because that enables transactional tests where any changes can be rolled back at the end of the test. All that said, as a general rule this type of interface should not be implemented in the library itself. In general a library should only expose interfaces that it consumes -- i.e. return structs and accept interfaces. Unfortunately, implementation concerns compelled violating this rule for pgx.Tx and pgx.Rows but I try to follow the rule when possible. |
wouldn't be easier to have that An example of where the interface has been provided and exposed from within the library itself is streadway/amqp#387 I apologies in advance if what I said above is incorrect - I might be wrong |
There's two downsides to including it in the library. First, if any new methods are added to the common method set of pgxpool.Pool, pgx.Conn, and pgx.Tx it couldn't be added to this interface without breaking backwards compatibility. i.e. Adding methods to a public interface is a breaking change. It is possible to document particular interfaces as outside the comparability guarantee (like pgx.Rows) but I prefer to avoid this when possible. The second is that this interface would be broader than most people would use. The true common method set for this interface is bigger than my sample |
I see your points - what you are referring to is known as interface pollution and can be avoided by defining smaller interfaces which can be composed together |
Would it be useful to have minimal interfaces for pgxpool.Pool / pgxpool.Conn for mocking/testing?
The text was updated successfully, but these errors were encountered: