Skip to content

Commit 748c7df

Browse files
authored
fix(mssql)!: Fix JSON transformer & column types (#7010)
Fixes #7001 BEGIN_COMMIT_OVERRIDE fix(mssql): Change column types from `varchar` to `nvarchar` to properly store Unicode characters BREAKING-CHANGE: Change column types from `varchar` to `nvarchar` to properly store Unicode characters fix(mssql): Fix JSON transformer to store unescaped data END_COMMIT_OVERRIDE
1 parent fe8ff12 commit 748c7df

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

plugins/destination/mssql/client/queries/create_table_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestCreateTable(t *testing.T) {
1313
expected = `CREATE TABLE [cq].[table_name] (
1414
[_cq_id] uniqueidentifier UNIQUE NOT NULL,
1515
[_cq_parent_id] uniqueidentifier,
16-
[_cq_source_name] varchar(8000),
16+
[_cq_source_name] nvarchar(4000),
1717
[_cq_sync_time] datetimeoffset,
1818
[extra_col] float NOT NULL
1919
CONSTRAINT [table_name_cqpk] PRIMARY KEY (

plugins/destination/mssql/client/queries/tvp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestTVPAddType(t *testing.T) {
6868
expected = `CREATE TYPE [cq].[cq_tbl_table_name] AS TABLE (
6969
[_cq_id] uniqueidentifier UNIQUE NOT NULL,
7070
[_cq_parent_id] uniqueidentifier,
71-
[_cq_source_name] varchar(8000),
71+
[_cq_source_name] nvarchar(4000),
7272
[_cq_sync_time] datetimeoffset,
7373
[extra_col_pk1] float NOT NULL,
7474
[extra_col_pk2] bit NOT NULL,

plugins/destination/mssql/client/queries/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func SQLType(t schema.ValueType) string {
2525
schema.TypeCIDR,
2626
schema.TypeMacAddr,
2727
schema.TypeInet:
28-
return "varchar(8000)" // feasible to see these as PK, so need to limit the value
28+
return "nvarchar(4000)" // feasible to see these as PK, so need to limit the value
2929
case schema.TypeStringArray,
3030
schema.TypeJSON,
3131
schema.TypeUUIDArray,
3232
schema.TypeCIDRArray,
3333
schema.TypeMacAddrArray,
3434
schema.TypeInetArray,
3535
schema.TypeIntArray:
36-
return "varchar(max)"
36+
return "nvarchar(max)"
3737
default:
3838
panic("unknown type " + t.String())
3939
}

plugins/destination/mssql/client/transform.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ func (*Client) TransformJSON(v *schema.JSON) any {
4747
if v.Status != schema.Present {
4848
return nil
4949
}
50-
return string(v.Bytes)
50+
51+
return unescape(string(v.Bytes))
5152
}
5253

5354
func (*Client) TransformUUID(v *schema.UUID) any {
@@ -109,7 +110,7 @@ func (*Client) TransformText(v *schema.Text) any {
109110
if v.Status != schema.Present {
110111
return nil
111112
}
112-
return v.String()
113+
return v.Str
113114
}
114115

115116
func (*Client) TransformCIDRArray(v *schema.CIDRArray) any {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package client
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestUnescape(t *testing.T) {
10+
type testCase struct {
11+
have, want string
12+
}
13+
for _, tc := range []testCase{
14+
{
15+
have: `{"BillTo":"P\u0026T"}`,
16+
want: `{"BillTo":"P&T"}`,
17+
},
18+
{
19+
have: `{"BillTo":"P&T"}`,
20+
want: `{"BillTo":"P&T"}`,
21+
},
22+
} {
23+
require.Equal(t, tc.want, unescape(tc.have))
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package client
2+
3+
import (
4+
"strconv"
5+
)
6+
7+
func unescape(str string) string {
8+
out := ""
9+
for len(str) > 0 {
10+
if str[0] == '\\' {
11+
if len(str) > 5 && str[1] == 'u' {
12+
u, err := strconv.ParseUint(str[2:6], 16, 64)
13+
if err == nil {
14+
out += string(byte(u))
15+
str = str[6:]
16+
continue
17+
}
18+
}
19+
}
20+
out += str[:1]
21+
str = str[1:]
22+
}
23+
24+
return out
25+
}

0 commit comments

Comments
 (0)