Skip to content

Commit

Permalink
schemadiff: formalize InstantDDLCapability (#14900)
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach authored Jan 18, 2024
1 parent 65e3b6e commit a207a69
Show file tree
Hide file tree
Showing 15 changed files with 841 additions and 450 deletions.
102 changes: 102 additions & 0 deletions go/mysql/capabilities/capability.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package capabilities

import (
"strconv"
"strings"

vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

var (
ErrUnspecifiedServerVersion = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "server version unspecified")
)

type FlavorCapability int

const (
Expand All @@ -21,3 +49,77 @@ const (
)

type CapableOf func(capability FlavorCapability) (bool, error)

// ServerVersionAtLeast returns true if current server is at least given value.
// Example: if input is []int{8, 0, 23}... the function returns 'true' if we're
// on MySQL 8.0.23, 8.0.24, ...
func ServerVersionAtLeast(serverVersion string, parts ...int) (bool, error) {
if serverVersion == "" {
return false, ErrUnspecifiedServerVersion
}
versionPrefix := strings.Split(serverVersion, "-")[0]
versionTokens := strings.Split(versionPrefix, ".")
for i, part := range parts {
if len(versionTokens) <= i {
return false, nil
}
tokenValue, err := strconv.Atoi(versionTokens[i])
if err != nil {
return false, err
}
if tokenValue > part {
return true, nil
}
if tokenValue < part {
return false, nil
}
}
return true, nil
}

// MySQLVersionHasCapability is specific to MySQL flavors (of all versions) and answers whether
// the given server version has the requested capability.
func MySQLVersionHasCapability(serverVersion string, capability FlavorCapability) (bool, error) {
atLeast := func(parts ...int) (bool, error) {
return ServerVersionAtLeast(serverVersion, parts...)
}
// Capabilities sorted by version.
switch capability {
case MySQLJSONFlavorCapability:
return atLeast(5, 7, 0)
case InstantDDLFlavorCapability,
InstantExpandEnumCapability,
InstantAddLastColumnFlavorCapability,
InstantAddDropVirtualColumnFlavorCapability,
InstantChangeColumnDefaultFlavorCapability:
return atLeast(8, 0, 0)
case PerformanceSchemaDataLocksTableCapability:
return atLeast(8, 0, 1)
case MySQLUpgradeInServerFlavorCapability:
return atLeast(8, 0, 16)
case CheckConstraintsCapability:
return atLeast(8, 0, 16)
case TransactionalGtidExecutedFlavorCapability:
return atLeast(8, 0, 17)
case DisableRedoLogFlavorCapability:
return atLeast(8, 0, 21)
case FastDropTableFlavorCapability:
return atLeast(8, 0, 23)
case InstantAddDropColumnFlavorCapability:
return atLeast(8, 0, 29)
case DynamicRedoLogCapacityFlavorCapability:
return atLeast(8, 0, 30)
default:
return false, nil
}
}

// MySQLVersionCapableOf returns a CapableOf function specific to MySQL flavors
func MySQLVersionCapableOf(serverVersion string) CapableOf {
if serverVersion == "" {
return nil
}
return func(capability FlavorCapability) (bool, error) {
return MySQLVersionHasCapability(serverVersion, capability)
}
}
260 changes: 260 additions & 0 deletions go/mysql/capabilities/capability_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package capabilities

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestServerVersionAtLeast(t *testing.T) {
testcases := []struct {
version string
parts []int
expect bool
expectError bool
}{
{
version: "8.0.14",
parts: []int{8, 0, 14},
expect: true,
},
{
version: "8.0.14-log",
parts: []int{8, 0, 14},
expect: true,
},
{
version: "8.0.14",
parts: []int{8, 0, 13},
expect: true,
},
{
version: "8.0.14-log",
parts: []int{8, 0, 13},
expect: true,
},
{
version: "8.0.14",
parts: []int{8, 0, 15},
expect: false,
},
{
version: "8.0.14-log",
parts: []int{8, 0, 15},
expect: false,
},
{
version: "8.0.14",
parts: []int{7, 5, 20},
expect: true,
},
{
version: "8.0.14",
parts: []int{7, 5},
expect: true,
},
{
version: "8.0.14",
parts: []int{5, 7},
expect: true,
},
{
version: "8.0.14-log",
parts: []int{7, 5, 20},
expect: true,
},
{
version: "8.0.14",
parts: []int{8, 1, 2},
expect: false,
},
{
version: "8.0.14",
parts: []int{10, 1, 2},
expect: false,
},
{
version: "8.0",
parts: []int{8, 0, 14},
expect: false,
},
{
version: "8.0.x",
parts: []int{8, 0, 14},
expectError: true,
},
{
version: "",
parts: []int{8, 0, 14},
expectError: true,
},
}
for _, tc := range testcases {
result, err := ServerVersionAtLeast(tc.version, tc.parts...)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expect, result)
}
}
}

func TestMySQLVersionCapableOf(t *testing.T) {
testcases := []struct {
version string
capability FlavorCapability
isCapable bool
expectNil bool
}{
{
version: "8.0.14",
capability: InstantDDLFlavorCapability,
isCapable: true,
},
{
version: "8.0.20",
capability: TransactionalGtidExecutedFlavorCapability,
isCapable: true,
},
{
version: "8.0.0",
capability: InstantAddLastColumnFlavorCapability,
isCapable: true,
},
{
version: "8.0.0",
capability: InstantAddDropColumnFlavorCapability,
isCapable: false,
},
{
version: "5.6.7",
capability: InstantDDLFlavorCapability,
isCapable: false,
},
{
version: "5.7.29",
capability: TransactionalGtidExecutedFlavorCapability,
isCapable: false,
},
{
version: "5.6.7",
capability: MySQLJSONFlavorCapability,
isCapable: false,
},
{
version: "5.7.29",
capability: MySQLJSONFlavorCapability,
isCapable: true,
},
{
version: "8.0.30",
capability: DynamicRedoLogCapacityFlavorCapability,
isCapable: true,
},
{
version: "8.0.29",
capability: DynamicRedoLogCapacityFlavorCapability,
isCapable: false,
},
{
version: "5.7.38",
capability: DynamicRedoLogCapacityFlavorCapability,
isCapable: false,
},
{
version: "8.0.21",
capability: DisableRedoLogFlavorCapability,
isCapable: true,
},
{
version: "8.0.20",
capability: DisableRedoLogFlavorCapability,
isCapable: false,
},
{
version: "8.0.15",
capability: CheckConstraintsCapability,
isCapable: false,
},
{
version: "8.0.15-log",
capability: CheckConstraintsCapability,
isCapable: false,
},
{
version: "8.0.20",
capability: CheckConstraintsCapability,
isCapable: true,
},
{
version: "8.0.20-log",
capability: CheckConstraintsCapability,
isCapable: true,
},
{
version: "5.7.38",
capability: PerformanceSchemaDataLocksTableCapability,
isCapable: false,
},
{
version: "8.0",
capability: PerformanceSchemaDataLocksTableCapability,
isCapable: false,
},
{
version: "8.0.0",
capability: PerformanceSchemaDataLocksTableCapability,
isCapable: false,
},
{
version: "8.0.20",
capability: PerformanceSchemaDataLocksTableCapability,
isCapable: true,
},
{
// What happens if server version is unspecified
version: "",
capability: CheckConstraintsCapability,
isCapable: false,
expectNil: true,
},
{
// Some ridiculous version. But seeing that we force the question on a MySQLVersionCapableOf
// then this far futuristic version should actually work.
version: "5914.234.17",
capability: CheckConstraintsCapability,
isCapable: true,
},
}
for _, tc := range testcases {
name := fmt.Sprintf("%s %v", tc.version, tc.capability)
t.Run(name, func(t *testing.T) {
capableOf := MySQLVersionCapableOf(tc.version)
if tc.expectNil {
assert.Nil(t, capableOf)
return
}
isCapable, err := capableOf(tc.capability)
assert.NoError(t, err)
assert.Equal(t, tc.isCapable, isCapable)
})
}
}
Loading

0 comments on commit a207a69

Please sign in to comment.