2017-05-06 02:21:12 +03:00
|
|
|
/*
|
2019-10-21 17:30:02 +03:00
|
|
|
Copyright 2019 The Vitess Authors.
|
2017-05-06 02:21:12 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-21 17:30:02 +03:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
2017-05-06 02:21:12 +03:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-05-18 01:17:40 +03:00
|
|
|
package mysql
|
2017-01-25 19:39:56 +03:00
|
|
|
|
|
|
|
import (
|
2022-01-25 20:35:05 +03:00
|
|
|
"vitess.io/vitess/go/mysql/collations"
|
2018-02-27 22:58:59 +03:00
|
|
|
"vitess.io/vitess/go/sqltypes"
|
2017-01-25 19:39:56 +03:00
|
|
|
|
2018-02-27 22:58:59 +03:00
|
|
|
querypb "vitess.io/vitess/go/vt/proto/query"
|
2017-01-25 19:39:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file provides a few utility variables and methods, mostly for tests.
|
|
|
|
// The assumptions made about the types of fields and data returned
|
|
|
|
// by MySQl are validated in schema_test.go. This way all tests
|
|
|
|
// can use these variables and methods to simulate a MySQL server
|
|
|
|
// (using fakesqldb/ package for instance) and still be guaranteed correct
|
|
|
|
// data.
|
|
|
|
|
2020-03-22 21:00:30 +03:00
|
|
|
const (
|
|
|
|
// BaseShowPrimary is the base query for fetching primary key info.
|
2022-05-10 21:31:35 +03:00
|
|
|
BaseShowPrimary = `
|
|
|
|
SELECT TABLE_NAME as table_name, COLUMN_NAME as column_name
|
|
|
|
FROM information_schema.STATISTICS
|
|
|
|
WHERE TABLE_SCHEMA = DATABASE() AND LOWER(INDEX_NAME) = 'primary'
|
|
|
|
ORDER BY table_name, SEQ_IN_INDEX`
|
2021-03-03 12:58:06 +03:00
|
|
|
// ShowRowsRead is the query used to find the number of rows read.
|
|
|
|
ShowRowsRead = "show status like 'Innodb_rows_read'"
|
2021-04-30 16:31:01 +03:00
|
|
|
|
2021-05-05 09:34:00 +03:00
|
|
|
// CreateVTDatabase creates the _vt database
|
|
|
|
CreateVTDatabase = `CREATE DATABASE IF NOT EXISTS _vt`
|
|
|
|
|
2021-04-30 16:31:01 +03:00
|
|
|
// CreateSchemaCopyTable query creates schemacopy table in _vt schema.
|
|
|
|
CreateSchemaCopyTable = `
|
|
|
|
CREATE TABLE if not exists _vt.schemacopy (
|
|
|
|
table_schema varchar(64) NOT NULL,
|
|
|
|
table_name varchar(64) NOT NULL,
|
|
|
|
column_name varchar(64) NOT NULL,
|
|
|
|
ordinal_position bigint(21) unsigned NOT NULL,
|
|
|
|
character_set_name varchar(32) DEFAULT NULL,
|
|
|
|
collation_name varchar(32) DEFAULT NULL,
|
2021-05-21 08:44:07 +03:00
|
|
|
data_type varchar(64) NOT NULL,
|
2021-04-30 16:31:01 +03:00
|
|
|
column_key varchar(3) NOT NULL,
|
|
|
|
PRIMARY KEY (table_schema, table_name, ordinal_position))`
|
|
|
|
|
|
|
|
// DetectSchemaChange query detects if there is any schema change from previous copy.
|
2022-06-07 08:45:08 +03:00
|
|
|
DetectSchemaChange = `
|
|
|
|
SELECT DISTINCT table_name
|
|
|
|
FROM (
|
|
|
|
SELECT table_name, column_name, ordinal_position, character_set_name, collation_name, data_type, column_key
|
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_schema = database()
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT table_name, column_name, ordinal_position, character_set_name, collation_name, data_type, column_key
|
|
|
|
FROM _vt.schemacopy c
|
|
|
|
WHERE table_schema = database()
|
|
|
|
) _inner
|
|
|
|
GROUP BY table_name, column_name, ordinal_position, character_set_name, collation_name, data_type, column_key
|
|
|
|
HAVING COUNT(*) = 1
|
|
|
|
`
|
2021-04-30 16:31:01 +03:00
|
|
|
|
|
|
|
// ClearSchemaCopy query clears the schemacopy table.
|
2021-07-22 14:21:56 +03:00
|
|
|
ClearSchemaCopy = `delete from _vt.schemacopy where table_schema = database()`
|
2021-04-30 16:31:01 +03:00
|
|
|
|
|
|
|
// InsertIntoSchemaCopy query copies over the schema information from information_schema.columns table.
|
2022-06-07 08:45:08 +03:00
|
|
|
InsertIntoSchemaCopy = `insert _vt.schemacopy
|
|
|
|
select table_schema, table_name, column_name, ordinal_position, character_set_name, collation_name, data_type, column_key
|
|
|
|
from information_schema.columns
|
2021-04-30 16:31:01 +03:00
|
|
|
where table_schema = database()`
|
2021-05-21 16:02:14 +03:00
|
|
|
|
2021-10-20 15:00:40 +03:00
|
|
|
// fetchColumns are the columns we fetch
|
|
|
|
fetchColumns = "table_name, column_name, data_type, collation_name"
|
|
|
|
|
2021-05-21 16:02:14 +03:00
|
|
|
// FetchUpdatedTables queries fetches all information about updated tables
|
2021-10-20 15:00:40 +03:00
|
|
|
FetchUpdatedTables = `select ` + fetchColumns + `
|
2022-06-07 08:45:08 +03:00
|
|
|
from _vt.schemacopy
|
|
|
|
where table_schema = database() and
|
|
|
|
table_name in ::tableNames
|
2021-05-25 18:52:14 +03:00
|
|
|
order by table_name, ordinal_position`
|
|
|
|
|
|
|
|
// FetchTables queries fetches all information about tables
|
2022-06-07 08:45:08 +03:00
|
|
|
FetchTables = `select ` + fetchColumns + `
|
|
|
|
from _vt.schemacopy
|
|
|
|
where table_schema = database()
|
2021-05-25 18:52:14 +03:00
|
|
|
order by table_name, ordinal_position`
|
2022-03-07 23:01:48 +03:00
|
|
|
|
|
|
|
// GetColumnNamesQueryPatternForTable is used for mocking queries in unit tests
|
|
|
|
GetColumnNamesQueryPatternForTable = `SELECT COLUMN_NAME.*TABLE_NAME.*%s.*`
|
2020-03-22 21:00:30 +03:00
|
|
|
)
|
2017-01-28 00:41:28 +03:00
|
|
|
|
2021-05-05 09:34:00 +03:00
|
|
|
// VTDatabaseInit contains all the schema creation queries needed to
|
|
|
|
var VTDatabaseInit = []string{
|
|
|
|
CreateVTDatabase,
|
|
|
|
CreateSchemaCopyTable,
|
|
|
|
}
|
|
|
|
|
2017-01-28 00:41:28 +03:00
|
|
|
// BaseShowTablesFields contains the fields returned by a BaseShowTables or a BaseShowTablesForTable command.
|
|
|
|
// They are validated by the
|
|
|
|
// testBaseShowTables test.
|
2021-02-04 19:12:44 +03:00
|
|
|
var BaseShowTablesFields = []*querypb.Field{{
|
|
|
|
Name: "t.table_name",
|
|
|
|
Type: querypb.Type_VARCHAR,
|
|
|
|
Table: "tables",
|
|
|
|
OrgTable: "TABLES",
|
|
|
|
Database: "information_schema",
|
|
|
|
OrgName: "TABLE_NAME",
|
|
|
|
ColumnLength: 192,
|
2022-01-25 20:35:05 +03:00
|
|
|
Charset: collations.CollationUtf8ID,
|
2021-02-04 19:12:44 +03:00
|
|
|
Flags: uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
|
|
|
|
}, {
|
|
|
|
Name: "t.table_type",
|
|
|
|
Type: querypb.Type_VARCHAR,
|
|
|
|
Table: "tables",
|
|
|
|
OrgTable: "TABLES",
|
|
|
|
Database: "information_schema",
|
|
|
|
OrgName: "TABLE_TYPE",
|
|
|
|
ColumnLength: 192,
|
2022-01-25 20:35:05 +03:00
|
|
|
Charset: collations.CollationUtf8ID,
|
2021-02-04 19:12:44 +03:00
|
|
|
Flags: uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
|
|
|
|
}, {
|
|
|
|
Name: "unix_timestamp(t.create_time)",
|
|
|
|
Type: querypb.Type_INT64,
|
|
|
|
ColumnLength: 11,
|
2022-01-25 20:35:05 +03:00
|
|
|
Charset: collations.CollationBinaryID,
|
2021-02-04 19:12:44 +03:00
|
|
|
Flags: uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
|
|
|
|
}, {
|
|
|
|
Name: "t.table_comment",
|
|
|
|
Type: querypb.Type_VARCHAR,
|
|
|
|
Table: "tables",
|
|
|
|
OrgTable: "TABLES",
|
|
|
|
Database: "information_schema",
|
|
|
|
OrgName: "TABLE_COMMENT",
|
|
|
|
ColumnLength: 6144,
|
2022-01-25 20:35:05 +03:00
|
|
|
Charset: collations.CollationUtf8ID,
|
2021-02-04 19:12:44 +03:00
|
|
|
Flags: uint32(querypb.MySqlFlag_NOT_NULL_FLAG),
|
|
|
|
}, {
|
|
|
|
Name: "i.file_size",
|
|
|
|
Type: querypb.Type_INT64,
|
|
|
|
ColumnLength: 11,
|
2022-01-25 20:35:05 +03:00
|
|
|
Charset: collations.CollationBinaryID,
|
2021-02-04 19:12:44 +03:00
|
|
|
Flags: uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
|
|
|
|
}, {
|
|
|
|
Name: "i.allocated_size",
|
|
|
|
Type: querypb.Type_INT64,
|
|
|
|
ColumnLength: 11,
|
2022-01-25 20:35:05 +03:00
|
|
|
Charset: collations.CollationBinaryID,
|
2021-02-04 19:12:44 +03:00
|
|
|
Flags: uint32(querypb.MySqlFlag_BINARY_FLAG | querypb.MySqlFlag_NUM_FLAG),
|
|
|
|
}}
|
2017-01-28 00:41:28 +03:00
|
|
|
|
|
|
|
// BaseShowTablesRow returns the fields from a BaseShowTables or
|
|
|
|
// BaseShowTablesForTable command.
|
|
|
|
func BaseShowTablesRow(tableName string, isView bool, comment string) []sqltypes.Value {
|
|
|
|
tableType := "BASE TABLE"
|
|
|
|
if isView {
|
|
|
|
tableType = "VIEW"
|
|
|
|
}
|
|
|
|
return []sqltypes.Value{
|
|
|
|
sqltypes.MakeTrusted(sqltypes.VarChar, []byte(tableName)),
|
|
|
|
sqltypes.MakeTrusted(sqltypes.VarChar, []byte(tableType)),
|
|
|
|
sqltypes.MakeTrusted(sqltypes.Int64, []byte("1427325875")), // unix_timestamp(create_time)
|
|
|
|
sqltypes.MakeTrusted(sqltypes.VarChar, []byte(comment)),
|
2021-02-04 19:12:44 +03:00
|
|
|
sqltypes.MakeTrusted(sqltypes.Int64, []byte("100")), // file_size
|
|
|
|
sqltypes.MakeTrusted(sqltypes.Int64, []byte("150")), // allocated_size
|
2017-01-28 00:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 21:00:30 +03:00
|
|
|
|
2020-03-23 00:08:43 +03:00
|
|
|
// ShowPrimaryFields contains the fields for a BaseShowPrimary.
|
|
|
|
var ShowPrimaryFields = []*querypb.Field{{
|
|
|
|
Name: "table_name",
|
|
|
|
Type: sqltypes.VarChar,
|
|
|
|
}, {
|
|
|
|
Name: "column_name",
|
|
|
|
Type: sqltypes.VarChar,
|
|
|
|
}}
|
2020-03-22 21:00:30 +03:00
|
|
|
|
|
|
|
// ShowPrimaryRow returns a row for a primary key column.
|
|
|
|
func ShowPrimaryRow(tableName, colName string) []sqltypes.Value {
|
|
|
|
return []sqltypes.Value{
|
|
|
|
sqltypes.MakeTrusted(sqltypes.VarChar, []byte(tableName)),
|
|
|
|
sqltypes.MakeTrusted(sqltypes.VarChar, []byte(colName)),
|
|
|
|
}
|
|
|
|
}
|