Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: refactor the catalog tests (#3934)

rebelice95c0d25b5bc6
diff --git a/plugin/advisor/catalog/finder.go b/plugin/advisor/catalog/finder.go
index 1826d3d121..212569c101 100644
--- a/plugin/advisor/catalog/finder.go
+++ b/plugin/advisor/catalog/finder.go
@@ -10,7 +10,7 @@ type FinderContext struct {
// CheckIntegrity defines the policy for integrity checking.
// There are two cases that will cause database to have an empty catalog:
// 1. we cannot fetch the catalog, such as GitHub App/Actions.
- // 2. the databse is indeed empty.
+ // 2. the database is indeed empty.
// We need different logic to deal with these two cases separately.
// If DROP TABLE t and t not exists:
// 1. For case one, just ignore this statement.
diff --git a/plugin/advisor/catalog/state.go b/plugin/advisor/catalog/state.go
index d450c69fe7..5e70aeed07 100644
--- a/plugin/advisor/catalog/state.go
+++ b/plugin/advisor/catalog/state.go
@@ -96,7 +96,7 @@ func newColumnState(c *storepb.ColumnMetadata, position int) *ColumnState {
func newIndexState(i *storepb.IndexMetadata) *IndexState {
index := &IndexState{
name: i.Name,
- indextype: newStringPointer(i.Type),
+ indexType: newStringPointer(i.Type),
unique: newBoolPointer(i.Unique),
primary: newBoolPointer(i.Primary),
visible: newBoolPointer(i.Visible),
@@ -322,7 +322,7 @@ type IndexState struct {
// This could refer to a column or an expression.
expressionList []string
// Type isn't supported for SQLite.
- indextype *string
+ indexType *string
unique *bool
primary *bool
// Visible isn't supported for Postgres, SQLite.
@@ -335,7 +335,7 @@ func (idx *IndexState) copy() *IndexState {
return &IndexState{
name: idx.name,
expressionList: copyStringSlice(idx.expressionList),
- indextype: copyStringPointer(idx.indextype),
+ indexType: copyStringPointer(idx.indexType),
unique: copyBoolPointer(idx.unique),
primary: copyBoolPointer(idx.primary),
visible: copyBoolPointer(idx.visible),
@@ -351,7 +351,7 @@ func (idx *IndexState) Unique() bool {
return false
}
-// Primary returns the priamry for the index.
+// Primary returns the primary for the index.
func (idx *IndexState) Primary() bool {
if idx.primary != nil {
return *idx.primary
diff --git a/plugin/advisor/catalog/test/mysql_walk_through.yaml b/plugin/advisor/catalog/test/mysql_walk_through.yaml
new file mode 100644
index 0000000000..f3971f7f3a
--- /dev/null
+++ b/plugin/advisor/catalog/test/mysql_walk_through.yaml
@@ -0,0 +1,885 @@
+- statement: |-
+ CREATE TABLE t(a TEXT);
+ ALTER TABLE t ALTER COLUMN a SET DEFAULT '1';
+ want: null
+ err:
+ type: 407
+ content: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ line: 2
+- statement: ' CREATE TABLE t(a TEXT DEFAULT ''1'')'
+ want: null
+ err:
+ type: 407
+ content: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ line: 1
+- statement: |-
+ CREATE TABLE t(a INT NOT NULL);
+ ALTER TABLE t ALTER COLUMN a SET DEFAULT NULL;
+ want: null
+ err:
+ type: 406
+ content: Invalid default value for column `a`
+ line: 2
+- statement: ' CREATE TABLE t(a INT NOT NULL DEFAULT NULL)'
+ want: null
+ err:
+ type: 406
+ content: Invalid default value for column `a`
+ line: 1
+- statement: ' CREATE TABLE t(a int ON UPDATE NOW())'
+ want: null
+ err:
+ type: 405
+ content: Column `a` use ON UPDATE but is not DATETIME or TIMESTAMP
+ line: 1
+- statement: ' CREATE TABLE t(a int auto_increment, b int auto_increment);'
+ want: null
+ err:
+ type: 404
+ content: There can be only one auto column for table `t`
+ line: 1
+- statement: ' CREATE TABLE t as select * from t1;'
+ want: null
+ err:
+ type: 303
+ content: Disallow the CREATE TABLE AS statement but "CREATE TABLE t as select * from t1;" uses
+ line: 1
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tCREATE INDEX idx_c on t(c);\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t MODIFY COLUMN c int;\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t CHANGE COLUMN c aa int;\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t DROP COLUMN c;\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t RENAME COLUMN c TO cc;\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t RENAME COLUMN c TO cc;\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t ALTER COLUMN c DROP DEFAULT;\n\t\t\t"
+ want: null
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+- statement: |-
+ ALTER DATABASE CHARACTER SET = utf8mb4;
+ ALTER DATABASE test COLLATE utf8mb4_polish_ci;
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables: []
+ views: []
+ characterset: utf8mb4
+ collation: utf8mb4_polish_ci
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t(
+ a int PRIMARY KEY DEFAULT 1,
+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
+ c int auto_increment NULL COMMENT 'This is a comment' DEFAULT NULL,
+ d varchar(10) COLLATE utf8mb4_polish_ci,
+ KEY idx_a (a),
+ INDEX (b, a),
+ UNIQUE (b, c, d),
+ FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
+ );
+ CREATE TABLE t_copy like t;
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 1
+ hasdefault: true
+ default: "1"
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: varchar(200)
+ characterset: utf8mb4
+ collation: ""
+ comment: ""
+ - name: c
+ position: 3
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: This is a comment
+ - name: d
+ position: 4
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(10)
+ characterset: ""
+ collation: utf8mb4_polish_ci
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ - name: b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_2
+ expressions:
+ - b
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_3
+ expressions:
+ - b
+ - c
+ - d
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ - name: t_copy
+ columns:
+ - name: a
+ position: 1
+ hasdefault: true
+ default: "1"
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: varchar(200)
+ characterset: utf8mb4
+ collation: ""
+ comment: ""
+ - name: c
+ position: 3
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: This is a comment
+ - name: d
+ position: 4
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(10)
+ characterset: ""
+ collation: utf8mb4_polish_ci
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ - name: b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_2
+ expressions:
+ - b
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_3
+ expressions:
+ - b
+ - c
+ - d
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t(
+ a int PRIMARY KEY DEFAULT 1,
+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
+ c int auto_increment NULL COMMENT 'This is a comment',
+ d varchar(10) COLLATE utf8mb4_polish_ci,
+ KEY idx_a (a),
+ INDEX (b, a),
+ UNIQUE (b, c, d),
+ FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
+ )
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 1
+ hasdefault: true
+ default: "1"
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: varchar(200)
+ characterset: utf8mb4
+ collation: ""
+ comment: ""
+ - name: c
+ position: 3
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: This is a comment
+ - name: d
+ position: 4
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(10)
+ characterset: ""
+ collation: utf8mb4_polish_ci
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ - name: b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_2
+ expressions:
+ - b
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_3
+ expressions:
+ - b
+ - c
+ - d
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t(
+ a int,
+ b int,
+ PRIMARY KEY (a, b)
+ )
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 1
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ - b
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t1(a int, b int, c int);
+ CREATE TABLE t2(a int);
+ DROP TABLE t1, t2
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: ' DROP TABLE t1, t2'
+ want: null
+ err:
+ type: 302
+ content: Table `t1` does not exist
+ line: 1
+- statement: |-
+ CREATE TABLE t(a int);
+ RENAME TABLE t to other_db.t1
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t(a int);
+ RENAME TABLE t to test.t1
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t1
+ columns:
+ - name: a
+ position: 1
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ indexes: []
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ -- this is comment
+ DROP DATABASE test;
+ CREATE TABLE t(a int);
+ want: null
+ err:
+ type: 202
+ content: Database `test` is deleted
+ line: 3
+- statement: |-
+ CREATE TABLE t(
+ a int PRIMARY KEY DEFAULT 1,
+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
+ c int auto_increment NULL COMMENT 'This is a comment',
+ d varchar(10) COLLATE utf8mb4_polish_ci,
+ e int,
+ KEY idx_a (a),
+ INDEX (b, a),
+ UNIQUE (b, c, d),
+ FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
+ );
+ ALTER TABLE t COLLATE utf8mb4_0900_ai_ci, ENGINE = INNODB, COMMENT 'This is a table comment';
+ ALTER TABLE t ADD COLUMN a1 int AFTER a;
+ ALTER TABLE t ADD INDEX idx_a_b (a, b);
+ ALTER TABLE t DROP COLUMN c;
+ ALTER TABLE t DROP PRIMARY KEY;
+ ALTER TABLE t DROP INDEX b_2;
+ ALTER TABLE t MODIFY COLUMN b varchar(20) FIRST;
+ ALTER TABLE t CHANGE COLUMN d d_copy varchar(10) COLLATE utf8mb4_polish_ci;
+ ALTER TABLE t RENAME COLUMN a to a_copy;
+ ALTER TABLE t RENAME TO t_copy;
+ ALTER TABLE t_copy ALTER COLUMN a_copy DROP DEFAULT;
+ ALTER TABLE t_copy RENAME INDEX b TO idx_b;
+ ALTER TABLE t_copy ALTER INDEX b_3 INVISIBLE;
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t_copy
+ columns:
+ - name: b
+ position: 1
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(20)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: a_copy
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: a1
+ position: 3
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: d_copy
+ position: 4
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(10)
+ characterset: ""
+ collation: utf8mb4_polish_ci
+ comment: ""
+ - name: e
+ position: 5
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ indexes:
+ - name: b_3
+ expressions:
+ - b
+ - d_copy
+ type: BTREE
+ unique: true
+ primary: false
+ visible: false
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d_copy
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a_copy
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: idx_a_b
+ expressions:
+ - a_copy
+ - b
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: idx_b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ engine: INNODB
+ collation: utf8mb4_0900_ai_ci
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: This is a table comment
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t(
+ a int PRIMARY KEY DEFAULT 1,
+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
+ c int auto_increment NULL COMMENT 'This is a comment',
+ d varchar(10) COLLATE utf8mb4_polish_ci
+ );
+ CREATE INDEX idx_a on t(a);
+ CREATE INDEX b_2 on t(b, a);
+ CREATE UNIQUE INDEX b_3 on t(b, c, d);
+ CREATE FULLTEXT INDEX b_4 on t(b, d) WITH PARSER ngram INVISIBLE;
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 1
+ hasdefault: true
+ default: "1"
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: varchar(200)
+ characterset: utf8mb4
+ collation: ""
+ comment: ""
+ - name: c
+ position: 3
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: This is a comment
+ - name: d
+ position: 4
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(10)
+ characterset: ""
+ collation: utf8mb4_polish_ci
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ - name: b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_2
+ expressions:
+ - b
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_3
+ expressions:
+ - b
+ - c
+ - d
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |-
+ CREATE TABLE t(
+ a int PRIMARY KEY DEFAULT 1,
+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE
+ );
+ DROP INDEX b on t;
+ want:
+ name: test
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 1
+ hasdefault: true
+ default: "1"
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: varchar(200)
+ characterset: utf8mb4
+ collation: ""
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
diff --git a/plugin/advisor/catalog/test/mysql_walk_through_for_incomplete.yaml b/plugin/advisor/catalog/test/mysql_walk_through_for_incomplete.yaml
new file mode 100644
index 0000000000..9a24fb056c
--- /dev/null
+++ b/plugin/advisor/catalog/test/mysql_walk_through_for_incomplete.yaml
@@ -0,0 +1,310 @@
+- statement: CREATE INDEX idx_a on t(a)
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns: []
+ indexes:
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: ALTER TABLE t RENAME COLUMN a TO a_copy
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a_copy
+ position: 0
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: ""
+ characterset: ""
+ collation: ""
+ comment: ""
+ indexes: []
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: ALTER TABLE t RENAME TO t1
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables:
+ - name: t1
+ columns: []
+ indexes: []
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |4-
+ ALTER TABLE t ADD PRIMARY KEY (a);
+ ALTER TABLE t ADD UNIQUE (b);
+ CREATE INDEX idx_a on t(a);
+ CREATE INDEX b_2 on t(b, a);
+ CREATE UNIQUE INDEX b_3 on t(b, c, d);
+ CREATE FULLTEXT INDEX b_4 on t(b, d) WITH PARSER ngram INVISIBLE;
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns: []
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ - name: b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_2
+ expressions:
+ - b
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_3
+ expressions:
+ - b
+ - c
+ - d
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: |4-
+ CREATE TABLE t(
+ a int PRIMARY KEY DEFAULT 1,
+ b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
+ c int auto_increment NULL COMMENT 'This is a comment',
+ d varchar(10) COLLATE utf8mb4_polish_ci,
+ KEY idx_a (a),
+ INDEX (b, a),
+ UNIQUE (b, c, d),
+ FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
+ )
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 1
+ hasdefault: true
+ default: "1"
+ nullable: false
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: ""
+ - name: b
+ position: 2
+ hasdefault: false
+ default: ""
+ nullable: false
+ type: varchar(200)
+ characterset: utf8mb4
+ collation: ""
+ comment: ""
+ - name: c
+ position: 3
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: int(11)
+ characterset: ""
+ collation: ""
+ comment: This is a comment
+ - name: d
+ position: 4
+ hasdefault: false
+ default: ""
+ nullable: true
+ type: varchar(10)
+ characterset: ""
+ collation: utf8mb4_polish_ci
+ comment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ type: BTREE
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ - name: b
+ expressions:
+ - b
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_2
+ expressions:
+ - b
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_3
+ expressions:
+ - b
+ - c
+ - d
+ type: BTREE
+ unique: true
+ primary: false
+ visible: true
+ comment: ""
+ - name: b_4
+ expressions:
+ - b
+ - d
+ type: FULLTEXT
+ unique: false
+ primary: false
+ visible: false
+ comment: ""
+ - name: idx_a
+ expressions:
+ - a
+ type: BTREE
+ unique: false
+ primary: false
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: DROP TABLE t1, t2
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
+- statement: INSERT INTO test values (1)
+ want:
+ name: ""
+ schemas:
+ - name: ""
+ tables: []
+ views: []
+ characterset: ""
+ collation: ""
+ extensions: []
+ err: null
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index 52b6756ae3..ac679c3ee9 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -1203,7 +1203,7 @@ func (t *TableState) createIndex(name string, keyList []string, unique bool, tp
index := &IndexState{
name: name,
expressionList: keyList,
- indextype: &tp,
+ indexType: &tp,
unique: &unique,
primary: newFalsePointer(),
visible: newTruePointer(),
@@ -1229,7 +1229,7 @@ func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughErro
pk := &IndexState{
name: PrimaryKeyName,
expressionList: keys,
- indextype: &tp,
+ indexType: &tp,
unique: newTruePointer(),
primary: newTruePointer(),
visible: newTruePointer(),
diff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.go
index 0f82ac99fa..044a694ab9 100644
--- a/plugin/advisor/catalog/walk_through_test.go
+++ b/plugin/advisor/catalog/walk_through_test.go
@@ -1,9 +1,14 @@
package catalog
import (
+ "io"
+ "os"
+ "path/filepath"
+ "sort"
"testing"
"github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/plugin/advisor/db"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -13,1356 +18,229 @@ import (
)
type testData struct {
- origin *storepb.DatabaseMetadata
- statement string
- want *storepb.DatabaseMetadata
- err error
+ Statement string
+ Want *storepb.DatabaseMetadata
+ Err *WalkThroughError
}
-var (
- one = "1"
-)
+func TestMySQLWalkThrough(t *testing.T) {
+ originDatabase := &storepb.DatabaseMetadata{
+ Name: "test",
+ }
-func TestWalkThrough(t *testing.T) {
- tests := []testData{
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a TEXT);
- ALTER TABLE t ALTER COLUMN a SET DEFAULT '1';
- `,
- err: &WalkThroughError{
- Type: ErrorTypeInvalidColumnTypeForDefaultValue,
- Content: "BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a TEXT DEFAULT '1')
- `,
- err: &WalkThroughError{
- Type: ErrorTypeInvalidColumnTypeForDefaultValue,
- Content: "BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value",
- Line: 2,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a INT NOT NULL);
- ALTER TABLE t ALTER COLUMN a SET DEFAULT NULL;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeSetNullDefaultForNotNullColumn,
- Content: "Invalid default value for column `a`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a INT NOT NULL DEFAULT NULL)
- `,
- err: &WalkThroughError{
- Type: ErrorTypeSetNullDefaultForNotNullColumn,
- Content: "Invalid default value for column `a`",
- Line: 2,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int ON UPDATE NOW())
- `,
- err: &WalkThroughError{
- Type: ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp,
- Content: "Column `a` use ON UPDATE but is not DATETIME or TIMESTAMP",
- Line: 2,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int auto_increment, b int auto_increment);
- `,
- err: &WalkThroughError{
- Type: ErrorTypeAutoIncrementExists,
- Content: "There can be only one auto column for table `t`",
- Line: 2,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t as select * from t1;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeUseCreateTableAs,
- Content: "Disallow the CREATE TABLE AS statement but \"CREATE TABLE t as select * from t1;\" uses",
- Line: 2,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- CREATE INDEX idx_c on t(c);
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- ALTER TABLE t MODIFY COLUMN c int;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- ALTER TABLE t CHANGE COLUMN c aa int;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- ALTER TABLE t DROP COLUMN c;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- ALTER TABLE t RENAME COLUMN c TO cc;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
+ tests := []string{
+ "mysql_walk_through",
+ }
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- ALTER TABLE t RENAME COLUMN c TO cc;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int, b int);
- ALTER TABLE t ALTER COLUMN c DROP DEFAULT;
- `,
- err: &WalkThroughError{
- Type: ErrorTypeColumnNotExists,
- Content: "Column `c` does not exist in table `t`",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- ALTER DATABASE CHARACTER SET = utf8mb4;
- ALTER DATABASE test COLLATE utf8mb4_polish_ci;
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
- CharacterSet: "utf8mb4",
- Collation: "utf8mb4_polish_ci",
- Schemas: []*storepb.SchemaMetadata{{}},
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY DEFAULT 1,
- b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
- c int auto_increment NULL COMMENT 'This is a comment' DEFAULT NULL,
- d varchar(10) COLLATE utf8mb4_polish_ci,
- KEY idx_a (a),
- INDEX (b, a),
- UNIQUE (b, c, d),
- FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
- );
- CREATE TABLE t_copy like t;
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+ for _, test := range tests {
+ runWalkThroughTest(t, test, originDatabase, false /* record */)
+ }
+}
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t_copy",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- HasDefault: true,
- Default: one,
- Nullable: false,
- Type: "int(11)",
- },
- {
- Name: "b",
- Position: 2,
- HasDefault: false,
- Nullable: false,
- Type: "varchar(200)",
- CharacterSet: "utf8mb4",
- },
- {
- Name: "c",
- Position: 3,
- HasDefault: false,
- Nullable: true,
- Type: "int(11)",
- Comment: "This is a comment",
- },
- {
- Name: "d",
- Position: 4,
- HasDefault: false,
- Nullable: true,
- Type: "varchar(10)",
- Collation: "utf8mb4_polish_ci",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "PRIMARY",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
- },
- {
- Name: "b",
- Expressions: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "idx_a",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_2",
- Expressions: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_3",
- Expressions: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_4",
- Expressions: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
- },
- },
- },
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- HasDefault: true,
- Default: one,
- Nullable: false,
- Type: "int(11)",
- },
- {
- Name: "b",
- Position: 2,
- Nullable: false,
- Type: "varchar(200)",
- CharacterSet: "utf8mb4",
- },
- {
- Name: "c",
- Position: 3,
- Nullable: true,
- Type: "int(11)",
- Comment: "This is a comment",
- },
- {
- Name: "d",
- Position: 4,
- Nullable: true,
- Type: "varchar(10)",
- Collation: "utf8mb4_polish_ci",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "PRIMARY",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
- },
- {
- Name: "b",
- Expressions: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "idx_a",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_2",
- Expressions: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_3",
- Expressions: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_4",
- Expressions: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
- },
- },
- },
- },
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY DEFAULT 1,
- b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
- c int auto_increment NULL COMMENT 'This is a comment',
- d varchar(10) COLLATE utf8mb4_polish_ci,
- KEY idx_a (a),
- INDEX (b, a),
- UNIQUE (b, c, d),
- FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
- )
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+func TestMySQLWalkThroughForIncomplete(t *testing.T) {
+ tests := []string{
+ "mysql_walk_through_for_incomplete",
+ }
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- HasDefault: true,
- Default: one,
- Nullable: false,
- Type: "int(11)",
- },
- {
- Name: "b",
- Position: 2,
- Nullable: false,
- Type: "varchar(200)",
- CharacterSet: "utf8mb4",
- },
- {
- Name: "c",
- Position: 3,
- Nullable: true,
- Type: "int(11)",
- Comment: "This is a comment",
- },
- {
- Name: "d",
- Position: 4,
- Nullable: true,
- Type: "varchar(10)",
- Collation: "utf8mb4_polish_ci",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "PRIMARY",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
- },
- {
- Name: "b",
- Expressions: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "idx_a",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_2",
- Expressions: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_3",
- Expressions: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_4",
- Expressions: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
- },
- },
- },
- },
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(
- a int,
- b int,
- PRIMARY KEY (a, b)
- )
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+ for _, test := range tests {
+ runWalkThroughTest(t, test, nil, false /* record */)
+ }
+}
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- Type: "int(11)",
- Nullable: false,
- },
- {
- Name: "b",
- Position: 2,
- Type: "int(11)",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "PRIMARY",
- Expressions: []string{"a", "b"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
- },
- },
- },
- },
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t1(a int, b int, c int);
- CREATE TABLE t2(a int);
- DROP TABLE t1, t2
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+func runWalkThroughTest(t *testing.T, file string, originDatabase *storepb.DatabaseMetadata, record bool) {
+ tests := []testData{}
+ filepath := filepath.Join("test", file+".yaml")
+ yamlFile, err := os.Open(filepath)
+ require.NoError(t, err)
+ defer yamlFile.Close()
+
+ byteValue, err := io.ReadAll(yamlFile)
+ require.NoError(t, err)
+ err = yaml.Unmarshal(byteValue, &tests)
+ require.NoError(t, err)
+
+ for i, test := range tests {
+ var state *DatabaseState
+ if originDatabase != nil {
+ state = newDatabaseState(originDatabase, &FinderContext{CheckIntegrity: true, EngineType: db.MySQL})
+ } else {
+ finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: db.MySQL})
+ state = finder.Origin
+ }
- Schemas: []*storepb.SchemaMetadata{
- {
- Tables: []*storepb.TableMetadata{},
- Views: []*storepb.ViewMetadata{},
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- DROP TABLE t1, t2
- `,
- err: &WalkThroughError{
- Type: ErrorTypeTableNotExists,
- Content: "Table `t1` does not exist",
- Line: 2,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int);
- RENAME TABLE t to other_db.t1
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+ err := state.WalkThrough(test.Statement)
+ if test.Err != nil {
+ if record {
+ walkThroughError, ok := err.(*WalkThroughError)
+ require.True(t, ok)
+ tests[i].Err = walkThroughError
+ } else {
+ require.Equal(t, err, test.Err)
+ }
+ continue
+ }
+ require.NoError(t, err, test.Statement)
- Schemas: []*storepb.SchemaMetadata{
- {},
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(a int);
- RENAME TABLE t to test.t1
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+ if record {
+ tests[i].Want = state.convertToDatabaseMetadata()
+ } else {
+ require.Equal(t, test.Want, state.convertToDatabaseMetadata(), test.Statement)
+ }
+ }
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t1",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- Nullable: true,
- Type: "int(11)",
- },
- },
- },
- },
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `-- this is comment
- DROP DATABASE test;
- CREATE TABLE t(a int);
- `,
- err: &WalkThroughError{
- Type: ErrorTypeDatabaseIsDeleted,
- Content: "Database `test` is deleted",
- Line: 3,
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY DEFAULT 1,
- b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
- c int auto_increment NULL COMMENT 'This is a comment',
- d varchar(10) COLLATE utf8mb4_polish_ci,
- e int,
- KEY idx_a (a),
- INDEX (b, a),
- UNIQUE (b, c, d),
- FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
- );
- ALTER TABLE t COLLATE utf8mb4_0900_ai_ci, ENGINE = INNODB, COMMENT 'This is a table comment';
- ALTER TABLE t ADD COLUMN a1 int AFTER a;
- ALTER TABLE t ADD INDEX idx_a_b (a, b);
- ALTER TABLE t DROP COLUMN c;
- ALTER TABLE t DROP PRIMARY KEY;
- ALTER TABLE t DROP INDEX b_2;
- ALTER TABLE t MODIFY COLUMN b varchar(20) FIRST;
- ALTER TABLE t CHANGE COLUMN d d_copy varchar(10) COLLATE utf8mb4_polish_ci;
- ALTER TABLE t RENAME COLUMN a to a_copy;
- ALTER TABLE t RENAME TO t_copy;
- ALTER TABLE t_copy ALTER COLUMN a_copy DROP DEFAULT;
- ALTER TABLE t_copy RENAME INDEX b TO idx_b;
- ALTER TABLE t_copy ALTER INDEX b_3 INVISIBLE;
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+ if record {
+ byteValue, err := yaml.Marshal(tests)
+ require.NoError(t, err)
+ err = os.WriteFile(filepath, byteValue, 0644)
+ require.NoError(t, err)
+ }
+}
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t_copy",
- Collation: "utf8mb4_0900_ai_ci",
- Engine: "INNODB",
- Comment: "This is a table comment",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "b",
- Position: 1,
- Nullable: true,
- Type: "varchar(20)",
- },
- {
- Name: "a_copy",
- Position: 2,
- Nullable: false,
- Type: "int(11)",
- },
- {
- Name: "a1",
- Position: 3,
- Nullable: true,
- Type: "int(11)",
- },
+// convertToDatabaseMetadata only used for tests.
+func (d *DatabaseState) convertToDatabaseMetadata() *storepb.DatabaseMetadata {
+ if d.deleted {
+ return nil
+ }
+ return &storepb.DatabaseMetadata{
+ Name: d.name,
+ CharacterSet: d.characterSet,
+ Collation: d.collation,
+ Schemas: d.convertToSchemaMetadataList(),
+ Extensions: []*storepb.ExtensionMetadata{},
+ }
+}
- {
- Name: "d_copy",
- Position: 4,
- Nullable: true,
- Type: "varchar(10)",
- Collation: "utf8mb4_polish_ci",
- },
- {
- Name: "e",
- Position: 5,
- Nullable: true,
- Type: "int(11)",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_b",
- Expressions: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "idx_a",
- Expressions: []string{"a_copy"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_3",
- Expressions: []string{"b", "d_copy"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: false,
- },
- {
- Name: "b_4",
- Expressions: []string{"b", "d_copy"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
- },
- {
- Name: "idx_a_b",
- Expressions: []string{"a_copy", "b"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- },
- },
- },
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY DEFAULT 1,
- b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
- c int auto_increment NULL COMMENT 'This is a comment',
- d varchar(10) COLLATE utf8mb4_polish_ci
- );
- CREATE INDEX idx_a on t(a);
- CREATE INDEX b_2 on t(b, a);
- CREATE UNIQUE INDEX b_3 on t(b, c, d);
- CREATE FULLTEXT INDEX b_4 on t(b, d) WITH PARSER ngram INVISIBLE;
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+// convertToSchemaMetadataList only used for tests.
+func (d *DatabaseState) convertToSchemaMetadataList() []*storepb.SchemaMetadata {
+ result := []*storepb.SchemaMetadata{}
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- HasDefault: true,
- Default: one,
- Nullable: false,
- Type: "int(11)",
- },
- {
- Name: "b",
- Position: 2,
- Nullable: false,
- Type: "varchar(200)",
- CharacterSet: "utf8mb4",
- },
- {
- Name: "c",
- Position: 3,
- Nullable: true,
- Type: "int(11)",
- Comment: "This is a comment",
- },
- {
- Name: "d",
- Position: 4,
- Nullable: true,
- Type: "varchar(10)",
- Collation: "utf8mb4_polish_ci",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "PRIMARY",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
- },
- {
- Name: "b",
- Expressions: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "idx_a",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_2",
- Expressions: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_3",
- Expressions: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
- },
- {
- Name: "b_4",
- Expressions: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
- },
- },
- },
- },
- },
- },
- },
- },
- {
- origin: &storepb.DatabaseMetadata{
- Name: "test",
- },
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY DEFAULT 1,
- b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE
- );
- DROP INDEX b on t;
- `,
- want: &storepb.DatabaseMetadata{
- Name: "test",
+ for _, schema := range d.schemaSet {
+ schemaMeta := &storepb.SchemaMetadata{
+ Name: schema.name,
+ Tables: schema.convertToTableMetadataList(),
+ // TODO(rebelice): convert views if needed.
+ Views: []*storepb.ViewMetadata{},
+ }
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "a",
- Position: 1,
- HasDefault: true,
- Default: one,
- Nullable: false,
- Type: "int(11)",
- },
- {
- Name: "b",
- Position: 2,
- Nullable: false,
- Type: "varchar(200)",
- CharacterSet: "utf8mb4",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "PRIMARY",
- Expressions: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
- },
- },
- },
- },
- },
- },
- },
- },
+ result = append(result, schemaMeta)
}
- for _, test := range tests {
- state := newDatabaseState(test.origin, &FinderContext{CheckIntegrity: true, EngineType: db.MySQL})
- err := state.WalkThrough(test.statement)
- if test.err != nil {
- require.Equal(t, err, test.err)
- continue
+ sort.Slice(result, func(i, j int) bool {
+ return result[i].Name < result[j].Name
+ })
+
+ return result
+}
+
+// convertToTableMetadataList only used for tests.
+func (s *SchemaState) convertToTableMetadataList() []*storepb.TableMetadata {
+ result := []*storepb.TableMetadata{}
+
+ for _, table := range s.tableSet {
+ tableMeta := &storepb.TableMetadata{
+ Name: table.name,
+ Columns: table.convertToColumnMetadataList(),
+ Indexes: table.convertToIndexMetadataList(),
+ ForeignKeys: []*storepb.ForeignKeyMetadata{},
}
- require.NoError(t, err)
- want := newDatabaseState(test.want, &FinderContext{CheckIntegrity: true, EngineType: db.MySQL})
- require.Equal(t, want, state, test.statement)
+
+ if table.engine != nil {
+ tableMeta.Engine = *table.engine
+ }
+
+ if table.collation != nil {
+ tableMeta.Collation = *table.collation
+ }
+
+ if table.comment != nil {
+ tableMeta.Comment = *table.comment
+ }
+
+ result = append(result, tableMeta)
}
-}
-type testDataForIncomplete struct {
- statement string
- want *DatabaseState
- err error
+ sort.Slice(result, func(i, j int) bool {
+ return result[i].Name < result[j].Name
+ })
+
+ return result
}
-func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
- tests := []testDataForIncomplete{
- {
- statement: `CREATE INDEX idx_a on t(a)`,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: "",
- collation: "",
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{
- "t": {
- name: "t",
- engine: nil,
- collation: nil,
- comment: nil,
- columnSet: columnStateMap{},
- indexSet: indexStateMap{
- "idx_a": {
- name: "idx_a",
- expressionList: []string{"a"},
- indextype: newStringPointer("BTREE"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- },
- },
- },
- viewSet: viewStateMap{},
- },
- },
- },
- },
- {
- statement: `ALTER TABLE t RENAME COLUMN a TO a_copy`,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: *newEmptyStringPointer(),
- collation: *newEmptyStringPointer(),
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{
- "t": {
- name: "t",
- engine: nil,
- collation: nil,
- comment: nil,
- columnSet: columnStateMap{
- "a_copy": {
- name: "a_copy",
- },
- },
- indexSet: indexStateMap{},
- },
- },
- viewSet: viewStateMap{},
- },
- },
- },
- },
- {
- statement: `ALTER TABLE t RENAME TO t1`,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: "",
- collation: "",
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{
- "t1": {
- name: "t1",
- engine: nil,
- collation: nil,
- comment: nil,
- columnSet: columnStateMap{},
- indexSet: indexStateMap{},
- },
- },
- viewSet: viewStateMap{},
- },
- },
- },
- },
- {
- statement: `
- ALTER TABLE t ADD PRIMARY KEY (a);
- ALTER TABLE t ADD UNIQUE (b);
- CREATE INDEX idx_a on t(a);
- CREATE INDEX b_2 on t(b, a);
- CREATE UNIQUE INDEX b_3 on t(b, c, d);
- CREATE FULLTEXT INDEX b_4 on t(b, d) WITH PARSER ngram INVISIBLE;
- `,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: "",
- collation: "",
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{
- "t": {
- name: "t",
- engine: nil,
- collation: nil,
- comment: nil,
- columnSet: columnStateMap{},
- indexSet: indexStateMap{
- "PRIMARY": {
- name: "PRIMARY",
- expressionList: []string{"a"},
- indextype: newStringPointer("BTREE"),
- unique: newTruePointer(),
- primary: newTruePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b": {
- name: "b",
- expressionList: []string{"b"},
- indextype: newStringPointer("BTREE"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "idx_a": {
- name: "idx_a",
- expressionList: []string{"a"},
- indextype: newStringPointer("BTREE"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b_2": {
- name: "b_2",
- expressionList: []string{"b", "a"},
- indextype: newStringPointer("BTREE"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b_3": {
- name: "b_3",
- expressionList: []string{"b", "c", "d"},
- indextype: newStringPointer("BTREE"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b_4": {
- name: "b_4",
- expressionList: []string{"b", "d"},
- indextype: newStringPointer("FULLTEXT"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newFalsePointer(),
- comment: newEmptyStringPointer(),
- },
- },
- },
- },
- viewSet: viewStateMap{},
- },
- },
- },
- },
- {
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY DEFAULT 1,
- b varchar(200) CHARACTER SET utf8mb4 NOT NULL UNIQUE,
- c int auto_increment NULL COMMENT 'This is a comment',
- d varchar(10) COLLATE utf8mb4_polish_ci,
- KEY idx_a (a),
- INDEX (b, a),
- UNIQUE (b, c, d),
- FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
- )
- `,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: "",
- collation: "",
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{
- "t": {
- name: "t",
- engine: newEmptyStringPointer(),
- collation: newEmptyStringPointer(),
- comment: newEmptyStringPointer(),
- columnSet: columnStateMap{
- "a": {
- name: "a",
- position: newIntPointer(1),
- defaultValue: &one,
- nullable: newFalsePointer(),
- columnType: newStringPointer("int(11)"),
- characterSet: newEmptyStringPointer(),
- collation: newEmptyStringPointer(),
- comment: newEmptyStringPointer(),
- },
- "b": {
- name: "b",
- position: newIntPointer(2),
- defaultValue: nil,
- nullable: newFalsePointer(),
- columnType: newStringPointer("varchar(200)"),
- characterSet: newStringPointer("utf8mb4"),
- collation: newEmptyStringPointer(),
- comment: newEmptyStringPointer(),
- },
- "c": {
- name: "c",
- position: newIntPointer(3),
- defaultValue: nil,
- nullable: newTruePointer(),
- columnType: newStringPointer("int(11)"),
- characterSet: newEmptyStringPointer(),
- collation: newEmptyStringPointer(),
- comment: newStringPointer("This is a comment"),
- },
- "d": {
- name: "d",
- position: newIntPointer(4),
- defaultValue: nil,
- nullable: newTruePointer(),
- columnType: newStringPointer("varchar(10)"),
- characterSet: newEmptyStringPointer(),
- collation: newStringPointer("utf8mb4_polish_ci"),
- comment: newEmptyStringPointer(),
- },
- },
- indexSet: indexStateMap{
- "PRIMARY": {
- name: "PRIMARY",
- expressionList: []string{"a"},
- indextype: newStringPointer("BTREE"),
- unique: newTruePointer(),
- primary: newTruePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b": {
- name: "b",
- expressionList: []string{"b"},
- indextype: newStringPointer("BTREE"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "idx_a": {
- name: "idx_a",
- expressionList: []string{"a"},
- indextype: newStringPointer("BTREE"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b_2": {
- name: "b_2",
- expressionList: []string{"b", "a"},
- indextype: newStringPointer("BTREE"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b_3": {
- name: "b_3",
- expressionList: []string{"b", "c", "d"},
- indextype: newStringPointer("BTREE"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- },
- "b_4": {
- name: "b_4",
- expressionList: []string{"b", "d"},
- indextype: newStringPointer("FULLTEXT"),
- unique: newFalsePointer(),
- primary: newFalsePointer(),
- visible: newFalsePointer(),
- comment: newEmptyStringPointer(),
- },
- },
- },
- },
- viewSet: viewStateMap{},
- },
- },
- },
- },
- {
- statement: `DROP TABLE t1, t2`,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: "",
- collation: "",
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{},
- viewSet: viewStateMap{},
- },
- },
- },
- },
- {
- statement: `INSERT INTO test values (1)`,
- want: &DatabaseState{
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- characterSet: "",
- collation: "",
- dbType: db.MySQL,
- schemaSet: schemaStateMap{
- "": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{},
- viewSet: viewStateMap{},
- },
- },
- },
- },
+// convertToIndexMetadataList only used for tests.
+func (t *TableState) convertToIndexMetadataList() []*storepb.IndexMetadata {
+ result := []*storepb.IndexMetadata{}
+
+ for _, index := range t.indexSet {
+ indexMeta := &storepb.IndexMetadata{
+ Name: index.name,
+ Expressions: index.ExpressionList(),
+ Unique: index.Unique(),
+ Primary: index.Primary(),
+ }
+
+ if index.indexType != nil {
+ indexMeta.Type = *index.indexType
+ }
+
+ if index.visible != nil {
+ indexMeta.Visible = *index.visible
+ }
+
+ if index.comment != nil {
+ indexMeta.Comment = *index.comment
+ }
+
+ result = append(result, indexMeta)
}
- for _, test := range tests {
- finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: db.MySQL})
- err := finder.WalkThrough(test.statement)
- if test.err != nil {
- require.Equal(t, err, test.err)
- continue
+
+ sort.Slice(result, func(i, j int) bool {
+ return result[i].Name < result[j].Name
+ })
+
+ return result
+}
+
+// convertToColumnMetadataList only used for tests.
+func (t *TableState) convertToColumnMetadataList() []*storepb.ColumnMetadata {
+ result := []*storepb.ColumnMetadata{}
+
+ for _, column := range t.columnSet {
+ columnMeta := &storepb.ColumnMetadata{
+ Name: column.name,
+ Nullable: column.Nullable(),
+ Type: column.Type(),
}
- require.NoError(t, err)
- require.Equal(t, test.want, finder.Final, test.statement)
+
+ if column.defaultValue != nil {
+ columnMeta.HasDefault = true
+ columnMeta.Default = *column.defaultValue
+ }
+
+ if column.characterSet != nil {
+ columnMeta.CharacterSet = *column.characterSet
+ }
+
+ if column.collation != nil {
+ columnMeta.Collation = *column.collation
+ }
+
+ if column.comment != nil {
+ columnMeta.Comment = *column.comment
+ }
+
+ if column.position != nil {
+ columnMeta.Position = int32(*column.position)
+ }
+
+ result = append(result, columnMeta)
}
+
+ sort.Slice(result, func(i, j int) bool {
+ if result[i].Position != result[j].Position {
+ return result[i].Position < result[j].Position
+ }
+ return result[i].Name < result[j].Name
+ })
+
+ return result
}