-
Notifications
You must be signed in to change notification settings - Fork 80
Add lookup_choice transformer
#1148
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
Open
kvch
wants to merge
2
commits into
main
Choose a base branch
from
afk-triage-transformer-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package builder | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/jackc/pgx/v5/pgtype" | ||
| "github.com/xataio/pgstream/pkg/transformers/internal/lookup" | ||
| ) | ||
|
|
||
| // lookupTestValues is what lookup_choice reads in this package's tests | ||
| var lookupTestValues = []any{int64(1), int64(2)} | ||
|
|
||
| // lookup_choice reads its values while it is being built, and the tables in | ||
| // this package build every registered transformer. The seam is installed here | ||
| // rather than inside a test because those tests run in parallel and would | ||
| // otherwise race each other writing it. | ||
| func TestMain(m *testing.M) { | ||
| lookup.NewQuerier = lookup.StubQuerier(pgtype.Int8OID, lookupTestValues, lookup.StubOptions{}) | ||
| os.Exit(m.Run()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package lookup holds the connection seam used by transformers that read | ||
| // their values from the database when they are built. It exists as its own | ||
| // package because pkg/transformers/builder's table driven tests construct | ||
| // every registered transformer and have no database, and they cannot reach an | ||
| // unexported variable in pkg/transformers. | ||
| package lookup | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| pglib "github.com/xataio/pgstream/internal/postgres" | ||
| ) | ||
|
|
||
| // NewQuerier opens the connection a lookup load runs on. It is a variable so | ||
| // that tests can build lookup based transformers without a database; replace | ||
| // it with StubQuerier. | ||
| var NewQuerier = func(ctx context.Context, url string) (pglib.Querier, error) { | ||
| return pglib.NewConnPool(ctx, url) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package lookup | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/jackc/pgx/v5/pgconn" | ||
| pglib "github.com/xataio/pgstream/internal/postgres" | ||
| pglibmocks "github.com/xataio/pgstream/internal/postgres/mocks" | ||
| ) | ||
|
|
||
| // StubOptions configures the failure a StubQuerier injects. The zero value | ||
| // serves the values successfully. | ||
| type StubOptions struct { | ||
| NewQuerierErr error | ||
| QueryErr error | ||
| ScanErr error | ||
| RowsErr error | ||
| // Closed reports whether the querier and its rows were closed | ||
| Closed *bool | ||
| // Query captures the SQL the loader built | ||
| Query *string | ||
| } | ||
|
|
||
| // StubQuerier returns a NewQuerier replacement serving the given values as a | ||
| // single column of the given pg type OID. It lives beside the seam so that | ||
| // every package testing a lookup based transformer shares one test double. | ||
| func StubQuerier(oid uint32, values []any, opts StubOptions) func(context.Context, string) (pglib.Querier, error) { | ||
| return func(context.Context, string) (pglib.Querier, error) { | ||
| if opts.NewQuerierErr != nil { | ||
| return nil, opts.NewQuerierErr | ||
| } | ||
| return &pglibmocks.Querier{ | ||
| QueryFn: func(_ context.Context, _ uint, query string, _ ...any) (pglib.Rows, error) { | ||
| if opts.Query != nil { | ||
| *opts.Query = query | ||
| } | ||
| if opts.QueryErr != nil { | ||
| return nil, opts.QueryErr | ||
| } | ||
| return &pglibmocks.Rows{ | ||
| FieldDescriptionsFn: func() []pgconn.FieldDescription { | ||
| return []pgconn.FieldDescription{{Name: "lookup", DataTypeOID: oid}} | ||
| }, | ||
| NextFn: func(i uint) bool { return i <= uint(len(values)) }, | ||
| ScanFn: func(i uint, dest ...any) error { | ||
| if opts.ScanErr != nil { | ||
| return opts.ScanErr | ||
| } | ||
| value, ok := dest[0].(*any) | ||
| if !ok { | ||
| return errors.New("unexpected scan destination") | ||
| } | ||
| *value = values[i-1] | ||
| return nil | ||
| }, | ||
| ErrFn: func() error { return opts.RowsErr }, | ||
| CloseFn: func() {}, | ||
| }, nil | ||
| }, | ||
| CloseFn: func(context.Context) error { | ||
| if opts.Closed != nil { | ||
| *opts.Closed = true | ||
| } | ||
| return nil | ||
| }, | ||
| }, nil | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a limit for safety? 100K or something like this.