vitess.io/vitess@v0.16.2/go/vt/schema/ddl_strategy_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 schema
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestIsDirect(t *testing.T) {
    27  	assert.True(t, DDLStrategyDirect.IsDirect())
    28  	assert.False(t, DDLStrategyVitess.IsDirect())
    29  	assert.False(t, DDLStrategyOnline.IsDirect())
    30  	assert.False(t, DDLStrategyGhost.IsDirect())
    31  	assert.False(t, DDLStrategyPTOSC.IsDirect())
    32  	assert.True(t, DDLStrategy("").IsDirect())
    33  	assert.False(t, DDLStrategy("vitess").IsDirect())
    34  	assert.False(t, DDLStrategy("online").IsDirect())
    35  	assert.False(t, DDLStrategy("gh-ost").IsDirect())
    36  	assert.False(t, DDLStrategy("pt-osc").IsDirect())
    37  	assert.False(t, DDLStrategy("mysql").IsDirect())
    38  	assert.True(t, DDLStrategy("something").IsDirect())
    39  }
    40  
    41  func TestParseDDLStrategy(t *testing.T) {
    42  	tt := []struct {
    43  		strategyVariable     string
    44  		strategy             DDLStrategy
    45  		options              string
    46  		isDeclarative        bool
    47  		isSingleton          bool
    48  		isPostponeLaunch     bool
    49  		isPostponeCompletion bool
    50  		isInOrderCompletion  bool
    51  		isAllowConcurrent    bool
    52  		fastOverRevertible   bool
    53  		fastRangeRotation    bool
    54  		allowForeignKeys     bool
    55  		runtimeOptions       string
    56  		err                  error
    57  	}{
    58  		{
    59  			strategyVariable: "direct",
    60  			strategy:         DDLStrategyDirect,
    61  		},
    62  		{
    63  			strategyVariable: "vitess",
    64  			strategy:         DDLStrategyVitess,
    65  		},
    66  		{
    67  			strategyVariable: "online",
    68  			strategy:         DDLStrategyOnline,
    69  		},
    70  		{
    71  			strategyVariable: "gh-ost",
    72  			strategy:         DDLStrategyGhost,
    73  		},
    74  		{
    75  			strategyVariable: "pt-osc",
    76  			strategy:         DDLStrategyPTOSC,
    77  		},
    78  		{
    79  			strategyVariable: "mysql",
    80  			strategy:         DDLStrategyMySQL,
    81  		},
    82  		{
    83  			strategy: DDLStrategyDirect,
    84  		},
    85  		{
    86  			strategyVariable: "gh-ost --max-load=Threads_running=100 --allow-master",
    87  			strategy:         DDLStrategyGhost,
    88  			// These are gh-ost options. Nothing we can do until that changes upstream
    89  			options:        "--max-load=Threads_running=100 --allow-master",
    90  			runtimeOptions: "--max-load=Threads_running=100 --allow-master",
    91  		},
    92  		{
    93  			strategyVariable: "gh-ost --max-load=Threads_running=100 -declarative",
    94  			strategy:         DDLStrategyGhost,
    95  			options:          "--max-load=Threads_running=100 -declarative",
    96  			runtimeOptions:   "--max-load=Threads_running=100",
    97  			isDeclarative:    true,
    98  		},
    99  		{
   100  			strategyVariable: "gh-ost --declarative --max-load=Threads_running=100",
   101  			strategy:         DDLStrategyGhost,
   102  			options:          "--declarative --max-load=Threads_running=100",
   103  			runtimeOptions:   "--max-load=Threads_running=100",
   104  			isDeclarative:    true,
   105  		},
   106  		{
   107  			strategyVariable: "pt-osc -singleton",
   108  			strategy:         DDLStrategyPTOSC,
   109  			options:          "-singleton",
   110  			runtimeOptions:   "",
   111  			isSingleton:      true,
   112  		},
   113  		{
   114  			strategyVariable: "online -postpone-launch",
   115  			strategy:         DDLStrategyOnline,
   116  			options:          "-postpone-launch",
   117  			runtimeOptions:   "",
   118  			isPostponeLaunch: true,
   119  		},
   120  		{
   121  			strategyVariable:     "online -postpone-completion",
   122  			strategy:             DDLStrategyOnline,
   123  			options:              "-postpone-completion",
   124  			runtimeOptions:       "",
   125  			isPostponeCompletion: true,
   126  		},
   127  		{
   128  			strategyVariable:    "online --in-order-completion",
   129  			strategy:            DDLStrategyOnline,
   130  			options:             "--in-order-completion",
   131  			runtimeOptions:      "",
   132  			isInOrderCompletion: true,
   133  		},
   134  		{
   135  			strategyVariable:  "online -allow-concurrent",
   136  			strategy:          DDLStrategyOnline,
   137  			options:           "-allow-concurrent",
   138  			runtimeOptions:    "",
   139  			isAllowConcurrent: true,
   140  		},
   141  		{
   142  			strategyVariable:  "vitess -allow-concurrent",
   143  			strategy:          DDLStrategyVitess,
   144  			options:           "-allow-concurrent",
   145  			runtimeOptions:    "",
   146  			isAllowConcurrent: true,
   147  		},
   148  		{
   149  			strategyVariable:   "vitess --prefer-instant-ddl",
   150  			strategy:           DDLStrategyVitess,
   151  			options:            "--prefer-instant-ddl",
   152  			runtimeOptions:     "",
   153  			fastOverRevertible: true,
   154  		},
   155  		{
   156  			strategyVariable:  "vitess --fast-range-rotation",
   157  			strategy:          DDLStrategyVitess,
   158  			options:           "--fast-range-rotation",
   159  			runtimeOptions:    "",
   160  			fastRangeRotation: true,
   161  		},
   162  		{
   163  			strategyVariable: "vitess --unsafe-allow-foreign-keys",
   164  			strategy:         DDLStrategyVitess,
   165  			options:          "--unsafe-allow-foreign-keys",
   166  			runtimeOptions:   "",
   167  			allowForeignKeys: true,
   168  		},
   169  	}
   170  	for _, ts := range tt {
   171  		t.Run(ts.strategyVariable, func(t *testing.T) {
   172  			setting, err := ParseDDLStrategy(ts.strategyVariable)
   173  			assert.NoError(t, err)
   174  			assert.Equal(t, ts.strategy, setting.Strategy)
   175  			assert.Equal(t, ts.options, setting.Options)
   176  			assert.Equal(t, ts.isDeclarative, setting.IsDeclarative())
   177  			assert.Equal(t, ts.isSingleton, setting.IsSingleton())
   178  			assert.Equal(t, ts.isPostponeCompletion, setting.IsPostponeCompletion())
   179  			assert.Equal(t, ts.isPostponeLaunch, setting.IsPostponeLaunch())
   180  			assert.Equal(t, ts.isAllowConcurrent, setting.IsAllowConcurrent())
   181  			assert.Equal(t, ts.fastOverRevertible, setting.IsPreferInstantDDL())
   182  			assert.Equal(t, ts.fastRangeRotation, setting.IsFastRangeRotationFlag())
   183  			assert.Equal(t, ts.allowForeignKeys, setting.IsAllowForeignKeysFlag())
   184  
   185  			runtimeOptions := strings.Join(setting.RuntimeOptions(), " ")
   186  			assert.Equal(t, ts.runtimeOptions, runtimeOptions)
   187  		})
   188  	}
   189  	{
   190  		_, err := ParseDDLStrategy("other")
   191  		assert.Error(t, err)
   192  	}
   193  }