Skip to content

Commit

Permalink
Add unit test code for dropping a table that doesn't exist
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <mattalord@gmail.com>
  • Loading branch information
mattlord committed Jul 30, 2024
1 parent 18812c8 commit 7ce084b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
4 changes: 3 additions & 1 deletion go/test/endtoend/vreplication/vreplication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,9 @@ func materialize(t *testing.T, spec string, useVtctldClient bool) {

func testMaterializeWithNonExistentTable(t *testing.T) {
t.Run("vtctldclient materialize with nonexistent table", func(t *testing.T) {
tableSettings := `[{"target_table": "table_that_doesnt_exist", "create_ddl": "create table mat_val_counts (mat_val varbinary(10), cnt int unsigned, primary key (mat_val))", "source_expression": "select val, count(*) as cnt from mat group by val"}]`
tableSettings := `[
{"target_table": "table_that_doesnt_exist", "create_ddl": "create table mat_val_counts (mat_val varbinary(10), cnt int unsigned, primary key (mat_val))", "source_expression": "select val, count(*) as cnt from mat group by val"},
]`
output, err := vc.VtctldClient.ExecuteCommandWithOutput("materialize", "--workflow=tablenogood", "--target-keyspace=source",
"create", "--source-keyspace=source", "--table-settings", tableSettings)
require.NoError(t, err, "Materialize create failed, err: %v, output: %s", err, output)
Expand Down
3 changes: 2 additions & 1 deletion go/vt/vtctl/workflow/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type testKeyspace struct {
type queryResult struct {
query string
result *querypb.QueryResult
err error
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -389,7 +390,7 @@ func (tmc *testTMClient) VReplicationExec(ctx context.Context, tablet *topodatap
return nil, fmt.Errorf("tablet %v:\nunexpected query\n%s\nwant:\n%s", tablet, query, qrs[0].query)
}
tmc.vrQueries[int(tablet.Alias.Uid)] = qrs[1:]
return qrs[0].result, nil
return qrs[0].result, qrs[0].err
}

func (tmc *testTMClient) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, req *tabletmanagerdatapb.ExecuteFetchAsDbaRequest) (*querypb.QueryResult, error) {
Expand Down
55 changes: 48 additions & 7 deletions go/vt/vtctl/workflow/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,34 @@ func TestWorkflowDelete(t *testing.T) {
defer cancel()

workflowName := "wf1"
tableName := "t1"
table1Name := "t1"
table2Name := "t1_2"
table3Name := "t1_3"
tableTemplate := "CREATE TABLE %s (id BIGINT, name VARCHAR(64), PRIMARY KEY (id))"
sourceKeyspaceName := "sourceks"
targetKeyspaceName := "targetks"
schema := map[string]*tabletmanagerdatapb.SchemaDefinition{
"t1": {
table1Name: {
TableDefinitions: []*tabletmanagerdatapb.TableDefinition{
{
Name: tableName,
Schema: fmt.Sprintf("CREATE TABLE %s (id BIGINT, name VARCHAR(64), PRIMARY KEY (id))", tableName),
Name: table1Name,
Schema: fmt.Sprintf(tableTemplate, table1Name),
},
},
},
table2Name: {
TableDefinitions: []*tabletmanagerdatapb.TableDefinition{
{
Name: table2Name,
Schema: fmt.Sprintf(tableTemplate, table2Name),
},
},
},
table3Name: {
TableDefinitions: []*tabletmanagerdatapb.TableDefinition{
{
Name: table3Name,
Schema: fmt.Sprintf(tableTemplate, table3Name),
},
},
},
Expand Down Expand Up @@ -261,7 +280,21 @@ func TestWorkflowDelete(t *testing.T) {
},
expectedTargetQueries: []*queryResult{
{
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, tableName),
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, table1Name),
result: &querypb.QueryResult{},
},
{
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, table2Name),
result: &querypb.QueryResult{},
// We don't care that the cell and tablet info is off in the error message, only that
// it contains the expected SQL error we'd encounter when attempting to drop a table
// that doesn't exist. That will then cause this error to be non-fatal and the workflow
// delete work will continue.
err: fmt.Errorf("rpc error: code = Unknown desc = TabletManager.ExecuteFetchAsDba on cell-01: rpc error: code = Unknown desc = Unknown table 'vt_%s.%s' (errno 1051) (sqlstate 42S02) during query: drop table `vt_%s`.`%s`",
targetKeyspaceName, table2Name, targetKeyspaceName, table2Name),
},
{
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, table3Name),
result: &querypb.QueryResult{},
},
},
Expand Down Expand Up @@ -298,7 +331,7 @@ func TestWorkflowDelete(t *testing.T) {
defer targetUnlock(&err)
for _, shard := range env.targetKeyspace.ShardNames {
_, err := env.ts.UpdateShardFields(lockCtx, targetKeyspaceName, shard, func(si *topo.ShardInfo) error {
err := si.UpdateDeniedTables(lockCtx, topodatapb.TabletType_PRIMARY, nil, false, []string{tableName, "t2", "t3"})
err := si.UpdateDeniedTables(lockCtx, topodatapb.TabletType_PRIMARY, nil, false, []string{table1Name, "t2", "t3"})
return err
})
require.NoError(t, err)
Expand All @@ -317,7 +350,15 @@ func TestWorkflowDelete(t *testing.T) {
},
expectedTargetQueries: []*queryResult{
{
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, tableName),
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, table1Name),
result: &querypb.QueryResult{},
},
{
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, table2Name),
result: &querypb.QueryResult{},
},
{
query: fmt.Sprintf("drop table `vt_%s`.`%s`", targetKeyspaceName, table3Name),
result: &querypb.QueryResult{},
},
},
Expand Down

0 comments on commit 7ce084b

Please sign in to comment.