diff --git a/go/vt/vtgate/engine/send.go b/go/vt/vtgate/engine/send.go index 31c9e9e0eb0..6867ff543af 100644 --- a/go/vt/vtgate/engine/send.go +++ b/go/vt/vtgate/engine/send.go @@ -47,6 +47,8 @@ type Send struct { // IsDML specifies how to deal with autocommit behaviour IsDML bool + IsDDL bool + // SingleShardOnly specifies that the query must be send to only single shard SingleShardOnly bool @@ -94,6 +96,10 @@ func (s *Send) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[str ctx, cancelFunc := addQueryTimeout(ctx, vcursor, s.QueryTimeout) defer cancelFunc() + if err := s.commitIfDDL(ctx, vcursor); err != nil { + return nil, err + } + rss, err := s.checkAndReturnShards(ctx, vcursor) if err != nil { return nil, err @@ -158,6 +164,10 @@ func copyBindVars(in map[string]*querypb.BindVariable) map[string]*querypb.BindV // TryStreamExecute implements Primitive interface func (s *Send) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error { + if err := s.commitIfDDL(ctx, vcursor); err != nil { + return err + } + rss, err := s.checkAndReturnShards(ctx, vcursor) if err != nil { return err @@ -204,3 +214,11 @@ func (s *Send) description() PrimitiveDescription { Other: other, } } + +// commitIfDDL commits any open transaction before executing the ddl query. +func (s *Send) commitIfDDL(ctx context.Context, vcursor VCursor) error { + if s.IsDDL { + return vcursor.Session().Commit(ctx) + } + return nil +}