vitess.io/vitess@v0.16.2/go/vt/vtgate/executor_ddl_test.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 vtgate
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
    24  
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestDDLFlags(t *testing.T) {
    29  	executor, _, _, _ := createExecutorEnv()
    30  	session := NewSafeSession(&vtgatepb.Session{TargetString: KsTestUnsharded})
    31  	defer func() {
    32  		enableOnlineDDL = true
    33  		enableDirectDDL = true
    34  	}()
    35  	testcases := []struct {
    36  		enableDirectDDL bool
    37  		enableOnlineDDL bool
    38  		sql             string
    39  		wantErr         bool
    40  		err             string
    41  	}{
    42  		{
    43  			enableDirectDDL: false,
    44  			sql:             "create table t (id int)",
    45  			wantErr:         true,
    46  			err:             "direct DDL is disabled",
    47  		}, {
    48  			enableDirectDDL: true,
    49  			sql:             "create table t (id int)",
    50  			wantErr:         false,
    51  		}, {
    52  			enableOnlineDDL: false,
    53  			sql:             "revert vitess_migration 'abc'",
    54  			wantErr:         true,
    55  			err:             "online DDL is disabled",
    56  		},
    57  	}
    58  	for _, testcase := range testcases {
    59  		t.Run(fmt.Sprintf("%s-%v-%v", testcase.sql, testcase.enableDirectDDL, testcase.enableOnlineDDL), func(t *testing.T) {
    60  			enableDirectDDL = testcase.enableDirectDDL
    61  			enableOnlineDDL = testcase.enableOnlineDDL
    62  			_, err := executor.Execute(ctx, "TestDDLFlags", session, testcase.sql, nil)
    63  			if testcase.wantErr {
    64  				require.EqualError(t, err, testcase.err)
    65  			} else {
    66  				require.NoError(t, err)
    67  			}
    68  		})
    69  	}
    70  }