Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support drop vindex #6151

Merged
merged 1 commit into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func IsDMLStatement(stmt Statement) bool {
//IsVschemaDDL returns true if the query is an Vschema alter ddl.
func IsVschemaDDL(ddl *DDL) bool {
switch ddl.Action {
case CreateVindexStr, AddVschemaTableStr, DropVschemaTableStr, AddColVindexStr, DropColVindexStr, AddSequenceStr, AddAutoIncStr:
case CreateVindexStr, DropVindexStr, AddVschemaTableStr, DropVschemaTableStr, AddColVindexStr, DropColVindexStr, AddSequenceStr, AddAutoIncStr:
return true
}
return false
Expand Down
19 changes: 19 additions & 0 deletions go/vt/topotools/vschema_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)

return ks, nil

case sqlparser.DropVindexStr:
name := ddl.VindexSpec.Name.String()
if _, ok := ks.Vindexes[name]; !ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vindex %s does not exists in keyspace %s", name, ksName)
}

for tableName, table := range ks.Tables {
// Make sure there isn't a vindex with the same name left on the table.
for _, vindex := range table.ColumnVindexes {
if vindex.Name == name {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "can not drop vindex cause %s still defined on table %s", name, tableName)
}
}
}

delete(ks.Vindexes, name)

return ks, nil

case sqlparser.AddVschemaTableStr:
if ks.Sharded {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "add vschema table: unsupported on sharded keyspace %s", ksName)
Expand Down
68 changes: 68 additions & 0 deletions go/vt/vtgate/plan_executor_vschema_ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,74 @@ func TestPlanExecutorCreateVindexDDL(t *testing.T) {
}
}

func TestPlanExecutorDropVindexDDL(t *testing.T) {
*vschemaacl.AuthorizedDDLUsers = "%"
defer func() {
*vschemaacl.AuthorizedDDLUsers = ""
}()
executor, _, _, _ := createExecutorEnvUsing(planAllTheThings)
ks := "TestExecutor"

vschemaUpdates := make(chan *vschemapb.SrvVSchema, 4)
executor.serv.WatchSrvVSchema(context.Background(), "aa", func(vschema *vschemapb.SrvVSchema, err error) {
vschemaUpdates <- vschema
})

vschema := <-vschemaUpdates
_, ok := vschema.Keyspaces[ks].Vindexes["test_vindex"]
if ok {
t.Fatalf("test_vindex should not exist in original vschema")
}

session := NewSafeSession(&vtgatepb.Session{TargetString: ks})
stmt := "alter vschema drop vindex test_vindex"
_, err := executor.Execute(context.Background(), "TestExecute", session, stmt, nil)
wantErr := "vindex test_vindex does not exists in keyspace TestExecutor"
if err == nil || err.Error() != wantErr {
t.Errorf("want error %v got %v", wantErr, err)
}

stmt = "alter vschema drop vindex TestExecutor.test_vindex"
_, err = executor.Execute(context.Background(), "TestExecute", session, stmt, nil)
wantErr = "vindex test_vindex does not exists in keyspace TestExecutor"
if err == nil || err.Error() != wantErr {
t.Errorf("want error %v got %v", wantErr, err)
}

//add one vindex that has never been used by the tables
stmt = "alter vschema create vindex test_vindex using hash"
_, err = executor.Execute(context.Background(), "TestExecute", session, stmt, nil)
require.NoError(t, err)

_, vindex := waitForVindex(t, ks, "test_vindex", vschemaUpdates, executor)
if vindex == nil || vindex.Type != "hash" {
t.Errorf("updated vschema did not contain test_vindex")
}

//drop an existing vindex that has never been used by the tables
stmt = "alter vschema drop vindex TestExecutor.test_vindex"
_, err = executor.Execute(context.Background(), "TestExecute", session, stmt, nil)
require.NoError(t, err)
vschema = <-vschemaUpdates
_, ok = vschema.Keyspaces[ks].Vindexes["test_vindex"]
if ok {
t.Fatalf("test_vindex should not exist after droping it")
}

//drop an existing vindex that is used by at least one table
stmt = "alter vschema drop vindex TestExecutor.keyspace_id"
_, err = executor.Execute(context.Background(), "TestExecute", session, stmt, nil)
wantErr = "can not drop vindex cause keyspace_id still defined on table ksid_table"
if err == nil || err.Error() != wantErr {
t.Errorf("drop vindex still defined: %v, want %s", err, wantErr)
}
select {
case <-vschemaUpdates:
t.Error("vschema should not be updated on error")
default:
}
}

func TestPlanExecutorAddDropVschemaTableDDL(t *testing.T) {
*vschemaacl.AuthorizedDDLUsers = "%"
defer func() {
Expand Down