Skip to content

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

chore(structurer): generate continuous create table stmt (#10360)

h3n4la3cca6ffc627
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.go
index b5b0a82437..3c1bc98ed4 100644
--- a/backend/plugin/schema/mysql/get_design_schema.go
+++ b/backend/plugin/schema/mysql/get_design_schema.go
@@ -876,9 +876,69 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi
}
}
+// DropViewStatement is called when production dropViewStatement is entered.
+//
+// mysqldump generate drop view if exists statement after all create table statement.
+// To provide the better ux, we generate the new tables before the drop view statement.
+func (g *mysqlDesignSchemaGenerator) EnterDropView(*mysql.DropViewContext) {
+ if g.err != nil {
+ return
+ }
+
+ if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+}
+
+func (g *mysqlDesignSchemaGenerator) EnterCreateProcedure(*mysql.CreateProcedureContext) {
+ if g.err != nil {
+ return
+ }
+
+ if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+}
+
+func (g *mysqlDesignSchemaGenerator) EnterCreateFunction(*mysql.CreateFunctionContext) {
+ if g.err != nil {
+ return
+ }
+
+ if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+}
+
+func (g *mysqlDesignSchemaGenerator) EnterCreateEvent(*mysql.CreateEventContext) {
+ if g.err != nil {
+ return
+ }
+
+ if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+}
+
+func (g *mysqlDesignSchemaGenerator) EnterCreateTriggers(*mysql.CreateTriggerContext) {
+ if g.err != nil {
+ return
+ }
+
+ if err := writeRemainingTables(&g.result, g.desired, g.to); err != nil {
+ g.err = err
+ return
+ }
+}
+
// EnterSetStatement is called when production setStatement is entered.
//
-// mysqldump generate `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;` statement at the end of the file,
+// mysqldump generates `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;` statement at the end of the file, and
+// generates `SET character_set_client` statement at the beginning of create function statement.
// to provide the better user experience, we generate the remaining tables before the set statement mentioned above.
func (g *mysqlDesignSchemaGenerator) EnterSetStatement(ctx *mysql.SetStatementContext) {
if g.err != nil {
@@ -886,7 +946,7 @@ func (g *mysqlDesignSchemaGenerator) EnterSetStatement(ctx *mysql.SetStatementCo
}
curSet := strings.TrimSpace(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx))
- if curSet != `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS` {
+ if curSet != `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS` && !strings.HasPrefix(curSet, "SET character_set_client") {
return
}
diff --git a/backend/plugin/schema/mysql/get_design_schema_test.go b/backend/plugin/schema/mysql/get_design_schema_test.go
index 4906a13ecf..0c2668f945 100644
--- a/backend/plugin/schema/mysql/get_design_schema_test.go
+++ b/backend/plugin/schema/mysql/get_design_schema_test.go
@@ -15,9 +15,10 @@ import (
func TestGetDesignSchema(t *testing.T) {
type designTest struct {
- Baseline string
- Target string
- Result string
+ Description string
+ Baseline string
+ Target string
+ Result string
}
const (
diff --git a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
index 919fd2b5fa..369d9bb98f 100644
--- a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
+++ b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
@@ -1,4 +1,133 @@
-- baseline: |-
+- description: New create table should not appear in delimiter block
+ baseline: |-
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Table structure for `u`
+ --
+ CREATE TABLE `u` (
+ `t` text NOT NULL,
+ `g` geometry NOT NULL,
+ `j` json NOT NULL,
+ `ti` tinyint(1) NOT NULL,
+ `tb` tinyblob
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ --
+ -- Function structure for `hello`
+ --
+ SET character_set_client = utf8mb3;
+ SET character_set_results = utf8mb3;
+ SET collation_connection = utf8mb3_general_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`localhost` FUNCTION `hello`(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+ DETERMINISTIC
+ RETURN CONCAT ('Hello, ', s, '!') ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+ target: |-
+ {
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "u",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "t",
+ "type": "text",
+ "position": 1
+ },
+ {
+ "name": "g",
+ "type": "geometry",
+ "position": 2
+ },
+ {
+ "name": "j",
+ "type": "json",
+ "position": 3
+ },
+ {
+ "name": "ti",
+ "type": "tinyint(1)",
+ "position": 4
+ },
+ {
+ "name": "tb",
+ "type": "tinyblob",
+ "nullable": true,
+ "position": 5,
+ "defaultNull": true
+ }
+ ],
+ "collation": "utf8mb4_0900_ai_ci"
+ },
+ {
+ "name": "wahaha",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "primary": true,
+ "expressions": [
+ "id"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ result: |
+ SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
+ SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
+ --
+ -- Table structure for `u`
+ --
+ CREATE TABLE `u` (
+ `t` text NOT NULL,
+ `g` geometry NOT NULL,
+ `j` json NOT NULL,
+ `ti` tinyint(1) NOT NULL,
+ `tb` tinyblob
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ --
+ -- Table structure for table `wahaha`
+ --
+ CREATE TABLE `wahaha` (
+ `id` int NOT NULL,
+ PRIMARY KEY (`id`)
+ );
+
+ --
+ -- Function structure for `hello`
+ --
+ SET character_set_client = utf8mb3;
+ SET character_set_results = utf8mb3;
+ SET collation_connection = utf8mb3_general_ci;
+ SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
+ DELIMITER ;;
+ CREATE DEFINER=`root`@`localhost` FUNCTION `hello`(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4
+ DETERMINISTIC
+ RETURN CONCAT ('Hello, ', s, '!') ;;
+ DELIMITER ;
+
+ SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
+ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
+- description: ""
+ baseline: |-
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
--
@@ -101,7 +230,8 @@
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
-- baseline: |-
+- description: ""
+ baseline: |-
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
--
@@ -404,7 +534,8 @@
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
-- baseline: |-
+- description: ""
+ baseline: |-
CREATE TABLE `t` (
`a` int
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
@@ -431,7 +562,8 @@
CREATE TABLE `t` (
`a` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT 'this is comment';
-- baseline: |-
+- description: ""
+ baseline: |-
CREATE TABLE `t1` (
`a` int primary key
);