Skip to content

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

feat: tidb get design schema support view (#13457)

h3n4l3f47a8bd1ee4
diff --git a/backend/plugin/schema/tidb/get_design_schema.go b/backend/plugin/schema/tidb/get_design_schema.go
index 66f6b0bbd5..fbb65c1d28 100644
--- a/backend/plugin/schema/tidb/get_design_schema.go
+++ b/backend/plugin/schema/tidb/get_design_schema.go
@@ -21,6 +21,9 @@ func GetDesignSchema(_ string, to *storepb.DatabaseSchemaMetadata) (string, erro
if err := writeTables(&sb, to, toState); err != nil {
return "", err
}
+ if err := writeViews(&sb, to, toState); err != nil {
+ return "", err
+ }
s := sb.String()
// Make goyamlv3 happy.
@@ -59,6 +62,40 @@ func writeTables(w io.StringWriter, to *storepb.DatabaseSchemaMetadata, state *d
return nil
}
+func writeViews(w io.StringWriter, to *storepb.DatabaseSchemaMetadata, state *databaseState) error {
+ // Follow the order of the input schemas.
+ for _, schema := range to.Schemas {
+ schemaState, ok := state.schemas[schema.Name]
+ if !ok {
+ continue
+ }
+ // Follow the order of the input views.
+ for _, view := range schema.Views {
+ view, ok := schemaState.views[view.Name]
+ if !ok {
+ continue
+ }
+ if _, err := w.WriteString(getViewAnnouncement(view.name)); err != nil {
+ return err
+ }
+
+ buf := &strings.Builder{}
+ if err := view.toString(buf); err != nil {
+ return err
+ }
+ if _, err := w.WriteString(buf.String()); err != nil {
+ return err
+ }
+ delete(schemaState.views, view.name)
+ }
+ }
+ return nil
+}
+
+func getViewAnnouncement(name string) string {
+ return fmt.Sprintf("\nDROP VIEW IF EXISTS `%s`;\n--\n-- View structure for `%s`\n--\n", name, name)
+}
+
func getTableAnnouncement(name string) string {
return fmt.Sprintf("\n--\n-- Table structure for `%s`\n--\n", name)
}
diff --git a/backend/plugin/schema/tidb/state.go b/backend/plugin/schema/tidb/state.go
index 513c5b932c..17789129f3 100644
--- a/backend/plugin/schema/tidb/state.go
+++ b/backend/plugin/schema/tidb/state.go
@@ -2,6 +2,7 @@ package tidb
import (
"fmt"
+ "io"
"regexp"
"sort"
"strings"
@@ -56,11 +57,13 @@ type schemaState struct {
id int
name string
tables map[string]*tableState
+ views map[string]*viewState
}
func newSchemaState() *schemaState {
return &schemaState{
tables: make(map[string]*tableState),
+ views: make(map[string]*viewState),
}
}
@@ -70,6 +73,9 @@ func convertToSchemaState(schema *storepb.SchemaMetadata) *schemaState {
for i, table := range schema.Tables {
state.tables[table.Name] = convertToTableState(i, table)
}
+ for i, view := range schema.Views {
+ state.views[view.Name] = convertToViewState(i, view)
+ }
return state
}
@@ -81,15 +87,26 @@ func (s *schemaState) convertToSchemaMetadata() *storepb.SchemaMetadata {
sort.Slice(tableStates, func(i, j int) bool {
return tableStates[i].id < tableStates[j].id
})
+ viewStates := []*viewState{}
+ for _, view := range s.views {
+ viewStates = append(viewStates, view)
+ }
+ sort.Slice(viewStates, func(i, j int) bool {
+ return viewStates[i].id < viewStates[j].id
+ })
tables := []*storepb.TableMetadata{}
for _, table := range tableStates {
tables = append(tables, table.convertToTableMetadata())
}
+ views := []*storepb.ViewMetadata{}
+ for _, view := range viewStates {
+ views = append(views, view.convertToViewMetadata())
+ }
return &storepb.SchemaMetadata{
Name: s.name,
Tables: tables,
+ Views: views,
// Unsupported, for tests only.
- Views: []*storepb.ViewMetadata{},
Functions: []*storepb.FunctionMetadata{},
Streams: []*storepb.StreamMetadata{},
Tasks: []*storepb.TaskMetadata{},
@@ -602,3 +619,36 @@ func normalizeOnUpdate(s string) string {
// not a current_timestamp family function
return s
}
+
+type viewState struct {
+ id int
+ name string
+ definition string
+}
+
+func (v *viewState) convertToViewMetadata() *storepb.ViewMetadata {
+ return &storepb.ViewMetadata{
+ Name: v.name,
+ Definition: v.definition,
+ }
+}
+
+func convertToViewState(id int, view *storepb.ViewMetadata) *viewState {
+ return &viewState{
+ id: id,
+ name: view.Name,
+ definition: view.Definition,
+ }
+}
+
+func (v *viewState) toString(buf io.StringWriter) error {
+ stmt := fmt.Sprintf("CREATE VIEW `%s` AS %s", v.name, v.definition)
+ if !strings.HasSuffix(stmt, ";") {
+ stmt += ";"
+ }
+ stmt += "\n"
+ if _, err := buf.WriteString(stmt); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/backend/plugin/schema/tidb/testdata/get_design_schema.yaml b/backend/plugin/schema/tidb/testdata/get_design_schema.yaml
index 5a32526b0f..3509926b02 100644
--- a/backend/plugin/schema/tidb/testdata/get_design_schema.yaml
+++ b/backend/plugin/schema/tidb/testdata/get_design_schema.yaml
@@ -54,6 +54,18 @@
}
]
}
+ ],
+ "views": [
+ {
+ "name": "v1",
+ "comment": "VIEW",
+ "definition": "select `t`.`id` AS `id` from `t`"
+ },
+ {
+ "name": "v2",
+ "comment": "VIEW",
+ "definition": "select (`t`.`id` + 1) AS `id + 1` from `t`"
+ }
]
}
]
@@ -71,6 +83,18 @@
KEY `idx_t_b` (`b`(20)),
KEY `idx_t_b2` (`b`(20))
);
+
+ DROP VIEW IF EXISTS `v1`;
+ --
+ -- View structure for `v1`
+ --
+ CREATE VIEW `v1` AS select `t`.`id` AS `id` from `t`;
+
+ DROP VIEW IF EXISTS `v2`;
+ --
+ -- View structure for `v2`
+ --
+ CREATE VIEW `v2` AS select (`t`.`id` + 1) AS `id + 1` from `t`;
- target: |-
{
"schemas": [