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

Augmenting the GetSchema RPC to also work for Table and All type of input #13197

Merged
merged 6 commits into from
Jun 6, 2023
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
37 changes: 0 additions & 37 deletions go/mysql/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,43 +98,6 @@ order by table_name, ordinal_position`

// GetColumnNamesQueryPatternForTable is used for mocking queries in unit tests
GetColumnNamesQueryPatternForTable = `SELECT COLUMN_NAME.*TABLE_NAME.*%s.*`

// DetectViewChange query detects if there is any view change from previous copy.
DetectViewChange = `
SELECT distinct table_name
FROM (
SELECT table_name, view_definition
FROM information_schema.views
WHERE table_schema = database()

UNION ALL

SELECT table_name, view_definition
FROM %s.views
WHERE table_schema = database()
) _inner
GROUP BY table_name, view_definition
HAVING COUNT(*) = 1
`
// FetchViewDefinition retrieves view definition from information_schema.views table.
FetchViewDefinition = `select table_name, view_definition from information_schema.views
where table_schema = database() and table_name in ::tableNames`

// FetchCreateStatement retrieves create statement.
FetchCreateStatement = `show create table %s`

// DeleteFromViewsTable removes the views from the table.
DeleteFromViewsTable = `delete from %s.views where table_schema = database() and table_name in ::tableNames`

// InsertIntoViewsTable using information_schema.views.
InsertIntoViewsTable = `insert %s.views(table_schema, table_name, create_statement, view_definition)
values (database(), :table_name, :create_statement, :view_definition)`

// FetchUpdatedViews queries fetches information about updated views
FetchUpdatedViews = `select table_name, create_statement from %s.views where table_schema = database() and table_name in ::viewnames`

// FetchViews queries fetches all views
FetchViews = `select table_name, create_statement from %s.views where table_schema = database()`
)

// BaseShowTablesFields contains the fields returned by a BaseShowTables or a BaseShowTablesForTable command.
Expand Down
2 changes: 1 addition & 1 deletion go/test/endtoend/vreplication/sidecardb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var ddls1, ddls2 []string

func init() {
sidecarDBTables = []string{"copy_state", "dt_participant", "dt_state", "heartbeat", "post_copy_action", "redo_state",
"redo_statement", "reparent_journal", "resharding_journal", "schema_engine_tables", "schema_engine_views", "schema_migrations", "schema_version", "schemacopy",
"redo_statement", "reparent_journal", "resharding_journal", "schema_migrations", "schema_version", "schemacopy", "tables",
"vdiff", "vdiff_log", "vdiff_table", "views", "vreplication", "vreplication_log"}
numSidecarDBTables = len(sidecarDBTables)
ddls1 = []string{
Expand Down
24 changes: 0 additions & 24 deletions go/vt/sidecardb/schema/schemaengine/schema_engine_views.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

CREATE TABLE IF NOT EXISTS schema_engine_tables
CREATE TABLE IF NOT EXISTS tables
(
TABLE_SCHEMA varchar(64) NOT NULL,
TABLE_NAME varchar(64) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ CREATE TABLE IF NOT EXISTS views
TABLE_NAME varchar(64) NOT NULL,
CREATE_STATEMENT longtext,
VIEW_DEFINITION longtext NOT NULL,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (TABLE_SCHEMA, TABLE_NAME)
) engine = InnoDB
2 changes: 1 addition & 1 deletion go/vt/sidecardb/sidecardb.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (si *schemaInit) setCurrentDatabase(dbName string) error {
func (si *schemaInit) getCurrentSchema(tableName string) (string, error) {
var currentTableSchema string

rs, err := si.exec(si.ctx, sqlparser.BuildParsedQuery(showCreateTableQuery, GetIdentifier(), tableName).Query, 1, false)
rs, err := si.exec(si.ctx, sqlparser.BuildParsedQuery(showCreateTableQuery, GetIdentifier(), sqlparser.String(sqlparser.NewIdentifierCS(tableName))).Query, 1, false)
if err != nil {
if sqlErr, ok := err.(*mysql.SQLError); ok && sqlErr.Number() == mysql.ERNoSuchTable {
// table does not exist in the sidecar database
Expand Down
6 changes: 4 additions & 2 deletions go/vt/vttablet/endtoend/framework/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,11 @@ func (client *QueryClient) UpdateContext(ctx context.Context) {
}

func (client *QueryClient) GetSchema(tableType querypb.SchemaTableType, tableNames ...string) (map[string]string, error) {
schemaDef := map[string]string{}
schemaDef := make(map[string]string)
err := client.server.GetSchema(client.ctx, client.target, tableType, tableNames, func(schemaRes *querypb.GetSchemaResponse) error {
schemaDef = schemaRes.TableDefinition
for tableName, schemaDefinition := range schemaRes.TableDefinition {
schemaDef[tableName] = schemaDefinition
}
Comment on lines +420 to +422
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was another bug I found! We shouldn't just be overriding the map we get back, but we should update it.

return nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vttablet/endtoend/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var tableACLConfig = `{
},
{
"name": "vitess",
"table_names_or_prefixes": ["vitess_a", "vitess_b", "vitess_c", "dual", "vitess_d", "vitess_temp", "vitess_e", "vitess_f", "vitess_mixed_case", "upsert_test", "vitess_strings", "vitess_fracts", "vitess_ints", "vitess_misc", "vitess_bit_default", "vitess_big", "vitess_stress", "vitess_view", "vitess_json", "vitess_bool", "vitess_autoinc_seq"],
"table_names_or_prefixes": ["vitess_a", "vitess_b", "vitess_c", "dual", "vitess_d", "vitess_temp", "vitess_temp1", "vitess_temp2", "vitess_temp3", "vitess_e", "vitess_f", "vitess_mixed_case", "upsert_test", "vitess_strings", "vitess_fracts", "vitess_ints", "vitess_misc", "vitess_bit_default", "vitess_big", "vitess_stress", "vitess_view", "vitess_json", "vitess_bool", "vitess_autoinc_seq"],
"readers": ["dev"],
"writers": ["dev"],
"admins": ["dev"]
Expand Down
218 changes: 218 additions & 0 deletions go/vt/vttablet/endtoend/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
Copyright 2023 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 endtoend
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/callerid"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vttablet/endtoend/framework"
)

// TestGetSchemaRPC will validate GetSchema RPC.
func TestGetSchemaRPC(t *testing.T) {
testcases := []struct {
name string
queries []string
deferQueries []string
getSchemaQueryType querypb.SchemaTableType
getSchemaTables []string
mapToExpect map[string]string
}{
{
name: "All views",
queries: []string{
"create view vitess_view1 as select id from vitess_a",
"create view vitess_view2 as select id from vitess_b",
},
deferQueries: []string{
"drop view vitess_view1",
"drop view vitess_view2",
},
mapToExpect: map[string]string{
"vitess_view1": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view1` AS select `vitess_a`.`id` AS `id` from `vitess_a`",
"vitess_view2": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view2` AS select `vitess_b`.`id` AS `id` from `vitess_b`",
},
getSchemaQueryType: querypb.SchemaTableType_VIEWS,
}, {
name: "Views listed",
queries: []string{
"create view vitess_view1 as select eid from vitess_a",
"create view vitess_view2 as select eid from vitess_b",
"create view vitess_view3 as select eid from vitess_c",
},
deferQueries: []string{
"drop view vitess_view1",
"drop view vitess_view2",
"drop view vitess_view3",
},
mapToExpect: map[string]string{
"vitess_view3": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view3` AS select `vitess_c`.`eid` AS `eid` from `vitess_c`",
"vitess_view2": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view2` AS select `vitess_b`.`eid` AS `eid` from `vitess_b`",
// These shouldn't be part of the result so we verify it is empty.
"vitess_view1": "",
"unknown_view": "",
},
getSchemaTables: []string{"vitess_view3", "vitess_view2", "unknown_view"},
getSchemaQueryType: querypb.SchemaTableType_VIEWS,
}, {
name: "All tables",
queries: []string{
"create table vitess_temp1 (id int);",
"create table vitess_temp2 (id int);",
"create table vitess_temp3 (id int);",
},
deferQueries: []string{
"drop table vitess_temp1",
"drop table vitess_temp2",
"drop table vitess_temp3",
},
mapToExpect: map[string]string{
"vitess_temp1": "CREATE TABLE `vitess_temp1` (\n `id` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"vitess_temp2": "CREATE TABLE `vitess_temp2` (\n `id` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"vitess_temp3": "CREATE TABLE `vitess_temp3` (\n `id` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
},
getSchemaQueryType: querypb.SchemaTableType_TABLES,
}, {
name: "Tables listed",
queries: []string{
"create table vitess_temp1 (eid int);",
"create table vitess_temp2 (eid int);",
"create table vitess_temp3 (eid int);",
},
deferQueries: []string{
"drop table vitess_temp1",
"drop table vitess_temp2",
"drop table vitess_temp3",
},
mapToExpect: map[string]string{
"vitess_temp1": "CREATE TABLE `vitess_temp1` (\n `eid` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"vitess_temp3": "CREATE TABLE `vitess_temp3` (\n `eid` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
// These shouldn't be part of the result so we verify it is empty.
"vitess_temp2": "",
"unknown_table": "",
},
getSchemaQueryType: querypb.SchemaTableType_TABLES,
getSchemaTables: []string{"vitess_temp1", "vitess_temp3", "unknown_table"},
}, {
name: "All tables and views",
queries: []string{
"create table vitess_temp1 (id int);",
"create table vitess_temp2 (id int);",
"create table vitess_temp3 (id int);",
"create view vitess_view1 as select id from vitess_a",
"create view vitess_view2 as select id from vitess_b",
},
deferQueries: []string{
"drop table vitess_temp1",
"drop table vitess_temp2",
"drop table vitess_temp3",
"drop view vitess_view1",
"drop view vitess_view2",
},
mapToExpect: map[string]string{
"vitess_view1": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view1` AS select `vitess_a`.`id` AS `id` from `vitess_a`",
"vitess_view2": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view2` AS select `vitess_b`.`id` AS `id` from `vitess_b`",
"vitess_temp1": "CREATE TABLE `vitess_temp1` (\n `id` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"vitess_temp2": "CREATE TABLE `vitess_temp2` (\n `id` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"vitess_temp3": "CREATE TABLE `vitess_temp3` (\n `id` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
},
getSchemaQueryType: querypb.SchemaTableType_ALL,
}, {
name: "Listed tables and views",
queries: []string{
"create table vitess_temp1 (eid int);",
"create table vitess_temp2 (eid int);",
"create table vitess_temp3 (eid int);",
"create view vitess_view1 as select eid from vitess_a",
"create view vitess_view2 as select eid from vitess_b",
"create view vitess_view3 as select eid from vitess_c",
},
deferQueries: []string{
"drop table vitess_temp1",
"drop table vitess_temp2",
"drop table vitess_temp3",
"drop view vitess_view1",
"drop view vitess_view2",
"drop view vitess_view3",
},
mapToExpect: map[string]string{
"vitess_view1": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view1` AS select `vitess_a`.`eid` AS `eid` from `vitess_a`",
"vitess_view3": "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view3` AS select `vitess_c`.`eid` AS `eid` from `vitess_c`",
"vitess_temp1": "CREATE TABLE `vitess_temp1` (\n `eid` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"vitess_temp3": "CREATE TABLE `vitess_temp3` (\n `eid` int DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
// These shouldn't be part of the result so we verify it is empty.
"vitess_temp2": "",
"vitess_view2": "",
"unknown_view": "",
"unknown_table": "",
},
getSchemaQueryType: querypb.SchemaTableType_ALL,
getSchemaTables: []string{"vitess_temp1", "vitess_temp3", "unknown_table", "vitess_view3", "vitess_view1", "unknown_view"},
},
}

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
client := framework.NewClient()
client.UpdateContext(callerid.NewContext(
context.Background(),
&vtrpcpb.CallerID{},
&querypb.VTGateCallerID{Username: "dev"}))

for _, query := range testcase.queries {
_, err := client.Execute(query, nil)
require.NoError(t, err)
}
defer func() {
for _, query := range testcase.deferQueries {
_, err := client.Execute(query, nil)
require.NoError(t, err)
}
}()

timeout := 1 * time.Minute
wait := time.After(timeout)
for {
select {
case <-wait:
t.Errorf("Schema tracking hasn't caught up")
return
case <-time.After(1 * time.Second):
schemaDefs, err := client.GetSchema(testcase.getSchemaQueryType, testcase.getSchemaTables...)
require.NoError(t, err)
success := true
for tableName, expectedCreateStatement := range testcase.mapToExpect {
if schemaDefs[tableName] != expectedCreateStatement {
success = false
break
}
}
if success {
return
}
}
}
})
}
}
25 changes: 0 additions & 25 deletions go/vt/vttablet/endtoend/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,31 +271,6 @@ func TestViewAndTableUnique(t *testing.T) {
require.ErrorContains(t, err, "Table 'vitess_view' already exists")
}

// TestGetSchemaRPC will validate GetSchema rpc..
func TestGetSchemaRPC(t *testing.T) {
client := framework.NewClient()

client.Execute("delete from _vt.views", nil)
viewSchemaDef, err := client.GetSchema(querypb.SchemaTableType_VIEWS)
require.NoError(t, err)
require.Zero(t, len(viewSchemaDef))

client.UpdateContext(callerid.NewContext(
context.Background(),
&vtrpcpb.CallerID{},
&querypb.VTGateCallerID{Username: "dev"}))

defer client.Execute("drop view vitess_view", nil)

_, err = client.Execute("create view vitess_view as select 1 from vitess_a", nil)
require.NoError(t, err)
waitForResult(t, client, 1, 1*time.Minute)

viewSchemaDef, err = client.GetSchema(querypb.SchemaTableType_VIEWS)
require.NoError(t, err)
require.Equal(t, "CREATE ALGORITHM=UNDEFINED DEFINER=`vt_dba`@`localhost` SQL SECURITY DEFINER VIEW `vitess_view` AS select 1 AS `1` from `vitess_a`", viewSchemaDef["vitess_view"])
}

func waitForResult(t *testing.T, client *framework.QueryClient, rowCount int, timeout time.Duration) {
t.Helper()
wait := time.After(timeout)
Expand Down
Loading