Repository navigation
Fixes for postgres and sink for sqlbatch #387
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 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
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,121 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "net/netip" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/jackc/pgx/v5/pgtype" | ||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| ) | ||
|
|
||
| func TestBSONToMapConvertsBinaryValues(t *testing.T) { | ||
| uuidBytes := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} | ||
| doc, err := bson.Marshal(bson.D{ | ||
| {Key: "bytes", Value: bson.Binary{Subtype: bson.TypeBinaryGeneric, Data: []byte("data")}}, | ||
| {Key: "uuid", Value: bson.Binary{Subtype: bson.TypeBinaryUUID, Data: uuidBytes}}, | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| got, err := bsonToMap(NewBSONRegistry(), doc, false) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(got["bytes"], []byte("data")) { | ||
| t.Fatalf("bytes = %#v", got["bytes"]) | ||
| } | ||
| wantUUID := pgtype.UUID{Valid: true} | ||
| copy(wantUUID.Bytes[:], uuidBytes) | ||
| if !reflect.DeepEqual(got["uuid"], wantUUID) { | ||
| t.Fatalf("uuid = %#v, want %#v", got["uuid"], wantUUID) | ||
| } | ||
| } | ||
|
|
||
| func TestBSONToMapRejectsLegacyUUID(t *testing.T) { | ||
| doc, err := bson.Marshal(bson.D{{ | ||
| Key: "uuid", | ||
| Value: bson.Binary{ | ||
| Subtype: bson.TypeBinaryUUIDOld, | ||
| Data: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, | ||
| }, | ||
| }}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := bsonToMap(NewBSONRegistry(), doc, false); err == nil { | ||
| t.Fatal("expected legacy UUID subtype to be rejected") | ||
| } | ||
| } | ||
|
|
||
| func TestBSONToMapPreservesIDForDestinationColumn(t *testing.T) { | ||
| doc, err := bson.Marshal(bson.D{{Key: "_id", Value: "source-id"}, {Key: "value", Value: "data"}}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| got, err := bsonToMap(NewBSONRegistry(), doc, true) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got["_id"] != "source-id" { | ||
| t.Fatalf("_id = %#v", got["_id"]) | ||
| } | ||
| } | ||
|
|
||
| func TestMarshalWithRegistryNormalizesPostgresNetworkTypes(t *testing.T) { | ||
| prefix := netip.MustParsePrefix("192.0.2.0/24") | ||
| doc, err := marshalWithRegistry(NewBSONRegistry(), map[string]interface{}{"network": prefix}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var got map[string]interface{} | ||
| if err := bson.Unmarshal(doc, &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got["network"] != prefix.String() { | ||
| t.Fatalf("network = %#v, want %q", got["network"], prefix.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestMarshalValueWithRegistryNormalizesPostgresNetworkTypes(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| value any | ||
| want string | ||
| }{ | ||
| {name: "address", value: netip.MustParseAddr("192.0.2.1"), want: "192.0.2.1"}, | ||
| {name: "prefix", value: netip.MustParsePrefix("192.0.2.0/24"), want: "192.0.2.0/24"}, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| typ, data, err := marshalValueWithRegistry(NewBSONRegistry(), test.value) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var got string | ||
| if err := bson.UnmarshalValue(typ, data, &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != test.want { | ||
| t.Fatalf("network ID = %q, want %q", got, test.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestBSONRegistryEncodesPostgresUUIDAsUUID(t *testing.T) { | ||
| uuid := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} | ||
| typ, data, err := marshalValueWithRegistry(NewBSONRegistry(), uuid) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var got bson.Binary | ||
| if err := bson.UnmarshalValue(typ, data, &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got.Subtype != bson.TypeBinaryUUID || !reflect.DeepEqual(got.Data, uuid[:]) { | ||
| t.Fatalf("binary = %#v", got) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 3736
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 23198
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 20862
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 21227
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 20819
🏁 Script executed:
Repository: adiom-data/dsync
Length of output: 17579
Select the named
_idcomponent before falling back to the last component.GetByIdsaccepts any repeatedBsonValueorder, butmongoIDPartalways selects the last element. A valid multi-part request with_idbefore another component can therefore query MongoDB with the wrong value.buildIdFilteralready identifies_idby name.🐛 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents