Skip to content

Commit

Permalink
Merge pull request #1513 migration version for UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Oct 23, 2024
2 parents 534c571 + 51e3a6c commit 8e47d2c
Show file tree
Hide file tree
Showing 31 changed files with 1,681 additions and 55 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Add workaround for bug in uuid send/receive from server. It is migration version. All native code and most database sql code worked with uuid continue to work.

## v3.85.3
* Renamed `query.WithPoolID()` into `query.WithResourcePool()`

Expand Down
17 changes: 16 additions & 1 deletion internal/bind/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sort"
"time"

"github.com/google/uuid"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
Expand Down Expand Up @@ -113,9 +115,22 @@ func toValue(v interface{}) (_ types.Value, err error) {

return types.ListValue(items...), nil
case [16]byte:
return types.UUIDValue(x), nil
return types.UUIDValue(x), nil //nolint:staticcheck
case *[16]byte:
return types.NullableUUIDValue(x), nil
case types.UUIDBytesWithIssue1501Type:
return types.UUIDWithIssue1501Value(x.AsBytesArray()), nil
case *types.UUIDBytesWithIssue1501Type:
if x == nil {
return types.NullableUUIDValueWithIssue1501(nil), nil
}
val := x.AsBytesArray()

return types.NullableUUIDValueWithIssue1501(&val), nil
case uuid.UUID:
return types.UuidValue(x), nil
case *uuid.UUID:
return types.NullableUUIDTypedValue(x), nil
case time.Time:
return types.TimestampValueFromTime(x), nil
case *time.Time:
Expand Down
28 changes: 23 additions & 5 deletions internal/bind/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
)
Expand Down Expand Up @@ -280,20 +282,36 @@ func TestToValue(t *testing.T) {

{
src: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
dst: types.UUIDValue([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
dst: types.UUIDValue([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), //nolint:staticcheck
err: nil,
},
{
src: func() *[16]byte { return nil }(),
dst: types.NullValue(types.TypeUUID),
err: nil,
},
{
src: func(v [16]byte) *[16]byte { return &v }([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
dst: types.OptionalValue(types.UUIDValue([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})),
dst: types.OptionalValue(types.UUIDValue([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})), //nolint:staticcheck,lll
err: nil,
},
{
src: func() *[16]byte { return nil }(),
dst: types.NullValue(types.TypeUUID),
src: uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
dst: value.TextValue("01020304-0506-0708-090a-0b0c0d0e0f10"),
err: nil,
},

{
src: func(v uuid.UUID) *uuid.UUID { return &v }(uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
// uuid implemented driver.Valuer and doesn't set optional wrapper
dst: types.TextValue("01020304-0506-0708-090a-0b0c0d0e0f10"),
err: nil,
},
// https://github.com/ydb-platform/ydb-go-sdk/issues/1515
//{
// src: func() *uuid.UUID { return nil }(),
// dst: nil,
// err: nil,
//},
{
src: time.Unix(42, 43),
dst: types.TimestampValueFromTime(time.Unix(42, 43)),
Expand Down
48 changes: 46 additions & 2 deletions internal/params/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package params
import (
"time"

"github.com/google/uuid"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
)

Expand Down Expand Up @@ -201,8 +203,28 @@ func (d *dictPair) YSON(v []byte) *dictValue {
}
}

// UUID has data corruption bug and will be removed in next version.
//
// Deprecated: Use Uuid (prefer) or UUIDWithIssue1501Value (for save old behavior) instead.
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
func (d *dictPair) UUID(v [16]byte) *dictValue {
d.keyValue = value.UUIDValue(v)
d.keyValue = value.UUIDWithIssue1501Value(v)

return &dictValue{
pair: d,
}
}

func (d *dictPair) Uuid(v uuid.UUID) *dictValue { //nolint:revive,stylecheck
d.keyValue = value.Uuid(v)

return &dictValue{
pair: d,
}
}

func (d *dictPair) UUIDWithIssue1501Value(v [16]byte) *dictValue {
d.keyValue = value.UUIDWithIssue1501Value(v)

return &dictValue{
pair: d,
Expand Down Expand Up @@ -422,10 +444,32 @@ func (d *dictValue) YSON(v []byte) *dict {
return d.pair.parent
}

// UUID has data corruption bug and will be removed in next version.
//
// Deprecated: Use Uuid (prefer) or UUIDWithIssue1501Value (for save old behavior) instead.
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
func (d *dictValue) UUID(v [16]byte) *dict {
d.pair.parent.values = append(d.pair.parent.values, value.DictValueField{
K: d.pair.keyValue,
V: value.UUIDValue(v),
V: value.UUIDWithIssue1501Value(v),
})

return d.pair.parent
}

func (d *dictValue) Uuid(v uuid.UUID) *dict { //nolint:revive,stylecheck
d.pair.parent.values = append(d.pair.parent.values, value.DictValueField{
K: d.pair.keyValue,
V: value.Uuid(v),
})

return d.pair.parent
}

func (d *dictValue) UUIDWithIssue1501Value(v [16]byte) *dict {
d.pair.parent.values = append(d.pair.parent.values, value.DictValueField{
K: d.pair.keyValue,
V: value.UUIDWithIssue1501Value(v),
})

return d.pair.parent
Expand Down
33 changes: 33 additions & 0 deletions internal/params/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

Expand Down Expand Up @@ -362,6 +363,38 @@ func TestDict(t *testing.T) {
},
},
},
{
method: "Uuid",
args: []any{uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},

expected: expected{
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
},
Value: &Ydb.Value{
Value: &Ydb.Value_Low_128{
Low_128: 506660481424032516,
},
High_128: 1157159078456920585,
},
},
},
{
method: "UUIDWithIssue1501Value",
args: []any{[...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},

expected: expected{
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
},
Value: &Ydb.Value{
Value: &Ydb.Value_Low_128{
Low_128: 651345242494996240,
},
High_128: 72623859790382856,
},
},
},
{
method: "TzDatetime",
args: []any{time.Unix(123456789, 456).UTC()},
Expand Down
20 changes: 19 additions & 1 deletion internal/params/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package params
import (
"time"

"github.com/google/uuid"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
)

Expand Down Expand Up @@ -165,8 +167,24 @@ func (l *listItem) YSON(v []byte) *list {
return l.parent
}

// UUID has data corruption bug and will be removed in next version.
//
// Deprecated: Use Uuid (prefer) or UUIDWithIssue1501Value (for save old behavior) instead.
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
func (l *listItem) UUID(v [16]byte) *list {
l.parent.values = append(l.parent.values, value.UUIDValue(v))
l.parent.values = append(l.parent.values, value.UUIDWithIssue1501Value(v))

return l.parent
}

func (l *listItem) Uuid(v uuid.UUID) *list { //nolint:revive,stylecheck
l.parent.values = append(l.parent.values, value.Uuid(v))

return l.parent
}

func (l *listItem) UUIDWithIssue1501Value(v [16]byte) *list {
l.parent.values = append(l.parent.values, value.UUIDWithIssue1501Value(v))

return l.parent
}
Expand Down
33 changes: 33 additions & 0 deletions internal/params/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

Expand Down Expand Up @@ -361,6 +362,38 @@ func TestList(t *testing.T) {
},
},
},
{
method: "Uuid",
args: []any{uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},

expected: expected{
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
},
Value: &Ydb.Value{
Value: &Ydb.Value_Low_128{
Low_128: 506660481424032516,
},
High_128: 1157159078456920585,
},
},
},
{
method: "UUIDWithIssue1501Value",
args: []any{[...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},

expected: expected{
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
},
Value: &Ydb.Value{
Value: &Ydb.Value_Low_128{
Low_128: 651345242494996240,
},
High_128: 72623859790382856,
},
},
},
{
method: "TzDatetime",
args: []any{time.Unix(123456789, 456).UTC()},
Expand Down
18 changes: 18 additions & 0 deletions internal/params/optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package params
import (
"time"

"github.com/google/uuid"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
)

Expand Down Expand Up @@ -153,12 +155,28 @@ func (p *optional) YSON(v *[]byte) *optionalBuilder {
return &optionalBuilder{opt: p}
}

// UUID has data corruption bug and will be removed in next version.
//
// Deprecated: Use Uuid (prefer) or UUIDWithIssue1501Value (for save old behavior) instead.
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
func (p *optional) UUID(v *[16]byte) *optionalBuilder {
p.value = value.NullableUUIDValue(v)

return &optionalBuilder{opt: p}
}

func (p *optional) Uuid(v *uuid.UUID) *optionalBuilder { //nolint:revive,stylecheck
p.value = value.NullableUuidValue(v)

return &optionalBuilder{opt: p}
}

func (p *optional) UUIDWithIssue1501Value(v *[16]byte) *optionalBuilder {
p.value = value.NullableUUIDValue(v)

return &optionalBuilder{opt: p}
}

func (p *optional) TzDate(v *time.Time) *optionalBuilder {
p.value = value.NullableTzDateValueFromTime(v)

Expand Down
53 changes: 53 additions & 0 deletions internal/params/optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

Expand Down Expand Up @@ -580,6 +581,58 @@ func TestOptional(t *testing.T) {
},
},
},
{
method: "Uuid",
args: []any{p(uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})},

expected: expected{
Type: &Ydb.Type{
Type: &Ydb.Type_OptionalType{
OptionalType: &Ydb.OptionalType{
Item: &Ydb.Type{
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
},
},
},
},
Value: &Ydb.Value{
Value: &Ydb.Value_NestedValue{
NestedValue: &Ydb.Value{
Value: &Ydb.Value_Low_128{
Low_128: 506660481424032516,
},
High_128: 1157159078456920585,
},
},
},
},
},
{
method: "UUIDWithIssue1501Value",
args: []any{p([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})},

expected: expected{
Type: &Ydb.Type{
Type: &Ydb.Type_OptionalType{
OptionalType: &Ydb.OptionalType{
Item: &Ydb.Type{
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
},
},
},
},
Value: &Ydb.Value{
Value: &Ydb.Value_NestedValue{
NestedValue: &Ydb.Value{
Value: &Ydb.Value_Low_128{
Low_128: 651345242494996240,
},
High_128: 72623859790382856,
},
},
},
},
},
{
method: "TzDatetime",
args: []any{p(time.Unix(123456789, 456).UTC())},
Expand Down
Loading

0 comments on commit 8e47d2c

Please sign in to comment.