Skip to content

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

chore(mysqlv2): support create table options (#9454)

zll600e0307e9e0f82
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
index 5700339a59..64219f83b6 100644
--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
@@ -1,3 +1,64 @@
+- statement: |-
+ CREATE TABLE t1(
+ a INT PRIMARY KEY,
+ b INT,
+ UNIQUE uk1 (b)
+ );
+ ALTER TABLE t1 ENGINE = MyISAM;
+ ALTER TABLE t1 COMMENT = 't1 table';
+ ALTER TABLE t1 COLLATE utf8mb4_unicode_ci;
+ ignore_case_sensitive: false
+ want: |-
+ {
+ "name": "test",
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "t1",
+ "columns": [
+ {
+ "name": "a",
+ "position": 1,
+ "type": "int"
+ },
+ {
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "a"
+ ],
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
+ },
+ {
+ "name": "uk1",
+ "expressions": [
+ "b"
+ ],
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
+ }
+ ],
+ "engine": "MyISAM",
+ "collation": "utf8mb4_unicode_ci",
+ "comment": "t1 table"
+ }
+ ]
+ }
+ ]
+ }
+ err: null
- statement: |-
CREATE TABLE t1(
a INT PRIMARY KEY,
@@ -9,23 +70,23 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "nullable": true,
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
}
]
}
@@ -48,42 +109,42 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t2",
- "columns": [
+ "name": "t2",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "nullable": true,
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "idx2",
- "expressions": [
+ "name": "idx2",
+ "expressions": [
"b"
],
- "type": "BTREE"
+ "type": "BTREE"
},
{
- "name": "uk_b",
- "expressions": [
+ "name": "uk_b",
+ "expressions": [
"b"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
}
]
}
@@ -120,18 +181,18 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "blob"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "blob"
}
]
}
@@ -153,30 +214,30 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
- {
- "name": "a",
- "position": 1,
- "default": "1",
- "nullable": true,
- "type": "int"
+ "name": "t1",
+ "columns": [
+ {
+ "name": "a",
+ "position": 1,
+ "default": "1",
+ "nullable": true,
+ "type": "int"
},
{
- "name": "d",
- "position": 2,
- "nullable": true,
- "type": "text"
+ "name": "d",
+ "position": 2,
+ "nullable": true,
+ "type": "text"
},
{
- "name": "c",
- "position": 3,
- "type": "int"
+ "name": "c",
+ "position": 3,
+ "type": "int"
}
]
}
@@ -198,30 +259,30 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "e",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "e",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "f",
- "position": 2,
- "nullable": true,
- "type": "text"
+ "name": "f",
+ "position": 2,
+ "nullable": true,
+ "type": "text"
},
{
- "name": "c",
- "position": 3,
- "nullable": true,
- "type": "int"
+ "name": "c",
+ "position": 3,
+ "nullable": true,
+ "type": "int"
}
]
}
@@ -238,18 +299,18 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t3",
- "columns": [
+ "name": "t3",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
}
]
}
@@ -296,29 +357,29 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
}
]
},
{
- "name": "t2",
- "columns": [
+ "name": "t2",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
}
]
}
@@ -341,78 +402,78 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "default": "1",
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "default": "1",
+ "type": "int"
},
{
- "name": "c",
- "position": 3,
- "nullable": true,
- "type": "int"
+ "name": "c",
+ "position": 3,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "d",
- "position": 4,
- "type": "varchar"
+ "name": "d",
+ "position": 4,
+ "type": "varchar"
},
{
- "name": "e",
- "position": 5,
- "nullable": true,
- "type": "varchar"
+ "name": "e",
+ "position": 5,
+ "nullable": true,
+ "type": "varchar"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "type": "BTREE",
- "unique": true,
- "primary": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
},
{
- "name": "d",
- "expressions": [
+ "name": "d",
+ "expressions": [
"d",
"e"
],
- "type": "FULLTEXT"
+ "type": "FULLTEXT"
},
{
- "name": "idx1",
- "expressions": [
+ "name": "idx1",
+ "expressions": [
"c"
],
- "type": "BTREE",
- "visible": true
+ "type": "BTREE",
+ "visible": true
},
{
- "name": "uk1",
- "expressions": [
+ "name": "uk1",
+ "expressions": [
"b",
"c"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
}
]
}
@@ -466,70 +527,70 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "default": "1",
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "default": "1",
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "nullable": true,
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "c",
- "position": 3,
- "default": "NULL",
- "type": "varchar",
- "comment": "column c"
+ "name": "c",
+ "position": 3,
+ "default": "NULL",
+ "type": "varchar",
+ "comment": "column c"
},
{
- "name": "e",
- "position": 4,
- "nullable": true,
- "type": "int"
+ "name": "e",
+ "position": 4,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "f",
- "position": 5,
- "type": "varchar",
- "collation": "utf8mb4_polish_ci"
+ "name": "f",
+ "position": 5,
+ "type": "varchar",
+ "collation": "utf8mb4_polish_ci"
},
{
- "name": "g",
- "position": 6,
- "type": "varchar",
- "characterSet": "utf8mb4"
+ "name": "g",
+ "position": 6,
+ "type": "varchar",
+ "characterSet": "utf8mb4"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "type": "BTREE",
- "unique": true,
- "primary": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
},
{
- "name": "b",
- "expressions": [
+ "name": "b",
+ "expressions": [
"b"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
}
]
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
index 765292c05f..3d75e393fd 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
@@ -223,6 +223,29 @@ func (l *mysqlV2Listener) EnterAlterTable(ctx *mysql.AlterTableContext) {
return
}
+ for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {
+ for _, op := range option.AllCreateTableOption() {
+ switch {
+ // engine.
+ case op.ENGINE_SYMBOL() != nil:
+ if op.EngineRef() == nil {
+ continue
+ }
+ engine := op.EngineRef().GetText()
+ table.engine = newStringPointer(engine)
+ // table comment.
+ case op.COMMENT_SYMBOL() != nil && op.TextStringLiteral() != nil:
+ comment := mysqlparser.NormalizeMySQLTextStringLiteral(op.TextStringLiteral())
+ table.comment = newStringPointer(comment)
+ // table collation.
+ case op.DefaultCollation() != nil && op.DefaultCollation().CollationName() != nil:
+ collation := mysqlparser.NormalizeMySQLCollationName(op.DefaultCollation().CollationName())
+ table.collation = newStringPointer(collation)
+ default:
+ }
+ }
+ }
+
// alter table add column, change column, modify column.
for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
if item == nil {