Skip to content

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

feat: implement GetSchemaString for MySQL (#15507)

rebelice63371e3a6c9c
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 677041ad5b..2121e5c39f 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -1287,8 +1287,19 @@ func (s *DatabaseService) GetSchemaString(ctx context.Context, request *v1pb.Get
}
return &v1pb.GetSchemaStringResponse{SchemaString: s}, nil
case v1pb.GetSchemaStringRequest_PROCEDURE:
- // TODO: implement.
- return nil, status.Errorf(codes.Unimplemented, "PROCEDURE is not supported yet")
+ schemaMetadata := dbSchema.GetDatabaseMetadata().GetSchema(request.Schema)
+ if schemaMetadata == nil {
+ return nil, status.Errorf(codes.NotFound, "schema %q not found", request.Schema)
+ }
+ procedureMetadata := schemaMetadata.GetProcedure(request.Object)
+ if procedureMetadata == nil {
+ return nil, status.Errorf(codes.NotFound, "procedure %q not found", request.Object)
+ }
+ s, err := schema.GetProcedureDefinition(instance.Metadata.Engine, request.Schema, procedureMetadata.GetProto())
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "Failed to get procedure schema: %v", err)
+ }
+ return &v1pb.GetSchemaStringResponse{SchemaString: s}, nil
case v1pb.GetSchemaStringRequest_SEQUENCE:
schemaMetadata := dbSchema.GetDatabaseMetadata().GetSchema(request.Schema)
if schemaMetadata == nil {
diff --git a/backend/plugin/schema/mysql/get_database_definition.go b/backend/plugin/schema/mysql/get_database_definition.go
index 7db2900e63..46a7a597b8 100644
--- a/backend/plugin/schema/mysql/get_database_definition.go
+++ b/backend/plugin/schema/mysql/get_database_definition.go
@@ -1,9 +1,13 @@
package mysql
import (
+ "fmt"
"io"
+ "strconv"
"strings"
+ "github.com/pkg/errors"
+
"github.com/bytebase/bytebase/backend/plugin/schema"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -31,6 +35,10 @@ const (
func init() {
schema.RegisterGetDatabaseDefinition(storepb.Engine_MYSQL, GetDatabaseDefinition)
+ schema.RegisterGetTableDefinition(storepb.Engine_MYSQL, GetTableDefinition)
+ schema.RegisterGetViewDefinition(storepb.Engine_MYSQL, GetViewDefinition)
+ schema.RegisterGetFunctionDefinition(storepb.Engine_MYSQL, GetFunctionDefinition)
+ schema.RegisterGetProcedureDefinition(storepb.Engine_MYSQL, GetProcedureDefinition)
}
func GetDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *storepb.DatabaseSchemaMetadata) (string, error) {
@@ -123,6 +131,38 @@ func GetDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *storepb.Da
return buf.String(), nil
}
+func GetTableDefinition(_ string, table *storepb.TableMetadata, _ []*storepb.SequenceMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeTable(&buf, table); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func GetViewDefinition(_ string, view *storepb.ViewMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeView(&buf, view); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func GetFunctionDefinition(_ string, function *storepb.FunctionMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeFunction(&buf, function); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func GetProcedureDefinition(_ string, procedure *storepb.ProcedureMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeProcedure(&buf, procedure); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
func writeEvent(out io.Writer, event *storepb.EventMetadata) error {
// Header.
if _, err := io.WriteString(out, emptyCommentLine); err != nil {
@@ -520,7 +560,7 @@ func writeView(out io.Writer, view *storepb.ViewMetadata) error {
return err
}
-func writeTable(out io.Writer, table *storepb.TableMetadata) error {
+func writeTable(out *strings.Builder, table *storepb.TableMetadata) error {
// Header.
if _, err := io.WriteString(out, emptyCommentLine); err != nil {
return err
@@ -542,17 +582,572 @@ func writeTable(out io.Writer, table *storepb.TableMetadata) error {
}
// Definition.
- definition, err := schema.StringifyTable(storepb.Engine_MYSQL, table)
- if err != nil {
+ if _, err := fmt.Fprintf(out, "CREATE TABLE `%s` (\n", table.Name); err != nil {
return err
}
- if _, err := io.WriteString(out, definition); err != nil {
+
+ for i, column := range table.Columns {
+ if i != 0 {
+ if _, err := fmt.Fprintf(out, ",\n"); err != nil {
+ return err
+ }
+ }
+ if err := printColumnClause(out, column); err != nil {
+ return err
+ }
+ }
+
+ if err := printPrimaryKeyClause(out, table.Indexes); err != nil {
+ return err
+ }
+
+ for _, index := range table.Indexes {
+ if index.Primary {
+ continue
+ }
+ if err := printIndexClause(out, index); err != nil {
+ return err
+ }
+ }
+
+ for _, fk := range table.ForeignKeys {
+ if err := printForeignKeyClause(out, fk); err != nil {
+ return err
+ }
+ }
+
+ for _, check := range table.CheckConstraints {
+ if err := printCheckClause(out, check); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(out, "\n) ENGINE=%s", table.Engine); err != nil {
return err
}
- _, err = io.WriteString(out, "\n")
+
+ if table.Charset != "" {
+ if _, err := fmt.Fprintf(out, " DEFAULT CHARSET=%s", table.Charset); err != nil {
+ return err
+ }
+ }
+
+ if table.Collation != "" {
+ if _, err := fmt.Fprintf(out, " COLLATE=%s", table.Collation); err != nil {
+ return err
+ }
+ }
+
+ if table.Comment != "" {
+ if _, err := fmt.Fprintf(out, " COMMENT='%s'", table.Comment); err != nil {
+ return err
+ }
+ }
+
+ if len(table.Partitions) > 0 {
+ if err := printPartitionClause(out, table.Partitions); err != nil {
+ return err
+ }
+ }
+
+ _, err := io.WriteString(out, ";\n\n")
return err
}
+// Copy the logic from backend/plugin/schema/mysql/state.go.
+func printPartitionClause(buf *strings.Builder, partitions []*storepb.TablePartitionMetadata) error {
+ if len(partitions) == 0 {
+ return nil
+ }
+ vsc := getVersionSpecificComment(partitions)
+ curComment := vsc
+ if _, err := fmt.Fprintf(buf, "%s PARTITION BY ", curComment); err != nil {
+ return err
+ }
+ switch partitions[0].Type {
+ case storepb.TablePartitionMetadata_RANGE:
+ if _, err := fmt.Fprintf(buf, "RANGE (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_RANGE_COLUMNS:
+ if _, err := fmt.Fprintf(buf, "RANGE COLUMNS (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_LIST:
+ if _, err := fmt.Fprintf(buf, "LIST (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_LIST_COLUMNS:
+ if _, err := fmt.Fprintf(buf, "LIST COLUMNS (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_HASH:
+ if _, err := fmt.Fprintf(buf, "HASH (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_KEY:
+ if _, err := fmt.Fprintf(buf, "KEY (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_LINEAR_HASH:
+ if _, err := fmt.Fprintf(buf, "LINEAR HASH (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_LINEAR_KEY:
+ if _, err := fmt.Fprintf(buf, "LINEAR KEY (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("unknown partition type: %v", partitions[0].Type)
+ }
+
+ useDefault := int64(0)
+ if partitions[0].UseDefault != "" {
+ var err error
+ useDefault, err = strconv.ParseInt(partitions[0].UseDefault, 10, 64)
+ if err != nil {
+ return err
+ }
+ }
+ if useDefault != 0 {
+ if _, err := fmt.Fprintf(buf, "\nPARTITIONS %d", useDefault); err != nil {
+ return err
+ }
+ }
+
+ if len(partitions[0].Subpartitions) > 0 {
+ if _, err := fmt.Fprintf(buf, "\nSUBPARTITION BY "); err != nil {
+ return err
+ }
+ switch partitions[0].Subpartitions[0].Type {
+ case storepb.TablePartitionMetadata_HASH:
+ if _, err := fmt.Fprintf(buf, "HASH (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_LINEAR_HASH:
+ if _, err := fmt.Fprintf(buf, "LINEAR HASH (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_KEY:
+ if _, err := fmt.Fprintf(buf, "KEY (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ case storepb.TablePartitionMetadata_LINEAR_KEY:
+ if _, err := fmt.Fprintf(buf, "LINEAR KEY (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("invalid subpartition type: %v", partitions[0].Subpartitions[0].Type)
+ }
+ }
+
+ subUseDefault := 0
+ if len(partitions[0].Subpartitions) > 0 && partitions[0].Subpartitions[0].UseDefault != "" {
+ var err error
+ subUseDefault, err = strconv.Atoi(partitions[0].Subpartitions[0].UseDefault)
+ if err != nil {
+ return err
+ }
+ }
+
+ if subUseDefault != 0 {
+ if _, err := fmt.Fprintf(buf, "\nSUBPARTITIONS %d", subUseDefault); err != nil {
+ return err
+ }
+ }
+
+ if useDefault == 0 {
+ if _, err := fmt.Fprintf(buf, "\n("); err != nil {
+ return err
+ }
+ preposition, err := getPrepositionByType(partitions[0].Type)
+ if err != nil {
+ return err
+ }
+ for i, partition := range partitions {
+ if i != 0 {
+ if _, err := fmt.Fprintf(buf, ",\n "); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, "PARTITION %s", partition.Name); err != nil {
+ return err
+ }
+ if preposition != "" {
+ if partition.Value != "MAXVALUE" {
+ if _, err := fmt.Fprintf(buf, " VALUES %s (%s)", preposition, partition.Value); err != nil {
+ return err
+ }
+ } else {
+ if _, err := fmt.Fprintf(buf, " VALUES %s %s", preposition, partition.Value); err != nil {
+ return err
+ }
+ }
+ }
+
+ if subUseDefault == 0 && len(partition.Subpartitions) > 0 {
+ if _, err := fmt.Fprintf(buf, "\n ("); err != nil {
+ return err
+ }
+ for j, subPartition := range partition.Subpartitions {
+ if _, err := fmt.Fprintf(buf, "SUBPARTITION %s", subPartition.Name); err != nil {
+ return err
+ }
+ if err := writePartitionOptions(buf); err != nil {
+ return err
+ }
+ if j == len(partition.Subpartitions)-1 {
+ if _, err := fmt.Fprintf(buf, ")"); err != nil {
+ return err
+ }
+ } else {
+ if _, err := fmt.Fprintf(buf, ",\n "); err != nil {
+ return err
+ }
+ }
+ }
+ } else {
+ if err := writePartitionOptions(buf); err != nil {
+ return err
+ }
+ }
+
+ if i == len(partitions)-1 {
+ if _, err := fmt.Fprintf(buf, ")"); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, " */"); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func getVersionSpecificComment(partitions []*storepb.TablePartitionMetadata) string {
+ if len(partitions) == 0 {
+ return ""
+ }
+ partition := partitions[0]
+ if partition.Type == storepb.TablePartitionMetadata_RANGE_COLUMNS || partition.Type == storepb.TablePartitionMetadata_LIST_COLUMNS {
+ // MySQL introduce columns partitioning in 5.5+
+ return "\n/*!50500"
+ }
+ return "\n/*!50100"
+}
+
+func printCheckClause(buf *strings.Builder, check *storepb.CheckConstraintMetadata) error {
+ if _, err := fmt.Fprintf(buf, ",\n CONSTRAINT `%s` CHECK %s", check.Name, check.Expression); err != nil {
+ return err
+ }
+ return nil
+}
+
+func printForeignKeyClause(buf *strings.Builder, fk *storepb.ForeignKeyMetadata) error {
+ if _, err := fmt.Fprintf(buf, ",\n CONSTRAINT `%s` FOREIGN KEY (", fk.Name); err != nil {
+ return err
+ }
+
+ for i, column := range fk.Columns {
+ if i != 0 {
+ if _, err := fmt.Fprintf(buf, ", "); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, "`%s`", column); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, ") REFERENCES `%s` (", fk.ReferencedTable); err != nil {
+ return err
+ }
+
+ for i, column := range fk.ReferencedColumns {
+ if i != 0 {
+ if _, err := fmt.Fprintf(buf, ", "); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, "`%s`", column); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, ")"); err != nil {
+ return err
+ }
+
+ if fk.OnDelete != "" && !strings.EqualFold(fk.OnDelete, mysqlNoAction) {
+ if _, err := fmt.Fprintf(buf, " ON DELETE %s", fk.OnDelete); err != nil {
+ return err
+ }
+ }
+
+ if fk.OnUpdate != "" && !strings.EqualFold(fk.OnUpdate, mysqlNoAction) {
+ if _, err := fmt.Fprintf(buf, " ON UPDATE %s", fk.OnUpdate); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func printIndexClause(buf *strings.Builder, index *storepb.IndexMetadata) error {
+ if index.Primary {
+ return nil
+ }
+
+ if _, err := fmt.Fprintf(buf, ",\n "); err != nil {
+ return err
+ }
+
+ if index.Unique {
+ if _, err := fmt.Fprintf(buf, "UNIQUE "); err != nil {
+ return err
+ }
+ } else if strings.EqualFold(index.Type, mysqlIndexFullText) {
+ if _, err := fmt.Fprintf(buf, "FULLTEXT "); err != nil {
+ return err
+ }
+ } else if strings.EqualFold(index.Type, mysqlIndexSpatial) {
+ if _, err := fmt.Fprintf(buf, "SPATIAL "); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, "KEY `%s` (", index.Name); err != nil {
+ return err
+ }
+
+ for i, expr := range index.Expressions {
+ if i != 0 {
+ if _, err := fmt.Fprintf(buf, ", "); err != nil {
+ return err
+ }
+ }
+ keyLength := int64(-1)
+ descending := false
+ if len(index.KeyLength) > i {
+ keyLength = index.KeyLength[i]
+ }
+ if len(index.Descending) > i {
+ descending = index.Descending[i]
+ }
+ if err := printIndexKeyPart(buf, expr, keyLength, descending); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, ")"); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func printIndexKeyPart(buf *strings.Builder, expr string, length int64, descending bool) error {
+ if len(expr) == 0 {
+ return errors.New("index key part expression is empty")
+ }
+ if expr[0] == '(' && expr[len(expr)-1] == ')' {
+ if _, err := buf.WriteString(expr); err != nil {
+ return err
+ }
+ } else {
+ if _, err := buf.WriteString("`"); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(expr); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString("`"); err != nil {
+ return err
+ }
+ }
+ if length > 0 {
+ if _, err := buf.WriteString("("); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(strconv.FormatInt(length, 10)); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(")"); err != nil {
+ return err
+ }
+ }
+ if descending {
+ if _, err := buf.WriteString(" DESC"); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func printPrimaryKeyClause(buf *strings.Builder, indexes []*storepb.IndexMetadata) error {
+ for _, index := range indexes {
+ if index.Primary {
+ if _, err := fmt.Fprintf(buf, ",\n PRIMARY KEY ("); err != nil {
+ return err
+ }
+ for i, column := range index.Expressions {
+ if i != 0 {
+ if _, err := fmt.Fprintf(buf, ", "); err != nil {
+ return err
+ }
+ }
+ keyLength := int64(-1)
+ descending := false
+ if len(index.KeyLength) > i {
+ keyLength = index.KeyLength[i]
+ }
+ if len(index.Descending) > i {
+ descending = index.Descending[i]
+ }
+ if err := printIndexKeyPart(buf, column, keyLength, descending); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, ")"); err != nil {
+ return err
+ }
+ return nil
+ }
+ }
+
+ return nil
+}
+
+func isAutoIncrement(column *storepb.ColumnMetadata) bool {
+ return strings.EqualFold(column.GetDefaultExpression(), autoIncrementSymbol)
+}
+
+func printColumnClause(buf *strings.Builder, column *storepb.ColumnMetadata) error {
+ if _, err := fmt.Fprintf(buf, " `%s` %s", column.Name, column.Type); err != nil {
+ return err
+ }
+
+ if column.CharacterSet != "" {
+ if _, err := fmt.Fprintf(buf, " CHARACTER SET %s", column.CharacterSet); err != nil {
+ return err
+ }
+ }
+
+ if column.Collation != "" {
+ if _, err := fmt.Fprintf(buf, " COLLATE %s", column.Collation); err != nil {
+ return err
+ }
+ }
+
+ if column.Generation != nil && column.Generation.Expression != "" {
+ if _, err := fmt.Fprintf(buf, " GENERATED ALWAYS AS (%s) ", column.Generation.Expression); err != nil {
+ return err
+ }
+ switch column.Generation.Type {
+ case storepb.GenerationMetadata_TYPE_STORED:
+ if _, err := fmt.Fprintf(buf, "STORED"); err != nil {
+ return err
+ }
+ case storepb.GenerationMetadata_TYPE_VIRTUAL:
+ if _, err := fmt.Fprintf(buf, "VIRTUAL"); err != nil {
+ return err
+ }
+ }
+ }
+
+ if column.Nullable {
+ if _, err := fmt.Fprintf(buf, " NULL"); err != nil {
+ return err
+ }
+ } else {
+ if _, err := fmt.Fprintf(buf, " NOT NULL"); err != nil {
+ return err
+ }
+ }
+
+ if err := printDefaultClause(buf, column); err != nil {
+ return err
+ }
+
+ // Handle auto_increment.
+ if isAutoIncrement(column) {
+ if _, err := fmt.Fprintf(buf, " %s", autoIncrementSymbol); err != nil {
+ return err
+ }
+ }
+
+ if column.OnUpdate != "" {
+ if _, err := buf.WriteString(fmt.Sprintf(" ON UPDATE %s", column.OnUpdate)); err != nil {
+ return err
+ }
+ }
+ if column.Comment != "" {
+ if _, err := fmt.Fprintf(buf, " COMMENT '%s'", column.Comment); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func printDefaultClause(buf *strings.Builder, column *storepb.ColumnMetadata) error {
+ if column.DefaultValue == nil {
+ return nil
+ }
+
+ if column.GetDefaultNull() {
+ if !column.Nullable || !typeSupportsDefaultValue(column.Type) {
+ // If the column is not nullable, then the default value should not be null.
+ // For this case, we should not print the default clause.
+ return nil
+ }
+ if column.Generation != nil && column.Generation.Expression != "" {
+ return nil
+ }
+ if _, err := fmt.Fprintf(buf, " DEFAULT NULL"); err != nil {
+ return err
+ }
+ return nil
+ }
+
+ if column.GetDefaultExpression() != "" {
+ if isAutoIncrement(column) {
+ // If the default value is auto_increment, then we should not print the default clause.
+ // We'll handle this in the following AUTO_INCREMENT clause.
+ return nil
+ }
+ if _, err := fmt.Fprintf(buf, " DEFAULT %s", column.GetDefaultExpression()); err != nil {
+ return err
+ }
+ return nil
+ }
+
+ if column.GetDefault() != nil {
+ if _, err := fmt.Fprintf(buf, " DEFAULT '%s'", column.GetDefault().GetValue()); err != nil {
+ return err
+ }
+ }
+
+ if column.OnUpdate != "" {
+ if _, err := fmt.Fprintf(buf, " ON UPDATE %s", column.OnUpdate); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func typeSupportsDefaultValue(tp string) bool {
+ switch strings.ToLower(tp) {
+ case mysqlTypeBlob, mysqlTypeTinyBob, mysqlTypeMediumBlob, mysqlTypeLongBlob, mysqlTypeJSON, mysqlTypeGeometry:
+ return false
+ default:
+ return true
+ }
+}
+
func writeInvalidTemporaryView(out io.Writer, view *storepb.ViewMetadata) error {
// Header.
if _, err := io.WriteString(out, emptyCommentLine); err != nil {
diff --git a/backend/plugin/schema/mysql/stringify_table.go b/backend/plugin/schema/mysql/stringify_table.go
index 6fd55f67e7..e0281ea028 100644
--- a/backend/plugin/schema/mysql/stringify_table.go
+++ b/backend/plugin/schema/mysql/stringify_table.go
@@ -2,11 +2,8 @@ package mysql
import (
"fmt"
- "strconv"
"strings"
- "github.com/pkg/errors"
-
"github.com/bytebase/bytebase/backend/plugin/schema"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -106,497 +103,3 @@ func StringifyTable(metadata *storepb.TableMetadata) (string, error) {
return buf.String(), nil
}
-
-// Copy the logic from backend/plugin/schema/mysql/state.go.
-func printPartitionClause(buf *strings.Builder, partitions []*storepb.TablePartitionMetadata) error {
- if len(partitions) == 0 {
- return nil
- }
- vsc := getVersionSpecificComment(partitions)
- curComment := vsc
- if _, err := fmt.Fprintf(buf, "%s PARTITION BY ", curComment); err != nil {
- return err
- }
- switch partitions[0].Type {
- case storepb.TablePartitionMetadata_RANGE:
- if _, err := fmt.Fprintf(buf, "RANGE (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_RANGE_COLUMNS:
- if _, err := fmt.Fprintf(buf, "RANGE COLUMNS (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_LIST:
- if _, err := fmt.Fprintf(buf, "LIST (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_LIST_COLUMNS:
- if _, err := fmt.Fprintf(buf, "LIST COLUMNS (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_HASH:
- if _, err := fmt.Fprintf(buf, "HASH (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_KEY:
- if _, err := fmt.Fprintf(buf, "KEY (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_LINEAR_HASH:
- if _, err := fmt.Fprintf(buf, "LINEAR HASH (%s)", partitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_LINEAR_KEY:
- if _, err := fmt.Fprintf(buf, "LINEAR KEY (%s)", partitions[0].Expression); err != nil {
- return err
- }
- default:
- return errors.Errorf("unknown partition type: %v", partitions[0].Type)
- }
-
- useDefault := int64(0)
- if partitions[0].UseDefault != "" {
- var err error
- useDefault, err = strconv.ParseInt(partitions[0].UseDefault, 10, 64)
- if err != nil {
- return err
- }
- }
- if useDefault != 0 {
- if _, err := fmt.Fprintf(buf, "\nPARTITIONS %d", useDefault); err != nil {
- return err
- }
- }
-
- if len(partitions[0].Subpartitions) > 0 {
- if _, err := fmt.Fprintf(buf, "\nSUBPARTITION BY "); err != nil {
- return err
- }
- switch partitions[0].Subpartitions[0].Type {
- case storepb.TablePartitionMetadata_HASH:
- if _, err := fmt.Fprintf(buf, "HASH (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_LINEAR_HASH:
- if _, err := fmt.Fprintf(buf, "LINEAR HASH (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_KEY:
- if _, err := fmt.Fprintf(buf, "KEY (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
- return err
- }
- case storepb.TablePartitionMetadata_LINEAR_KEY:
- if _, err := fmt.Fprintf(buf, "LINEAR KEY (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
- return err
- }
- default:
- return errors.Errorf("invalid subpartition type: %v", partitions[0].Subpartitions[0].Type)
- }
- }
-
- subUseDefault := 0
- if len(partitions[0].Subpartitions) > 0 && partitions[0].Subpartitions[0].UseDefault != "" {
- var err error
- subUseDefault, err = strconv.Atoi(partitions[0].Subpartitions[0].UseDefault)
- if err != nil {
- return err
- }
- }
-
- if subUseDefault != 0 {
- if _, err := fmt.Fprintf(buf, "\nSUBPARTITIONS %d", subUseDefault); err != nil {
- return err
- }
- }
-
- if useDefault == 0 {
- if _, err := fmt.Fprintf(buf, "\n("); err != nil {
- return err
- }
- preposition, err := getPrepositionByType(partitions[0].Type)
- if err != nil {
- return err
- }
- for i, partition := range partitions {
- if i != 0 {
- if _, err := fmt.Fprintf(buf, ",\n "); err != nil {
- return err
- }
- }
- if _, err := fmt.Fprintf(buf, "PARTITION %s", partition.Name); err != nil {
- return err
- }
- if preposition != "" {
- if partition.Value != "MAXVALUE" {
- if _, err := fmt.Fprintf(buf, " VALUES %s (%s)", preposition, partition.Value); err != nil {
- return err
- }
- } else {
- if _, err := fmt.Fprintf(buf, " VALUES %s %s", preposition, partition.Value); err != nil {
- return err
- }
- }
- }
-
- if subUseDefault == 0 && len(partition.Subpartitions) > 0 {
- if _, err := fmt.Fprintf(buf, "\n ("); err != nil {
- return err
- }
- for j, subPartition := range partition.Subpartitions {
- if _, err := fmt.Fprintf(buf, "SUBPARTITION %s", subPartition.Name); err != nil {
- return err
- }
- if err := writePartitionOptions(buf); err != nil {
- return err
- }
- if j == len(partition.Subpartitions)-1 {
- if _, err := fmt.Fprintf(buf, ")"); err != nil {
- return err
- }
- } else {
- if _, err := fmt.Fprintf(buf, ",\n "); err != nil {
- return err
- }
- }
- }
- } else {
- if err := writePartitionOptions(buf); err != nil {
- return err
- }
- }
-
- if i == len(partitions)-1 {
- if _, err := fmt.Fprintf(buf, ")"); err != nil {
- return err
- }
- }
- }
- }
-
- if _, err := fmt.Fprintf(buf, " */"); err != nil {
- return err
- }
-
- return nil
-}
-
-func getVersionSpecificComment(partitions []*storepb.TablePartitionMetadata) string {
- if len(partitions) == 0 {
- return ""
- }
- partition := partitions[0]
- if partition.Type == storepb.TablePartitionMetadata_RANGE_COLUMNS || partition.Type == storepb.TablePartitionMetadata_LIST_COLUMNS {
- // MySQL introduce columns partitioning in 5.5+
- return "\n/*!50500"
- }
- return "\n/*!50100"
-}
-
-func printCheckClause(buf *strings.Builder, check *storepb.CheckConstraintMetadata) error {
- if _, err := fmt.Fprintf(buf, ",\n CONSTRAINT `%s` CHECK %s", check.Name, check.Expression); err != nil {
- return err
- }
- return nil
-}
-
-func printForeignKeyClause(buf *strings.Builder, fk *storepb.ForeignKeyMetadata) error {
- if _, err := fmt.Fprintf(buf, ",\n CONSTRAINT `%s` FOREIGN KEY (", fk.Name); err != nil {
- return err
- }
-
- for i, column := range fk.Columns {
- if i != 0 {
- if _, err := fmt.Fprintf(buf, ", "); err != nil {
- return err
- }
- }
- if _, err := fmt.Fprintf(buf, "`%s`", column); err != nil {
- return err
- }
- }
-
- if _, err := fmt.Fprintf(buf, ") REFERENCES `%s` (", fk.ReferencedTable); err != nil {
- return err
- }
-
- for i, column := range fk.ReferencedColumns {
- if i != 0 {
- if _, err := fmt.Fprintf(buf, ", "); err != nil {
- return err
- }
- }
- if _, err := fmt.Fprintf(buf, "`%s`", column); err != nil {
- return err
- }
- }
-
- if _, err := fmt.Fprintf(buf, ")"); err != nil {
- return err
- }
-
- if fk.OnDelete != "" && !strings.EqualFold(fk.OnDelete, mysqlNoAction) {
- if _, err := fmt.Fprintf(buf, " ON DELETE %s", fk.OnDelete); err != nil {
- return err
- }
- }
-
- if fk.OnUpdate != "" && !strings.EqualFold(fk.OnUpdate, mysqlNoAction) {
- if _, err := fmt.Fprintf(buf, " ON UPDATE %s", fk.OnUpdate); err != nil {
- return err
- }
- }
-
- return nil
-}
-
-func printIndexClause(buf *strings.Builder, index *storepb.IndexMetadata) error {
- if index.Primary {
- return nil
- }
-
- if _, err := fmt.Fprintf(buf, ",\n "); err != nil {
- return err
- }
-
- if index.Unique {
- if _, err := fmt.Fprintf(buf, "UNIQUE "); err != nil {
- return err
- }
- } else if strings.EqualFold(index.Type, mysqlIndexFullText) {
- if _, err := fmt.Fprintf(buf, "FULLTEXT "); err != nil {
- return err
- }
- } else if strings.EqualFold(index.Type, mysqlIndexSpatial) {
- if _, err := fmt.Fprintf(buf, "SPATIAL "); err != nil {
- return err
- }
- }
-
- if _, err := fmt.Fprintf(buf, "KEY `%s` (", index.Name); err != nil {
- return err
- }
-
- for i, expr := range index.Expressions {
- if i != 0 {
- if _, err := fmt.Fprintf(buf, ", "); err != nil {
- return err
- }
- }
- keyLength := int64(-1)
- descending := false
- if len(index.KeyLength) > i {
- keyLength = index.KeyLength[i]
- }
- if len(index.Descending) > i {
- descending = index.Descending[i]
- }
- if err := printIndexKeyPart(buf, expr, keyLength, descending); err != nil {
- return err
- }
- }
-
- if _, err := fmt.Fprintf(buf, ")"); err != nil {
- return err
- }
-
- return nil
-}
-
-func printIndexKeyPart(buf *strings.Builder, expr string, length int64, descending bool) error {
- if len(expr) == 0 {
- return errors.New("index key part expression is empty")
- }
- if expr[0] == '(' && expr[len(expr)-1] == ')' {
- if _, err := buf.WriteString(expr); err != nil {
- return err
- }
- } else {
- if _, err := buf.WriteString("`"); err != nil {
- return err
- }
- if _, err := buf.WriteString(expr); err != nil {
- return err
- }
- if _, err := buf.WriteString("`"); err != nil {
- return err
- }
- }
- if length > 0 {
- if _, err := buf.WriteString("("); err != nil {
- return err
- }
- if _, err := buf.WriteString(strconv.FormatInt(length, 10)); err != nil {
- return err
- }
- if _, err := buf.WriteString(")"); err != nil {
- return err
- }
- }
- if descending {
- if _, err := buf.WriteString(" DESC"); err != nil {
- return err
- }
- }
- return nil
-}
-
-func printPrimaryKeyClause(buf *strings.Builder, indexes []*storepb.IndexMetadata) error {
- for _, index := range indexes {
- if index.Primary {
- if _, err := fmt.Fprintf(buf, ",\n PRIMARY KEY ("); err != nil {
- return err
- }
- for i, column := range index.Expressions {
- if i != 0 {
- if _, err := fmt.Fprintf(buf, ", "); err != nil {
- return err
- }
- }
- keyLength := int64(-1)
- descending := false
- if len(index.KeyLength) > i {
- keyLength = index.KeyLength[i]
- }
- if len(index.Descending) > i {
- descending = index.Descending[i]
- }
- if err := printIndexKeyPart(buf, column, keyLength, descending); err != nil {
- return err
- }
- }
- if _, err := fmt.Fprintf(buf, ")"); err != nil {
- return err
- }
- return nil
- }
- }
-
- return nil
-}
-
-func isAutoIncrement(column *storepb.ColumnMetadata) bool {
- return strings.EqualFold(column.GetDefaultExpression(), autoIncrementSymbol)
-}
-
-func printColumnClause(buf *strings.Builder, column *storepb.ColumnMetadata) error {
- if _, err := fmt.Fprintf(buf, " `%s` %s", column.Name, column.Type); err != nil {
- return err
- }
-
- if column.CharacterSet != "" {
- if _, err := fmt.Fprintf(buf, " CHARACTER SET %s", column.CharacterSet); err != nil {
- return err
- }
- }
-
- if column.Collation != "" {
- if _, err := fmt.Fprintf(buf, " COLLATE %s", column.Collation); err != nil {
- return err
- }
- }
-
- if column.Generation != nil && column.Generation.Expression != "" {
- if _, err := fmt.Fprintf(buf, " GENERATED ALWAYS AS (%s) ", column.Generation.Expression); err != nil {
- return err
- }
- switch column.Generation.Type {
- case storepb.GenerationMetadata_TYPE_STORED:
- if _, err := fmt.Fprintf(buf, "STORED"); err != nil {
- return err
- }
- case storepb.GenerationMetadata_TYPE_VIRTUAL:
- if _, err := fmt.Fprintf(buf, "VIRTUAL"); err != nil {
- return err
- }
- }
- }
-
- if column.Nullable {
- if _, err := fmt.Fprintf(buf, " NULL"); err != nil {
- return err
- }
- } else {
- if _, err := fmt.Fprintf(buf, " NOT NULL"); err != nil {
- return err
- }
- }
-
- if err := printDefaultClause(buf, column); err != nil {
- return err
- }
-
- // Handle auto_increment.
- if isAutoIncrement(column) {
- if _, err := fmt.Fprintf(buf, " %s", autoIncrementSymbol); err != nil {
- return err
- }
- }
-
- if column.OnUpdate != "" {
- if _, err := buf.WriteString(fmt.Sprintf(" ON UPDATE %s", column.OnUpdate)); err != nil {
- return err
- }
- }
- if column.Comment != "" {
- if _, err := fmt.Fprintf(buf, " COMMENT '%s'", column.Comment); err != nil {
- return err
- }
- }
- return nil
-}
-
-func printDefaultClause(buf *strings.Builder, column *storepb.ColumnMetadata) error {
- if column.DefaultValue == nil {
- return nil
- }
-
- if column.GetDefaultNull() {
- if !column.Nullable || !typeSupportsDefaultValue(column.Type) {
- // If the column is not nullable, then the default value should not be null.
- // For this case, we should not print the default clause.
- return nil
- }
- if column.Generation != nil && column.Generation.Expression != "" {
- return nil
- }
- if _, err := fmt.Fprintf(buf, " DEFAULT NULL"); err != nil {
- return err
- }
- return nil
- }
-
- if column.GetDefaultExpression() != "" {
- if isAutoIncrement(column) {
- // If the default value is auto_increment, then we should not print the default clause.
- // We'll handle this in the following AUTO_INCREMENT clause.
- return nil
- }
- if _, err := fmt.Fprintf(buf, " DEFAULT %s", column.GetDefaultExpression()); err != nil {
- return err
- }
- return nil
- }
-
- if column.GetDefault() != nil {
- if _, err := fmt.Fprintf(buf, " DEFAULT '%s'", column.GetDefault().GetValue()); err != nil {
- return err
- }
- }
-
- if column.OnUpdate != "" {
- if _, err := fmt.Fprintf(buf, " ON UPDATE %s", column.OnUpdate); err != nil {
- return err
- }
- }
-
- return nil
-}
-
-func typeSupportsDefaultValue(tp string) bool {
- switch strings.ToLower(tp) {
- case mysqlTypeBlob, mysqlTypeTinyBob, mysqlTypeMediumBlob, mysqlTypeLongBlob, mysqlTypeJSON, mysqlTypeGeometry:
- return false
- default:
- return true
- }
-}
diff --git a/backend/plugin/schema/schema.go b/backend/plugin/schema/schema.go
index c7c8829353..3ede1e7a54 100644
--- a/backend/plugin/schema/schema.go
+++ b/backend/plugin/schema/schema.go
@@ -20,7 +20,8 @@ var (
getViewDefinitions = make(map[storepb.Engine]getViewDefinition)
getMaterializedViewDefinitions = make(map[storepb.Engine]getMaterializedViewDefinition)
getFunctionDefinitions = make(map[storepb.Engine]getFunctionDefinition)
- GetSequenceDefinitions = make(map[storepb.Engine]getSequenceDefinition)
+ getProcedureDefinitions = make(map[storepb.Engine]getProcedureDefinition)
+ getSequenceDefinitions = make(map[storepb.Engine]getSequenceDefinition)
)
type getDesignSchema func(*storepb.DatabaseSchemaMetadata) (string, error)
@@ -32,6 +33,7 @@ type getTableDefinition func(string, *storepb.TableMetadata, []*storepb.Sequence
type getViewDefinition func(string, *storepb.ViewMetadata) (string, error)
type getMaterializedViewDefinition func(string, *storepb.MaterializedViewMetadata) (string, error)
type getFunctionDefinition func(string, *storepb.FunctionMetadata) (string, error)
+type getProcedureDefinition func(string, *storepb.ProcedureMetadata) (string, error)
type getSequenceDefinition func(string, *storepb.SequenceMetadata) (string, error)
type GetDefinitionContext struct {
@@ -42,14 +44,14 @@ type GetDefinitionContext struct {
func RegisterGetSequenceDefinition(engine storepb.Engine, f getSequenceDefinition) {
mux.Lock()
defer mux.Unlock()
- if _, dup := GetSequenceDefinitions[engine]; dup {
+ if _, dup := getSequenceDefinitions[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- GetSequenceDefinitions[engine] = f
+ getSequenceDefinitions[engine] = f
}
func GetSequenceDefinition(engine storepb.Engine, sequenceName string, sequence *storepb.SequenceMetadata) (string, error) {
- f, ok := GetSequenceDefinitions[engine]
+ f, ok := getSequenceDefinitions[engine]
if !ok {
return "", errors.Errorf("engine %s is not supported", engine)
}
@@ -73,6 +75,23 @@ func GetFunctionDefinition(engine storepb.Engine, functionName string, function
return f(functionName, function)
}
+func RegisterGetProcedureDefinition(engine storepb.Engine, f getProcedureDefinition) {
+ mux.Lock()
+ defer mux.Unlock()
+ if _, dup := getProcedureDefinitions[engine]; dup {
+ panic(fmt.Sprintf("Register called twice %s", engine))
+ }
+ getProcedureDefinitions[engine] = f
+}
+
+func GetProcedureDefinition(engine storepb.Engine, procedureName string, procedure *storepb.ProcedureMetadata) (string, error) {
+ f, ok := getProcedureDefinitions[engine]
+ if !ok {
+ return "", errors.Errorf("engine %s is not supported", engine)
+ }
+ return f(procedureName, procedure)
+}
+
func RegisterGetMaterializedViewDefinition(engine storepb.Engine, f getMaterializedViewDefinition) {
mux.Lock()
defer mux.Unlock()