vitess.io/vitess@v0.16.2/go/vt/schema/tablegc_test.go (about)

     1  /*
     2  Copyright 2020 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  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestIsGCTableName(t *testing.T) {
    28  	tm := time.Now()
    29  	states := []TableGCState{HoldTableGCState, PurgeTableGCState, EvacTableGCState, DropTableGCState}
    30  	for _, state := range states {
    31  		for i := 0; i < 10; i++ {
    32  			tableName, err := generateGCTableName(state, "", tm)
    33  			assert.NoError(t, err)
    34  			assert.True(t, IsGCTableName(tableName))
    35  		}
    36  	}
    37  	names := []string{
    38  		"_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_202009151204100",
    39  		"_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 ",
    40  		"__vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    41  		"_vt_DROP_6ace8bcef73211ea87e9f875a4d2_20200915120410",
    42  		"_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915",
    43  		"_vt_OTHER_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    44  		"_vt_OTHER_6ace8bcef73211ea87e9f875a4d24e90_zz20200915120410",
    45  	}
    46  	for _, tableName := range names {
    47  		assert.False(t, IsGCTableName(tableName))
    48  	}
    49  }
    50  
    51  func TestAnalyzeGCTableName(t *testing.T) {
    52  	baseTime, err := time.Parse(time.RFC1123, "Tue, 15 Sep 2020 12:04:10 UTC")
    53  	assert.NoError(t, err)
    54  	tt := []struct {
    55  		tableName string
    56  		state     TableGCState
    57  		t         time.Time
    58  	}{
    59  		{
    60  			tableName: "_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    61  			state:     DropTableGCState,
    62  			t:         baseTime,
    63  		},
    64  		{
    65  			tableName: "_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    66  			state:     HoldTableGCState,
    67  			t:         baseTime,
    68  		},
    69  		{
    70  			tableName: "_vt_EVAC_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    71  			state:     EvacTableGCState,
    72  			t:         baseTime,
    73  		},
    74  		{
    75  			tableName: "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    76  			state:     PurgeTableGCState,
    77  			t:         baseTime,
    78  		},
    79  	}
    80  	for _, ts := range tt {
    81  		isGC, state, uuid, tm, err := AnalyzeGCTableName(ts.tableName)
    82  		assert.NoError(t, err)
    83  		assert.True(t, isGC)
    84  		assert.True(t, IsGCUUID(uuid))
    85  		assert.Equal(t, ts.state, state)
    86  		assert.Equal(t, ts.t, tm)
    87  	}
    88  }
    89  
    90  func TestParseGCLifecycle(t *testing.T) {
    91  	tt := []struct {
    92  		lifecycle string
    93  		states    map[TableGCState]bool
    94  		expectErr bool
    95  	}{
    96  		{
    97  			lifecycle: "",
    98  			states: map[TableGCState]bool{
    99  				DropTableGCState: true,
   100  			},
   101  		},
   102  		{
   103  			lifecycle: "drop",
   104  			states: map[TableGCState]bool{
   105  				DropTableGCState: true,
   106  			},
   107  		},
   108  		{
   109  			lifecycle: "   drop, ",
   110  			states: map[TableGCState]bool{
   111  				DropTableGCState: true,
   112  			},
   113  		},
   114  		{
   115  			lifecycle: "hold, drop",
   116  			states: map[TableGCState]bool{
   117  				HoldTableGCState: true,
   118  				DropTableGCState: true,
   119  			},
   120  		},
   121  		{
   122  			lifecycle: "hold,purge, evac;drop",
   123  			states: map[TableGCState]bool{
   124  				HoldTableGCState:  true,
   125  				PurgeTableGCState: true,
   126  				EvacTableGCState:  true,
   127  				DropTableGCState:  true,
   128  			},
   129  		},
   130  		{
   131  			lifecycle: "hold,purge,evac,drop",
   132  			states: map[TableGCState]bool{
   133  				HoldTableGCState:  true,
   134  				PurgeTableGCState: true,
   135  				EvacTableGCState:  true,
   136  				DropTableGCState:  true,
   137  			},
   138  		},
   139  		{
   140  			lifecycle: "hold, other, drop",
   141  			expectErr: true,
   142  		},
   143  	}
   144  	for _, ts := range tt {
   145  		t.Run(ts.lifecycle, func(*testing.T) {
   146  			states, err := ParseGCLifecycle(ts.lifecycle)
   147  			if ts.expectErr {
   148  				assert.Error(t, err)
   149  			} else {
   150  				assert.NoError(t, err)
   151  				assert.Equal(t, ts.states, states)
   152  			}
   153  		})
   154  	}
   155  }
   156  
   157  func TestGenerateRenameStatementWithUUID(t *testing.T) {
   158  	uuid := "997342e3_e91d_11eb_aaae_0a43f95f28a3"
   159  	tableName := "mytbl"
   160  	countIterations := 5
   161  	toTableNames := map[string]bool{}
   162  	for i := 0; i < countIterations; i++ {
   163  		_, toTableName, err := GenerateRenameStatementWithUUID(tableName, PurgeTableGCState, OnlineDDLToGCUUID(uuid), time.Now().Add(time.Duration(i)*time.Second).UTC())
   164  		require.NoError(t, err)
   165  		toTableNames[toTableName] = true
   166  	}
   167  	assert.Equal(t, countIterations, len(toTableNames))
   168  }