Skip to content

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

chore(mysqlv2): support statements abount database (#9479)

zll600083e12f3d73a
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
index 0ca26744aa..194aa2e96e 100644
--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
@@ -1,3 +1,36 @@
+- statement: CREATE DATABASE another;
+ ignore_case_sensitive: false
+ want: ""
+ err:
+ type: 201
+ content: Database `another` is not the current database `test`
+ line: 0
+ payload: null
+- statement: |-
+ -- this is comment
+ DROP DATABASE test;
+ CREATE TABLE t(a int);
+ ignore_case_sensitive: false
+ want: ""
+ err:
+ type: 202
+ content: Database `test` is deleted
+ line: 1
+ payload: null
+- statement: |-
+ ALTER DATABASE CHARACTER SET = utf8mb4;
+ ALTER DATABASE test COLLATE utf8mb4_polish_ci;
+ ignore_case_sensitive: false
+ want: |-
+ {
+ "name": "test",
+ "schemas": [
+ {}
+ ],
+ "characterSet": "utf8mb4",
+ "collation": "utf8mb4_polish_ci"
+ }
+ err: null
- statement: |-
CREATE TABLE t1(
a INT PRIMARY KEY,
@@ -49,67 +82,67 @@
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": "b",
- "position": 2,
- "nullable": true,
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "c",
- "position": 3,
- "type": "varchar"
+ "name": "c",
+ "position": 3,
+ "type": "varchar"
},
{
- "name": "d",
- "position": 4,
- "type": "geometry"
+ "name": "d",
+ "position": 4,
+ "type": "geometry"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "ftk1",
- "expressions": [
+ "name": "ftk1",
+ "expressions": [
"c"
],
- "type": "FULLTEXT",
- "visible": true
+ "type": "FULLTEXT",
+ "visible": true
},
{
- "name": "idx1",
- "expressions": [
+ "name": "idx1",
+ "expressions": [
"a"
],
- "type": "BTREE"
+ "type": "BTREE"
},
{
- "name": "sk1",
- "expressions": [
+ "name": "sk1",
+ "expressions": [
"d"
],
- "type": "SPATIAL",
- "visible": true
+ "type": "SPATIAL",
+ "visible": true
},
{
- "name": "uk1",
- "expressions": [
+ "name": "uk1",
+ "expressions": [
"b"
],
- "type": "BTREE",
- "visible": true
+ "type": "BTREE",
+ "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 37fa5d84fd..38795503d8 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
@@ -442,6 +442,56 @@ func (l *mysqlV2Listener) EnterCreateIndex(ctx *mysql.CreateIndexContext) {
}
}
+// EnterAlterDatabase is called when production alterDatabase is entered.
+func (l *mysqlV2Listener) EnterAlterDatabase(ctx *mysql.AlterDatabaseContext) {
+ if ctx.SchemaRef() != nil {
+ databaseName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())
+ if !l.databaseState.isCurrentDatabase(databaseName) {
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)
+ return
+ }
+ }
+
+ for _, option := range ctx.AllAlterDatabaseOption() {
+ if option.CreateDatabaseOption() == nil {
+ continue
+ }
+
+ switch {
+ case option.CreateDatabaseOption().DefaultCharset() != nil && option.CreateDatabaseOption().DefaultCharset().CharsetName() != nil:
+ charset := mysqlparser.NormalizeMySQLCharsetName(option.CreateDatabaseOption().DefaultCharset().CharsetName())
+ l.databaseState.characterSet = charset
+ case option.CreateDatabaseOption().DefaultCollation() != nil && option.CreateDatabaseOption().DefaultCollation().CollationName() != nil:
+ collation := mysqlparser.NormalizeMySQLCollationName(option.CreateDatabaseOption().DefaultCollation().CollationName())
+ l.databaseState.collation = collation
+ }
+ }
+}
+
+// EnterDropDatabase is called when production dropDatabase is entered.
+func (l *mysqlV2Listener) EnterDropDatabase(ctx *mysql.DropDatabaseContext) {
+ if ctx.SchemaRef() == nil {
+ return
+ }
+
+ databaseName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())
+ if !l.databaseState.isCurrentDatabase(databaseName) {
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)
+ return
+ }
+
+ l.databaseState.deleted = true
+}
+
+// EnterCreateDatabase is called when production createDatabase is entered.
+func (l *mysqlV2Listener) EnterCreateDatabase(ctx *mysql.CreateDatabaseContext) {
+ if ctx.SchemaName() == nil {
+ return
+ }
+ databaseName := mysqlparser.NormalizeMySQLSchemaName(ctx.SchemaName())
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)
+}
+
func (t *TableState) mysqlV2ChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {
index, exists := t.indexSet[indexName]
if !exists {
diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.go
index b26de2d31b..e5b44cd5c6 100644
--- a/backend/plugin/parser/mysql/normalize.go
+++ b/backend/plugin/parser/mysql/normalize.go
@@ -175,7 +175,7 @@ func NormalizeMySQLProcedureName(ctx parser.IProcedureNameContext) (string, stri
return "", ""
}
-// NormalizeMySQLSchemaRef noamalizes the given schemaRef.
+// NormalizeMySQLSchemaRef normalize the given schemaRef.
func NormalizeMySQLSchemaRef(ctx parser.ISchemaRefContext) string {
if ctx.Identifier() != nil {
return NormalizeMySQLIdentifier(ctx.Identifier())
@@ -183,6 +183,14 @@ func NormalizeMySQLSchemaRef(ctx parser.ISchemaRefContext) string {
return ""
}
+// NormalizeMySQLSchemaRef normalize the given schemaName.
+func NormalizeMySQLSchemaName(ctx parser.ISchemaNameContext) string {
+ if ctx.Identifier() != nil {
+ return NormalizeMySQLIdentifier(ctx.Identifier())
+ }
+ return ""
+}
+
// NormalizeMySQLKeyListVariants normalize the given keyListVariants.
func NormalizeKeyListVariants(ctx parser.IKeyListVariantsContext) []string {
if ctx.KeyList() != nil {