vitess.io/vitess@v0.16.2/go/vt/wrangler/schema_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 wrangler 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 25 "vitess.io/vitess/go/sqltypes" 26 tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" 27 ) 28 29 func TestValidateSchemaShard(t *testing.T) { 30 ctx := context.Background() 31 sourceShards := []string{"-80", "80-"} 32 targetShards := []string{"-40", "40-80", "80-c0", "c0-"} 33 34 tme := newTestShardMigrater(ctx, t, sourceShards, targetShards) 35 36 schm := &tabletmanagerdatapb.SchemaDefinition{ 37 TableDefinitions: []*tabletmanagerdatapb.TableDefinition{{ 38 Name: "not_in_vschema", 39 Columns: []string{"c1", "c2"}, 40 PrimaryKeyColumns: []string{"c1"}, 41 Fields: sqltypes.MakeTestFields("c1|c2", "int64|int64"), 42 }}, 43 } 44 45 // This is the vschema returned by newTestShardMigrater 46 schm2 := &tabletmanagerdatapb.SchemaDefinition{ 47 TableDefinitions: []*tabletmanagerdatapb.TableDefinition{ 48 { 49 Name: "t1", 50 Columns: []string{"c1"}, 51 }, 52 { 53 Name: "t2", 54 Columns: []string{"c1"}, 55 }, 56 { 57 Name: "t3", 58 Columns: []string{"c1"}, 59 }, 60 }, 61 } 62 63 for _, primary := range tme.sourcePrimaries { 64 if primary.Tablet.Shard == "80-" { 65 primary.FakeMysqlDaemon.Schema = schm 66 } else { 67 primary.FakeMysqlDaemon.Schema = schm2 68 } 69 } 70 71 // Schema Checks 72 err := tme.wr.ValidateSchemaShard(ctx, "ks", "-80", nil /*excludeTables*/, true /*includeViews*/, true /*includeVSchema*/) 73 require.NoError(t, err) 74 shouldErr := tme.wr.ValidateSchemaShard(ctx, "ks", "80-", nil /*excludeTables*/, true /*includeViews*/, true /*includeVSchema*/) 75 require.Contains(t, shouldErr.Error(), "ks/80- has tables that are not in the vschema:") 76 77 // VSchema Specific Checks 78 err = tme.wr.ValidateVSchema(ctx, "ks", []string{"-80"}, nil /*excludeTables*/, true /*includeViews*/) 79 require.NoError(t, err) 80 shouldErr = tme.wr.ValidateVSchema(ctx, "ks", []string{"80-"}, nil /*excludeTables*/, true /*includeVoews*/) 81 require.Contains(t, shouldErr.Error(), "ks/80- has tables that are not in the vschema:") 82 } 83 84 func TestValidateSchemaKeyspace(t *testing.T) { 85 ctx := context.Background() 86 sourceShards := []string{"-80", "80-"} 87 targetShards := []string{"-40", "40-80", "80-c0", "c0-"} 88 89 tmePass := newTestShardMigrater(ctx, t, sourceShards, targetShards) 90 tmeDiffs := newTestShardMigrater(ctx, t, sourceShards, targetShards) 91 92 schm := &tabletmanagerdatapb.SchemaDefinition{ 93 TableDefinitions: []*tabletmanagerdatapb.TableDefinition{{ 94 Name: "not_in_vschema", 95 Columns: []string{"c1", "c2"}, 96 PrimaryKeyColumns: []string{"c1"}, 97 Fields: sqltypes.MakeTestFields("c1|c2", "int64|int64"), 98 }}, 99 } 100 101 // This is the vschema returned by newTestShardMigrater 102 sameAsVSchema := &tabletmanagerdatapb.SchemaDefinition{ 103 TableDefinitions: []*tabletmanagerdatapb.TableDefinition{ 104 { 105 Name: "t1", 106 Columns: []string{"c1"}, 107 }, 108 { 109 Name: "t2", 110 Columns: []string{"c1"}, 111 }, 112 { 113 Name: "t3", 114 Columns: []string{"c1"}, 115 }, 116 }, 117 } 118 119 for _, primary := range append(tmePass.sourcePrimaries, tmePass.targetPrimaries...) { 120 primary.FakeMysqlDaemon.Schema = sameAsVSchema 121 } 122 123 for _, primary := range append(tmeDiffs.sourcePrimaries, tmeDiffs.targetPrimaries...) { 124 primary.FakeMysqlDaemon.Schema = schm 125 } 126 127 // Schema Checks 128 err := tmePass.wr.ValidateSchemaKeyspace(ctx, "ks", nil /*excludeTables*/, true /*includeViews*/, true /*skipNoPrimary*/, true /*includeVSchema*/) 129 require.NoError(t, err) 130 err = tmePass.wr.ValidateSchemaKeyspace(ctx, "ks", nil /*excludeTables*/, true /*includeViews*/, true /*skipNoPrimary*/, false /*includeVSchema*/) 131 require.NoError(t, err) 132 shouldErr := tmeDiffs.wr.ValidateSchemaKeyspace(ctx, "ks", nil /*excludeTables*/, true /*includeViews*/, true /*skipNoPrimary*/, true /*includeVSchema*/) 133 require.Error(t, shouldErr) 134 }