vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/revert_migration.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package engine
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"vitess.io/vitess/go/sqltypes"
    24  	"vitess.io/vitess/go/vt/key"
    25  	querypb "vitess.io/vitess/go/vt/proto/query"
    26  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    27  	"vitess.io/vitess/go/vt/schema"
    28  	"vitess.io/vitess/go/vt/sqlparser"
    29  	"vitess.io/vitess/go/vt/vterrors"
    30  	"vitess.io/vitess/go/vt/vtgate/vindexes"
    31  )
    32  
    33  var _ Primitive = (*RevertMigration)(nil)
    34  
    35  // RevertMigration represents the instructions to perform an online schema change via vtctld
    36  type RevertMigration struct {
    37  	Keyspace          *vindexes.Keyspace
    38  	Stmt              *sqlparser.RevertMigration
    39  	Query             string
    40  	TargetDestination key.Destination
    41  
    42  	noTxNeeded
    43  
    44  	noInputs
    45  }
    46  
    47  func (v *RevertMigration) description() PrimitiveDescription {
    48  	return PrimitiveDescription{
    49  		OperatorType: "RevertMigration",
    50  		Keyspace:     v.Keyspace,
    51  		Other: map[string]any{
    52  			"query": v.Query,
    53  		},
    54  	}
    55  }
    56  
    57  // RouteType implements the Primitive interface
    58  func (v *RevertMigration) RouteType() string {
    59  	return "RevertMigration"
    60  }
    61  
    62  // GetKeyspaceName implements the Primitive interface
    63  func (v *RevertMigration) GetKeyspaceName() string {
    64  	return v.Keyspace.Name
    65  }
    66  
    67  // GetTableName implements the Primitive interface
    68  func (v *RevertMigration) GetTableName() string {
    69  	return ""
    70  }
    71  
    72  // TryExecute implements the Primitive interface
    73  func (v *RevertMigration) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (result *sqltypes.Result, err error) {
    74  	result = &sqltypes.Result{
    75  		Fields: []*querypb.Field{
    76  			{
    77  				Name: "uuid",
    78  				Type: sqltypes.VarChar,
    79  			},
    80  		},
    81  		Rows: [][]sqltypes.Value{},
    82  	}
    83  
    84  	sql := fmt.Sprintf("revert %s", v.Stmt.UUID)
    85  
    86  	ddlStrategySetting, err := schema.ParseDDLStrategy(vcursor.Session().GetDDLStrategy())
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	ddlStrategySetting.Strategy = schema.DDLStrategyOnline // and we keep the options as they were
    91  	onlineDDL, err := schema.NewOnlineDDL(v.GetKeyspaceName(), "", sql, ddlStrategySetting, fmt.Sprintf("vtgate:%s", vcursor.Session().GetSessionUUID()), "")
    92  	if err != nil {
    93  		return result, err
    94  	}
    95  
    96  	s := Send{
    97  		Keyspace:          v.Keyspace,
    98  		TargetDestination: v.TargetDestination,
    99  		Query:             onlineDDL.SQL,
   100  		IsDML:             false,
   101  		SingleShardOnly:   false,
   102  	}
   103  	if _, err := vcursor.ExecutePrimitive(ctx, &s, bindVars, wantfields); err != nil {
   104  		return result, err
   105  	}
   106  	result.Rows = append(result.Rows, []sqltypes.Value{
   107  		sqltypes.NewVarChar(onlineDDL.UUID),
   108  	})
   109  	return result, err
   110  }
   111  
   112  // TryStreamExecute implements the Primitive interface
   113  func (v *RevertMigration) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
   114  	results, err := v.TryExecute(ctx, vcursor, bindVars, wantfields)
   115  	if err != nil {
   116  		return err
   117  	}
   118  	return callback(results)
   119  }
   120  
   121  // GetFields implements the Primitive interface
   122  func (v *RevertMigration) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
   123  	return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] GetFields is not reachable")
   124  }