-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6932 from planetscale/information_schema_routing_…
…rules Route INFORMATION_SCHEMA queries
- Loading branch information
Showing
14 changed files
with
563 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2020 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 vtgate | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/test/endtoend/cluster" | ||
) | ||
|
||
// TestCheckConstraint test check constraints on CREATE TABLE | ||
// This feature is supported from MySQL 8.0.16 and MariaDB 10.2.1. | ||
func TestCheckConstraint(t *testing.T) { | ||
// Skipping as tests are run against MySQL 5.7 | ||
t.Skip() | ||
|
||
conn, err := mysql.Connect(context.Background(), &vtParams) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
query := `CREATE TABLE t7 (CHECK (c1 <> c2), c1 INT CHECK (c1 > 10), c2 INT CONSTRAINT c2_positive CHECK (c2 > 0), c3 INT CHECK (c3 < 100), CONSTRAINT c1_nonzero CHECK (c1 <> 0), CHECK (c1 > c3));` | ||
exec(t, conn, query) | ||
|
||
checkQuery := `SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 't7';` | ||
expected := `[[VARCHAR("t7_chk_1")] [VARCHAR("t7_chk_2")] [VARCHAR("c2_positive")] [VARCHAR("t7_chk_3")] [VARCHAR("c1_nonzero")] [VARCHAR("t7_chk_4")]]` | ||
|
||
assertMatches(t, conn, checkQuery, expected) | ||
|
||
cleanup := `DROP TABLE t7` | ||
exec(t, conn, cleanup) | ||
} | ||
|
||
func TestDbNameOverride(t *testing.T) { | ||
defer cluster.PanicHandler(t) | ||
ctx := context.Background() | ||
conn, err := mysql.Connect(ctx, &vtParams) | ||
require.Nil(t, err) | ||
defer conn.Close() | ||
qr, err := conn.ExecuteFetch("SELECT distinct database() FROM information_schema.tables WHERE table_schema = database()", 1000, true) | ||
|
||
require.Nil(t, err) | ||
assert.Equal(t, 1, len(qr.Rows), "did not get enough rows back") | ||
assert.Equal(t, "vt_ks", qr.Rows[0][0].ToString()) | ||
} | ||
|
||
func TestInformationSchemaQuery(t *testing.T) { | ||
defer cluster.PanicHandler(t) | ||
ctx := context.Background() | ||
conn, err := mysql.Connect(ctx, &vtParams) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
assertSingleRowIsReturned(t, conn, "table_schema = 'ks'") | ||
assertSingleRowIsReturned(t, conn, "table_schema = 'vt_ks'") | ||
assertResultIsEmpty(t, conn, "table_schema = 'NONE'") | ||
} | ||
|
||
func assertResultIsEmpty(t *testing.T, conn *mysql.Conn, pre string) { | ||
t.Run(pre, func(t *testing.T) { | ||
qr, err := conn.ExecuteFetch("SELECT distinct table_schema FROM information_schema.tables WHERE "+pre, 1000, true) | ||
require.NoError(t, err) | ||
assert.Empty(t, qr.Rows) | ||
}) | ||
} | ||
|
||
func assertSingleRowIsReturned(t *testing.T, conn *mysql.Conn, predicate string) { | ||
t.Run(predicate, func(t *testing.T) { | ||
qr, err := conn.ExecuteFetch("SELECT distinct table_schema FROM information_schema.tables WHERE "+predicate, 1000, true) | ||
require.NoError(t, err) | ||
assert.Equal(t, 1, len(qr.Rows), "did not get enough rows back") | ||
assert.Equal(t, "vt_ks", qr.Rows[0][0].ToString()) | ||
}) | ||
} | ||
|
||
func TestInformationSchemaWithSubquery(t *testing.T) { | ||
defer cluster.PanicHandler(t) | ||
ctx := context.Background() | ||
conn, err := mysql.Connect(ctx, &vtParams) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
result := exec(t, conn, "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = (SELECT SCHEMA()) AND TABLE_NAME = 'not_exists'") | ||
assert.Empty(t, result.Rows) | ||
} | ||
|
||
func TestInformationSchemaQueryGetsRoutedToTheRightTableAndKeyspace(t *testing.T) { | ||
defer cluster.PanicHandler(t) | ||
ctx := context.Background() | ||
conn, err := mysql.Connect(ctx, &vtParams) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
_ = exec(t, conn, "SELECT * FROM t1000") // test that the routed table is available to us | ||
result := exec(t, conn, "SELECT * FROM information_schema.tables WHERE table_schema = database() and table_name='t1000'") | ||
assert.NotEmpty(t, result.Rows) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.