Skip to content
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

streamline the database interfaces and add pgx adapter #29

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

julesjcraske
Copy link

@julesjcraske julesjcraske commented Feb 8, 2024

I am opening this draft PR to see if you are interested in this direction for the library (trimming down the interfaces to only what is used). If so I can put the effort in to get it up to your required standards and resolve the final issues. If not I can close it and keep my fork.

Thanks for all the great work you have done on the lib so far!

I want to use watermill to fulfil the outbox pattern (persisting events that will be asynchronously broadcast as part of the main persistence transaction) but my company mainly uses the pgx library.

This change makes this library more flexible to allow for the use of the pgx library. I can keep the pgx adapter itself elsewhere, outside of this repo if you would like.

  • Strips out all of the unused methods declared in the database Beginner and Executor (totally unused) and ContextExecutor interfaces.
  • Adds interfaces for the Exec Result type and the Query Rows type.
  • Adds small plumbing adapters for the StdSQL and Pgx types

Still to do:

  • Make it so that it is backwards compatible initialising a Publisher or Subscriber with a raw StdSQL connection or transaction.
  • Add to documentation.
  • Add tests against a pgx adapter.

}

// BeginTx converts the stdSQL.Tx struct to our Tx interface
func (c *StdSQLBeginner) BeginTx(ctx context.Context, options *sql.TxOptions) (Tx, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making these value receivers instead of pointers? There should be no change performance-wise since the embedded values are pointers anyway, and it would make for a bit simpler construction of these. :)

@m110
Copy link
Member

m110 commented Aug 23, 2024

Thanks for contributing @julesjcraske and I'm sorry it took us so long to reply!

This looks great, and I since we're getting more people asking about using specific database frameworks with the outbox pattern, I'm happy to go ahead with it. Since you changed the public interfaces anyway, I think we can't keep backward compatibility and will need to bump major version, unless you see some way?

lyubchev added a commit to Integer-Technologies-B-V/watermill-sql that referenced this pull request Aug 26, 2024
@julesjcraske
Copy link
Author

julesjcraske commented Sep 3, 2024

I think we can't keep backward compatibility and will need to bump major version, unless you see some way?

Thanks for taking a look, its great to here you are interested in the concept. I will try and find time to explore the options backwards compat and finishing touches on the PR over the next few weeks.

Comment on lines 81 to 93
func (t Tx) Rollback() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

return t.Tx.Rollback(ctx)
}

func (t Tx) Commit() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

return t.Tx.Commit(ctx)
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the stdSQL interface this interface uses doesn't take a context, it looks like I decided to create a context and add a timeout here.

I don't want to refactor too much to make context available here in this PR.

  1. I could just use a background context with no timeout
  2. I could make the timeout configurable when creating this adapter
  3. I could keep a hardcoded default timeout as it is currently (I don't like this as much)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants