Skip to content

Commit

Permalink
Encode blobs as blobs and not as strings
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Jan 29, 2025
1 parent 359128d commit 553446e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion go/vt/vttablet/tabletserver/schema/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sync"
"time"

"vitess.io/vitess/go/bytes2"
"vitess.io/vitess/go/constants/sidecar"
"vitess.io/vitess/go/mysql/replication"
"vitess.io/vitess/go/sqltypes"
Expand Down Expand Up @@ -230,10 +231,15 @@ func (tr *Tracker) saveCurrentSchemaToDb(ctx context.Context, gtid, ddl string,
}
defer conn.Recycle()

// We serialize a blob here, encodeString is for strings only
// and should not be used for binary data.
blobVal := sqltypes.MakeTrusted(sqltypes.VarBinary, blob)
buf := bytes2.Buffer{}
blobVal.EncodeSQLBytes2(&buf)
query := sqlparser.BuildParsedQuery("insert into %s.schema_version "+
"(pos, ddl, schemax, time_updated) "+
"values (%s, %s, %s, %d)", sidecar.GetIdentifier(), encodeString(gtid),
encodeString(ddl), encodeString(string(blob)), timestamp).Query
encodeString(ddl), buf.String(), timestamp).Query
_, err = conn.Conn.Exec(ctx, query, 1, false)
if err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"

"vitess.io/vitess/go/bytes2"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
Expand Down Expand Up @@ -371,9 +372,15 @@ func TestVersion(t *testing.T) {
}
blob, _ := dbSchema.MarshalVT()
gtid := "MariaDB/0-41983-20"
// We serialize a blob here, encodeString is for strings only
// and should not be used for binary data.
blobVal := sqltypes.MakeTrusted(sqltypes.VarBinary, blob)
buf := bytes2.Buffer{}
blobVal.EncodeSQLBytes2(&buf)

testcases := []testcase{{
input: []string{
fmt.Sprintf("insert into _vt.schema_version values(1, '%s', 123, 'create table t1', %v)", gtid, encodeString(string(blob))),
fmt.Sprintf("insert into _vt.schema_version values(1, '%s', 123, 'create table t1', %v)", gtid, buf.String()),
},
// External table events don't get sent.
output: [][]string{{
Expand Down

0 comments on commit 553446e

Please sign in to comment.