Skip to content

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

feat: improve branch merge (#9863)

h3n4le0f2103c999f
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.go
index 9f30550c9b..2e09192deb 100644
--- a/backend/api/v1/schema_design_merge.go
+++ b/backend/api/v1/schema_design_merge.go
@@ -3,50 +3,48 @@ package v1
import (
"fmt"
"reflect"
+ "slices"
+ "github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/testing/protocmp"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-func tryMerge(base, head, target *storepb.DatabaseSchemaMetadata) (*storepb.DatabaseSchemaMetadata, error) {
- base, head, target = proto.Clone(base).(*storepb.DatabaseSchemaMetadata), proto.Clone(head).(*storepb.DatabaseSchemaMetadata), proto.Clone(target).(*storepb.DatabaseSchemaMetadata)
+type diffAction string
+
+const (
+ diffActionCreate diffAction = "CREATE"
+ diffActionUpdate diffAction = "UPDATE"
+ diffActionDrop diffAction = "DROP"
+)
+
+func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata) (*storepb.DatabaseSchemaMetadata, error) {
+ ancestor, head, base = proto.Clone(ancestor).(*storepb.DatabaseSchemaMetadata), proto.Clone(head).(*storepb.DatabaseSchemaMetadata), proto.Clone(base).(*storepb.DatabaseSchemaMetadata)
- diffBetweenBaseAndHead, err := diffMetadata(base, head)
+ diffBetweenAncestorAndHead, err := diffMetadata(ancestor, head)
if err != nil {
- return nil, errors.Wrap(err, "failed to diff between base and head")
+ return nil, errors.Wrap(err, "failed to diff between ancestor and head")
}
- diffBetweenBaseAndTarget, err := diffMetadata(base, target)
+ diffBetweenAncestorAndBase, err := diffMetadata(ancestor, base)
if err != nil {
- return nil, errors.Wrap(err, "failed to diff between base and target")
+ return nil, errors.Wrap(err, "failed to diff between ancestor and base")
}
- if conflict, msg := diffBetweenBaseAndHead.isConflictWith(diffBetweenBaseAndTarget); conflict {
+ if conflict, msg := diffBetweenAncestorAndHead.tryMerge(diffBetweenAncestorAndBase); conflict {
return nil, errors.Errorf("merge conflict: %s", msg)
}
- if err := diffBetweenBaseAndHead.applyDiffTo(target); err != nil {
+ if err := diffBetweenAncestorAndHead.applyDiffTo(ancestor); err != nil {
return nil, errors.Wrap(err, "failed to apply diff to target")
}
- return target, nil
-}
-
-type metadataDiffNode interface {
- isConflictWith(other metadataDiffNode) (bool, string)
- applyDiffTo(target proto.Message) error
+ return ancestor, nil
}
-type diffAction string
-
-const (
- diffActionCreate diffAction = "CREATE"
- diffActionUpdate diffAction = "UPDATE"
- diffActionDrop diffAction = "DROP"
-)
-
type metadataDiffBaseNode struct {
action diffAction
}
@@ -55,22 +53,34 @@ type metadataDiffRootNode struct {
schemas map[string]*metadataDiffSchemaNode
}
-func (mr *metadataDiffRootNode) isConflictWith(other *metadataDiffRootNode) (bool, string) {
+// tryMerge merges other root node to current root node, stop and return error if conflict occurs.
+func (mr *metadataDiffRootNode) tryMerge(other *metadataDiffRootNode) (bool, string) {
for _, schema := range mr.schemas {
otherSchema, in := other.schemas[schema.name]
if !in {
continue
}
- conflict, msg := schema.isConflictWith(otherSchema)
+ conflict, msg := schema.tryMerge(otherSchema)
if conflict {
return true, msg
}
+ delete(other.schemas, schema.name)
+ }
+ // Append other schema to current root node.
+ for _, otherSchema := range other.schemas {
+ mr.schemas[otherSchema.name] = otherSchema
}
return false, ""
}
func (mr *metadataDiffRootNode) applyDiffTo(target *storepb.DatabaseSchemaMetadata) error {
- for _, schema := range mr.schemas {
+ sortedSchemaNames := make([]string, 0, len(mr.schemas))
+ for schemaName := range mr.schemas {
+ sortedSchemaNames = append(sortedSchemaNames, schemaName)
+ }
+ slices.Sort(sortedSchemaNames)
+ for _, schemaName := range sortedSchemaNames {
+ schema := mr.schemas[schemaName]
if err := schema.applyDiffTo(target); err != nil {
return errors.Wrapf(err, "failed to apply diff to schema %q", schema.name)
}
@@ -83,69 +93,105 @@ type metadataDiffSchemaNode struct {
metadataDiffBaseNode
name string
//nolint
- from *storepb.SchemaMetadata
- to *storepb.SchemaMetadata
+ base *storepb.SchemaMetadata
+ head *storepb.SchemaMetadata
- tables map[string]metadataDiffNode
+ tables map[string]*metadataDiffTableNode
// SchemaMetadata contains other object types, likes function, view and etc, but we do not support them yet.
}
-func (n *metadataDiffSchemaNode) isConflictWith(other metadataDiffNode) (bool, string) {
+func (n *metadataDiffSchemaNode) tryMerge(other *metadataDiffSchemaNode) (bool, string) {
if other == nil {
return true, "other node check conflict with schema node not be nil"
}
- switch other := other.(type) {
- case *metadataDiffSchemaNode:
- if !(n.action == diffActionUpdate && other.action == diffActionUpdate) {
- return true, fmt.Sprintf("conflict schema action, one is %s, the other is %s", n.action, other.action)
+ if n.name != other.name {
+ return true, fmt.Sprintf("non-expected schema node pair, one is %s, the other is %s", n.name, other.name)
+ }
+ if n.action != other.action {
+ return true, fmt.Sprintf("conflict schema action, one is %s, the other is %s", n.action, other.action)
+ }
+
+ if n.action == diffActionDrop {
+ return false, ""
+ }
+
+ // if n.action == diffActionCreate {
+ // // If two actions are CREATE both, we need to check the schema attributes is conflict.
+ // // XXX: Expanding the schema attributes check if we support more attributes.
+ // }
+
+ // if n.action == diffActionUpdate {
+ // // If two actions are UPDATE both, we need to check the schema attributes is conflict.
+ // // XXX: Expanding the schema attributes check if we support more attributes.
+ // }
+
+ for tableName, tableNode := range n.tables {
+ otherTableNode, in := other.tables[tableName]
+ if !in {
+ continue
}
- if n.name != other.name {
- return true, fmt.Sprintf("conflict schema name, one is %s, the other is %s", n.name, other.name)
+ conflict, msg := tableNode.tryMerge(otherTableNode)
+ if conflict {
+ return true, msg
}
+ delete(other.tables, tableName)
+ }
- for tableName, tableNode := range n.tables {
- otherTableNode, in := other.tables[tableName]
- if !in {
- continue
- }
- conflict, msg := tableNode.isConflictWith(otherTableNode)
- if conflict {
- return true, msg
- }
- }
- return false, ""
- default:
- return true, fmt.Sprintf("non-expected node type pair, one is %T, the other is %T", n, other)
+ for _, remainingTable := range other.tables {
+ n.tables[remainingTable.name] = remainingTable
}
+
+ return false, ""
}
-func (n *metadataDiffSchemaNode) applyDiffTo(target proto.Message) error {
- databaseTarget, ok := target.(*storepb.DatabaseSchemaMetadata)
- if !ok {
- return errors.Errorf("target is not a database metadata, but %T", target)
+func (n *metadataDiffSchemaNode) applyDiffTo(target *storepb.DatabaseSchemaMetadata) error {
+ if target == nil {
+ return errors.New("target must not be nil")
}
+ sortedTableNames := make([]string, 0, len(n.tables))
+ for tableName := range n.tables {
+ sortedTableNames = append(sortedTableNames, tableName)
+ }
+ slices.Sort(sortedTableNames)
+
switch n.action {
case diffActionCreate:
- databaseTarget.Schemas = append(databaseTarget.Schemas, n.to)
+ newSchema := &storepb.SchemaMetadata{
+ Name: n.name,
+ }
+ for _, tableName := range sortedTableNames {
+ table := n.tables[tableName]
+ if err := table.applyDiffTo(newSchema); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to table %q", table.name)
+ }
+ }
+ target.Schemas = append(target.Schemas, newSchema)
case diffActionDrop:
- for i, schema := range databaseTarget.Schemas {
+ for i, schema := range target.Schemas {
if schema.Name == n.name {
- databaseTarget.Schemas = append(databaseTarget.Schemas[:i], databaseTarget.Schemas[i+1:]...)
+ target.Schemas = append(target.Schemas[:i], target.Schemas[i+1:]...)
break
}
}
case diffActionUpdate:
- for _, schema := range databaseTarget.Schemas {
+
+ for idx, schema := range target.Schemas {
if schema.Name == n.name {
+ newSchema := &storepb.SchemaMetadata{
+ Name: n.name,
+ Tables: schema.Tables,
+ }
// Update schema currently is only contains diff of tables. So we do apply table diff to target schema.
- for _, table := range n.tables {
- if err := table.applyDiffTo(schema); err != nil {
- return errors.Wrapf(err, "failed to apply diff to table %q", table)
+ for _, tableName := range sortedTableNames {
+ table := n.tables[tableName]
+ if err := table.applyDiffTo(newSchema); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to table %q", table.name)
}
}
+ target.Schemas[idx] = newSchema
}
}
}
@@ -160,83 +206,270 @@ type metadataDiffTableNode struct {
from *storepb.TableMetadata
to *storepb.TableMetadata
- columns map[string]metadataDiffNode
- foreignKeys map[string]metadataDiffNode
-
+ columns map[string]*metadataDiffColumnNode
+ foreignKeys map[string]*metadataDiffForeignKeyNode
+ indexes map[string]*metadataDiffIndexNode
// TableMetaData contains other object types, likes trigger, index and etc, but we do not support them yet.
}
-func (n *metadataDiffTableNode) isConflictWith(other metadataDiffNode) (bool, string) {
+func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode) (bool, string) {
if other == nil {
return true, "other node check conflict with table node must not be nil"
}
- switch other := other.(type) {
- case *metadataDiffTableNode:
- if !(n.action == diffActionUpdate && other.action == diffActionUpdate) {
- return true, fmt.Sprintf("conflict table action, one is %s, the other is %s", n.action, other.action)
+ if n.name != other.name {
+ return true, fmt.Sprintf("non-expected table node pair, one is %s, the other is %s", n.name, other.name)
+ }
+ if n.action != other.action {
+ return true, fmt.Sprintf("conflict table action, one is %s, the other is %s", n.action, other.action)
+ }
+
+ if n.action == diffActionDrop {
+ return false, ""
+ }
+
+ if n.action == diffActionCreate {
+ // If two actions are CREATE or UPDATE both, we need to check the table attributes is conflict.
+ // XXX: Expanding the table attributes check if we support more attributes.
+ if n.to.Engine != other.to.Engine {
+ return true, fmt.Sprintf("conflict table engine, one is %s, the other is %s", n.to.Engine, other.to.Engine)
+ }
+ if n.to.Collation != other.to.Collation {
+ return true, fmt.Sprintf("conflict table collation, one is %s, the other is %s", n.to.Collation, other.to.Collation)
+ }
+ if n.to.Comment != other.to.Comment {
+ return true, fmt.Sprintf("conflict table comment, one is %s, the other is %s", n.to.Comment, other.to.Comment)
+ }
+ if n.to.UserComment != other.to.UserComment {
+ return true, fmt.Sprintf("conflict table user comment, one is %s, the other is %s", n.to.UserComment, other.to.UserComment)
}
- if n.name != other.name {
- return true, fmt.Sprintf("conflict table name, one is %s, the other is %s", n.name, other.name)
+ if n.to.Classification != other.to.Classification {
+ return true, fmt.Sprintf("conflict table classification, one is %s, the other is %s", n.to.Classification, other.to.Classification)
}
+ }
- for columnName, columnNode := range n.columns {
- otherColumnNode, in := other.columns[columnName]
- if !in {
- continue
+ if n.action == diffActionUpdate {
+ if other.from.Engine != other.to.Engine {
+ if n.from.Engine != n.to.Engine {
+ if n.to.Engine != other.to.Engine {
+ return true, fmt.Sprintf("conflict table engine, one is %s, the other is %s", n.from.Engine, other.from.Engine)
+ }
+ } else {
+ n.to.Engine = other.to.Engine
}
- conflict, msg := columnNode.isConflictWith(otherColumnNode)
- if conflict {
- return true, msg
+ }
+
+ if other.from.Collation != other.to.Collation {
+ if n.from.Collation != n.to.Collation {
+ if n.to.Collation != other.to.Collation {
+ return true, fmt.Sprintf("conflict table collation, one is %s, the other is %s", n.from.Collation, other.from.Collation)
+ }
+ } else {
+ n.to.Collation = other.to.Collation
+ }
+ }
+
+ if other.from.Comment != other.to.Comment {
+ if n.from.Comment != n.to.Comment {
+ if n.to.Comment != other.to.Comment {
+ return true, fmt.Sprintf("conflict table comment, one is %s, the other is %s", n.from.Comment, other.from.Comment)
+ }
+ } else {
+ n.to.Comment = other.to.Comment
}
}
- for foreignKeyName, foreignKeyNode := range n.foreignKeys {
- otherForeignKeyNode, in := other.foreignKeys[foreignKeyName]
- if !in {
- continue
+ if other.from.UserComment != other.to.UserComment {
+ if n.from.UserComment != n.to.UserComment {
+ if n.to.UserComment != other.to.UserComment {
+ return true, fmt.Sprintf("conflict table user comment, one is %s, the other is %s", n.from.UserComment, other.from.UserComment)
+ }
+ } else {
+ n.to.UserComment = other.to.UserComment
}
- conflict, msg := foreignKeyNode.isConflictWith(otherForeignKeyNode)
- if conflict {
- return true, msg
+ }
+
+ if other.from.Classification != other.to.Classification {
+ if n.from.Classification != n.to.Classification {
+ if n.to.Classification != other.to.Classification {
+ return true, fmt.Sprintf("conflict table classification, one is %s, the other is %s", n.from.Classification, other.from.Classification)
+ }
+ } else {
+ n.to.Classification = other.to.Classification
}
}
- return false, ""
- default:
- return true, fmt.Sprintf("non-expected node type pair, one is %T, the other is %T", n, other)
}
+
+ for columnName, columnNode := range n.columns {
+ otherColumnNode, in := other.columns[columnName]
+ if !in {
+ continue
+ }
+ conflict, msg := columnNode.tryMerge(otherColumnNode)
+ if conflict {
+ return true, msg
+ }
+ delete(other.columns, columnName)
+ }
+
+ for foreignKeyName, foreignKeyNode := range n.foreignKeys {
+ otherForeignKeyNode, in := other.foreignKeys[foreignKeyName]
+ if !in {
+ continue
+ }
+ conflict, msg := foreignKeyNode.tryMerge(otherForeignKeyNode)
+ if conflict {
+ return true, msg
+ }
+ delete(other.foreignKeys, foreignKeyName)
+ }
+
+ for indexName, indexNode := range n.indexes {
+ otherIndexNode, in := other.indexes[indexName]
+ if !in {
+ continue
+ }
+ conflict, msg := indexNode.tryMerge(otherIndexNode)
+ if conflict {
+ return true, msg
+ }
+ delete(other.indexes, indexName)
+ }
+
+ for _, remainingColumn := range other.columns {
+ n.columns[remainingColumn.name] = remainingColumn
+ }
+
+ for _, remainingForeignKey := range other.foreignKeys {
+ n.foreignKeys[remainingForeignKey.name] = remainingForeignKey
+ }
+
+ for _, remainingIndex := range other.indexes {
+ n.indexes[remainingIndex.name] = remainingIndex
+ }
+
+ return false, ""
}
-func (n *metadataDiffTableNode) applyDiffTo(target proto.Message) error {
- schemaTarget, ok := target.(*storepb.SchemaMetadata)
- if !ok {
- return errors.Errorf("target is not a schema metadata, but %T", target)
+func (n *metadataDiffTableNode) applyDiffTo(target *storepb.SchemaMetadata) error {
+ if target == nil {
+ return errors.New("target must not be nil")
}
+ sortedColumnName := make([]string, 0, len(n.columns))
+ for columnName := range n.columns {
+ sortedColumnName = append(sortedColumnName, columnName)
+ }
+ slices.Sort(sortedColumnName)
+
+ sortedForeignKeyName := make([]string, 0, len(n.foreignKeys))
+ for foreignKeyName := range n.foreignKeys {
+ sortedForeignKeyName = append(sortedForeignKeyName, foreignKeyName)
+ }
+ slices.Sort(sortedForeignKeyName)
+
+ sortedIndexName := make([]string, 0, len(n.indexes))
+ for indexName := range n.indexes {
+ sortedIndexName = append(sortedIndexName, indexName)
+ }
+ slices.Sort(sortedIndexName)
+
switch n.action {
case diffActionCreate:
- schemaTarget.Tables = append(schemaTarget.Tables, n.to)
+ columnsInHead := make(map[string]bool)
+ for _, column := range n.to.Columns {
+ columnsInHead[column.Name] = true
+ }
+ newTable := &storepb.TableMetadata{
+ Name: n.name,
+ Engine: n.to.Engine,
+ Collation: n.to.Collation,
+ Comment: n.to.Comment,
+ UserComment: n.to.UserComment,
+ Classification: n.to.Classification,
+ }
+ for columnName := range columnsInHead {
+ if columnNode, in := n.columns[columnName]; in {
+ if err := columnNode.applyDiffTo(newTable); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to column %q", columnNode.name)
+ }
+ }
+ }
+
+ for _, columnName := range sortedColumnName {
+ if _, in := columnsInHead[columnName]; !in {
+ column := n.columns[columnName]
+ if err := column.applyDiffTo(newTable); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to column %q", column.name)
+ }
+ }
+ }
+
+ // XXX(zp): We need to find a better way to solve the problem of column position.
+ // We need to sort the columns by position after applying the diff.
+ for idx := range newTable.Columns {
+ newTable.Columns[idx].Position = int32(idx + 1)
+ }
+
+ for _, foreignKeyName := range sortedForeignKeyName {
+ foreignKey := n.foreignKeys[foreignKeyName]
+ if err := foreignKey.applyDiffTo(newTable); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to foreign key %q", foreignKey.name)
+ }
+ }
+ for _, indexName := range sortedIndexName {
+ index := n.indexes[indexName]
+ if err := index.applyDiffTo(n); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to index %q", index.name)
+ }
+ }
+ target.Tables = append(target.Tables, newTable)
case diffActionDrop:
- for i, table := range schemaTarget.Tables {
+ for i, table := range target.Tables {
if table.Name == n.name {
- schemaTarget.Tables = append(schemaTarget.Tables[:i], schemaTarget.Tables[i+1:]...)
+ target.Tables = append(target.Tables[:i], target.Tables[i+1:]...)
break
}
}
case diffActionUpdate:
- for _, table := range schemaTarget.Tables {
+ for idx, table := range target.Tables {
// Update table currently is only contains diff of columns and foreign keys. So we do apply column and foreign key diff to target table.
if table.Name == n.name {
- for _, column := range n.columns {
- if err := column.applyDiffTo(table); err != nil {
- return errors.Wrapf(err, "failed to apply diff to column %q", column)
+ newTable := &storepb.TableMetadata{
+ Name: n.name,
+ Engine: n.to.Engine,
+ Collation: n.to.Collation,
+ Comment: n.to.Comment,
+ UserComment: n.to.UserComment,
+ Classification: n.to.Classification,
+ Columns: table.Columns,
+ ForeignKeys: table.ForeignKeys,
+ Indexes: table.Indexes,
+ }
+ for _, columnName := range sortedColumnName {
+ column := n.columns[columnName]
+ if err := column.applyDiffTo(newTable); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to column %q", column.name)
+ }
+ }
+ for _, foreignKeyName := range sortedForeignKeyName {
+ foreignKey := n.foreignKeys[foreignKeyName]
+ if err := foreignKey.applyDiffTo(newTable); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to foreign key %q", foreignKey.name)
}
}
- for _, foreignKey := range n.foreignKeys {
- if err := foreignKey.applyDiffTo(table); err != nil {
- return errors.Wrapf(err, "failed to apply diff to foreign key %q", foreignKey)
+ for _, indexName := range sortedIndexName {
+ index := n.indexes[indexName]
+ if err := index.applyDiffTo(n); err != nil {
+ return errors.Wrapf(err, "failed to apply diff to index %q", index.name)
}
}
+ // XXX(zp): We need to find a better way to solve the problem of column position.
+ // We need to sort the columns by position after applying the diff.
+ for idx := range newTable.Columns {
+ newTable.Columns[idx].Position = int32(idx + 1)
+ }
+ target.Tables[idx] = newTable
}
}
}
@@ -252,44 +485,228 @@ type metadataDiffColumnNode struct {
to *storepb.ColumnMetadata
}
-func (n *metadataDiffColumnNode) isConflictWith(other metadataDiffNode) (bool, string) {
+func (n *metadataDiffColumnNode) tryMerge(other *metadataDiffColumnNode) (bool, string) {
if other == nil {
return true, "other node check conflict with column node must not be nil"
}
- switch other := other.(type) {
- case *metadataDiffColumnNode:
- if !(n.action == diffActionUpdate && other.action == diffActionUpdate) {
- return true, fmt.Sprintf("conflict column action, one is %s, the other is %s", n.action, other.action)
+ if n.name != other.name {
+ return true, fmt.Sprintf("non-expected column node pair, one is %s, the other is %s", n.name, other.name)
+ }
+ if n.action != other.action {
+ return true, fmt.Sprintf("conflict column action, one is %s, the other is %s", n.action, other.action)
+ }
+
+ if n.action == diffActionDrop {
+ return false, ""
+ }
+
+ if n.action == diffActionCreate {
+ if n.to.Type != other.to.Type {
+ return true, fmt.Sprintf("conflict column type, one is %s, the other is %s", n.to.Type, other.to.Type)
+ }
+ if n.to.DefaultValue != other.to.DefaultValue {
+ return true, fmt.Sprintf("conflict column default value, one is %s, the other is %s", n.to.DefaultValue, other.to.DefaultValue)
+ }
+ if n.to.Nullable != other.to.Nullable {
+ return true, fmt.Sprintf("conflict column nullable, one is %t, the other is %t", n.to.Nullable, other.to.Nullable)
+ }
+ if n.to.Comment != other.to.Comment {
+ return true, fmt.Sprintf("conflict column comment, one is %s, the other is %s", n.to.Comment, other.to.Comment)
+ }
+ if n.to.UserComment != other.to.UserComment {
+ return true, fmt.Sprintf("conflict column user comment, one is %s, the other is %s", n.to.UserComment, other.to.UserComment)
+ }
+ if n.to.Classification != other.to.Classification {
+ return true, fmt.Sprintf("conflict column classification, one is %s, the other is %s", n.to.Classification, other.to.Classification)
}
- // TODO(zp): check update column conflict
- return true, "not implemented yet"
- default:
- return true, fmt.Sprintf("non-expected node type pair, one is %T, the other is %T", n, other)
}
-}
+ if n.action == diffActionUpdate {
+ if other.from.Type != other.to.Type {
+ if n.from.Type != n.to.Type {
+ if n.to.Type != other.to.Type {
+ return true, fmt.Sprintf("conflict column type, one is %s, the other is %s", n.from.Type, other.from.Type)
+ }
+ } else {
+ n.to.Type = other.to.Type
+ }
+ }
+
+ if otherDiff := cmp.Diff(other.from.DefaultValue, other.to.DefaultValue, protocmp.Transform()); otherDiff != "" {
+ if nDiff := cmp.Diff(n.from.DefaultValue, n.to.DefaultValue, protocmp.Transform()); nDiff != "" {
+ if d := cmp.Diff(n.to.DefaultValue, other.to.DefaultValue, protocmp.Transform()); d != "" {
+ return true, fmt.Sprintf("conflict column default value, one is %v, the other is %v", n.from.DefaultValue, other.from.DefaultValue)
+ }
+ } else {
+ n.to.DefaultValue = other.to.DefaultValue
+ }
+ }
+
+ if other.from.Nullable != other.to.Nullable {
+ if n.from.Nullable != n.to.Nullable {
+ if n.to.Nullable != other.to.Nullable {
+ return true, fmt.Sprintf("conflict column nullable, one is %t, the other is %t", n.from.Nullable, other.from.Nullable)
+ }
+ } else {
+ n.to.Nullable = other.to.Nullable
+ }
+ }
+
+ if other.from.Comment != other.to.Comment {
+ if n.from.Comment != n.to.Comment {
+ if n.to.Comment != other.to.Comment {
+ return true, fmt.Sprintf("conflict column comment, one is %s, the other is %s", n.from.Comment, other.from.Comment)
+ }
+ } else {
+ n.to.Comment = other.to.Comment
+ }
+ }
+
+ if other.from.UserComment != other.to.UserComment {
+ if n.from.UserComment != n.to.UserComment {
+ if n.to.UserComment != other.to.UserComment {
+ return true, fmt.Sprintf("conflict column user comment, one is %s, the other is %s", n.from.UserComment, other.from.UserComment)
+ }
+ } else {
+ n.to.UserComment = other.to.UserComment
+ }
+ }
-func (n *metadataDiffColumnNode) applyDiffTo(target proto.Message) error {
- tableTarget, ok := target.(*storepb.TableMetadata)
- if !ok {
- return errors.Errorf("target is not a table metadata, but %T", target)
+ if other.from.Classification != other.to.Classification {
+ if n.from.Classification != n.to.Classification {
+ if n.to.Classification != other.to.Classification {
+ return true, fmt.Sprintf("conflict column classification, one is %s, the other is %s", n.from.Classification, other.from.Classification)
+ }
+ } else {
+ n.to.Classification = other.to.Classification
+ }
+ }
}
+ return false, ""
+}
+
+func (n *metadataDiffColumnNode) applyDiffTo(target *storepb.TableMetadata) error {
// TODO(zp): handle the column position...
switch n.action {
case diffActionCreate:
- tableTarget.Columns = append(tableTarget.Columns, n.to)
+ target.Columns = append(target.Columns, n.to)
case diffActionDrop:
- for i, column := range tableTarget.Columns {
+ for i, column := range target.Columns {
if column.Name == n.name {
- tableTarget.Columns = append(tableTarget.Columns[:i], tableTarget.Columns[i+1:]...)
+ target.Columns = append(target.Columns[:i], target.Columns[i+1:]...)
break
}
}
case diffActionUpdate:
- for i, column := range tableTarget.Columns {
+ for i, column := range target.Columns {
if column.Name == n.name {
- tableTarget.Columns[i] = n.to
+ target.Columns[i] = n.to
+ break
+ }
+ }
+ }
+ return nil
+}
+
+// Index related.
+type metadataDiffIndexNode struct {
+ metadataDiffBaseNode
+ name string
+ //nolint
+ from *storepb.IndexMetadata
+ to *storepb.IndexMetadata
+}
+
+func (n *metadataDiffIndexNode) tryMerge(other *metadataDiffIndexNode) (bool, string) {
+ if other == nil {
+ return true, "other node check conflict with column node must not be nil"
+ }
+
+ if n.name != other.name {
+ return true, fmt.Sprintf("non-expected index node pair, one is %s, the other is %s", n.name, other.name)
+ }
+ if n.action != other.action {
+ return true, fmt.Sprintf("conflict index action, one is %s, the other is %s", n.action, other.action)
+ }
+ if n.action == diffActionDrop {
+ return false, ""
+ }
+ if n.action == diffActionCreate {
+ if slices.Equal(n.to.Expressions, other.to.Expressions) {
+ return true, fmt.Sprintf("conflict index expressions, one is %v, the other is %v", n.to.Expressions, other.to.Expressions)
+ }
+ if n.to.Type != other.to.Type {
+ return true, fmt.Sprintf("conflict index type, one is %s, the other is %s", n.to.Type, other.to.Type)
+ }
+ if n.to.Unique != other.to.Unique {
+ return true, fmt.Sprintf("conflict index unique, one is %t, the other is %t", n.to.Unique, other.to.Unique)
+ }
+ if n.to.Primary != other.to.Primary {
+ return true, fmt.Sprintf("conflict index primary, one is %t, the other is %t", n.to.Primary, other.to.Primary)
+ }
+ }
+
+ if n.action == diffActionUpdate {
+ if !slices.Equal(other.from.Expressions, other.to.Expressions) {
+ if !slices.Equal(n.from.Expressions, n.to.Expressions) {
+ if !slices.Equal(n.to.Expressions, other.to.Expressions) {
+ return true, fmt.Sprintf("conflict index expressions, one is %v, the other is %v", n.from.Expressions, other.from.Expressions)
+ }
+ } else {
+ n.to.Expressions = other.to.Expressions
+ }
+ }
+
+ if other.from.Type != other.to.Type {
+ if n.from.Type != n.to.Type {
+ if n.to.Type != other.to.Type {
+ return true, fmt.Sprintf("conflict index type, one is %s, the other is %s", n.from.Type, other.from.Type)
+ }
+ } else {
+ n.to.Type = other.to.Type
+ }
+ }
+
+ if other.from.Unique != other.to.Unique {
+ if n.from.Unique != n.to.Unique {
+ if n.to.Unique != other.to.Unique {
+ return true, fmt.Sprintf("conflict index unique, one is %t, the other is %t", n.from.Unique, other.from.Unique)
+ }
+ } else {
+ n.to.Unique = other.to.Unique
+ }
+ }
+
+ if other.from.Primary != other.to.Primary {
+ if n.from.Primary != n.to.Primary {
+ if n.to.Primary != other.to.Primary {
+ return true, fmt.Sprintf("conflict index primary, one is %t, the other is %t", n.from.Primary, other.from.Primary)
+ }
+ } else {
+ n.to.Primary = other.to.Primary
+ }
+ }
+ }
+
+ return false, ""
+}
+
+func (n *metadataDiffIndexNode) applyDiffTo(target *metadataDiffTableNode) error {
+ switch n.action {
+ case diffActionCreate:
+ target.to.Indexes = append(target.to.Indexes, n.to)
+ case diffActionDrop:
+ for i, index := range target.to.Indexes {
+ if index.Name == n.name {
+ target.to.Indexes = append(target.to.Indexes[:i], target.to.Indexes[i+1:]...)
+ break
+ }
+ }
+ case diffActionUpdate:
+ for i, index := range target.to.Indexes {
+ if index.Name == n.name {
+ target.to.Indexes[i] = n.to
break
}
}
@@ -306,43 +723,110 @@ type metadataDiffForeignKeyNode struct {
to *storepb.ForeignKeyMetadata
}
-func (n *metadataDiffForeignKeyNode) isConflictWith(other metadataDiffNode) (bool, string) {
+func (n *metadataDiffForeignKeyNode) tryMerge(other *metadataDiffForeignKeyNode) (bool, string) {
if other == nil {
return true, "other node check conflict with column node must not be nil"
}
- switch other := other.(type) {
- case *metadataDiffForeignKeyNode:
- if !(n.action == diffActionUpdate && other.action == diffActionUpdate) {
- return true, fmt.Sprintf("conflict column action, one is %s, the other is %s", n.action, other.action)
+ if n.name != other.name {
+ return true, fmt.Sprintf("non-expected foreign key node pair, one is %s, the other is %s", n.name, other.name)
+ }
+ if n.action != other.action {
+ return true, fmt.Sprintf("conflict foreign key action, one is %s, the other is %s", n.action, other.action)
+ }
+ if n.action == diffActionDrop {
+ return false, ""
+ }
+ if n.action == diffActionCreate {
+ if n.to.ReferencedSchema != other.to.ReferencedSchema {
+ return true, fmt.Sprintf("conflict foreign key referenced schema, one is %s, the other is %s", n.to.ReferencedSchema, other.to.ReferencedSchema)
+ }
+ if n.to.ReferencedTable != other.to.ReferencedTable {
+ return true, fmt.Sprintf("conflict foreign key referenced table, one is %s, the other is %s", n.to.ReferencedTable, other.to.ReferencedTable)
+ }
+ if n.to.OnDelete != other.to.OnDelete {
+ return true, fmt.Sprintf("conflict foreign key on delete, one is %s, the other is %s", n.to.OnDelete, other.to.OnDelete)
+ }
+ if n.to.OnUpdate != other.to.OnUpdate {
+ return true, fmt.Sprintf("conflict foreign key on update, one is %s, the other is %s", n.to.OnUpdate, other.to.OnUpdate)
+ }
+ if !slices.Equal(n.to.Columns, other.to.Columns) {
+ return true, fmt.Sprintf("conflict foreign key columns, one is %v, the other is %v", n.to.Columns, other.to.Columns)
+ }
+ if !slices.Equal(n.to.ReferencedColumns, other.to.ReferencedColumns) {
+ return true, fmt.Sprintf("conflict foreign key referenced columns, one is %v, the other is %v", n.to.ReferencedColumns, other.to.ReferencedColumns)
}
- // TODO(zp): check update foreign key conflict
- return true, "not implemented yet"
- default:
- return true, fmt.Sprintf("non-expected node type pair, one is %T, the other is %T", n, other)
}
-}
+ if n.action == diffActionUpdate {
+ if other.from.ReferencedSchema != other.to.ReferencedSchema {
+ if n.from.ReferencedSchema != n.to.ReferencedSchema {
+ if n.to.ReferencedSchema != other.to.ReferencedSchema {
+ return true, fmt.Sprintf("conflict foreign key referenced schema, one is %s, the other is %s", n.from.ReferencedSchema, other.from.ReferencedSchema)
+ }
+ } else {
+ n.to.ReferencedSchema = other.to.ReferencedSchema
+ }
+ }
+
+ if other.from.ReferencedTable != other.to.ReferencedTable {
+ if n.from.ReferencedTable != n.to.ReferencedTable {
+ if n.to.ReferencedTable != other.to.ReferencedTable {
+ return true, fmt.Sprintf("conflict foreign key referenced table, one is %s, the other is %s", n.from.ReferencedTable, other.from.ReferencedTable)
+ }
+ } else {
+ n.to.ReferencedTable = other.to.ReferencedTable
+ }
+ }
+
+ if other.from.OnDelete != other.to.OnDelete {
+ if n.from.OnDelete != n.to.OnDelete {
+ if n.to.OnDelete != other.to.OnDelete {
+ return true, fmt.Sprintf("conflict foreign key on delete, one is %s, the other is %s", n.from.OnDelete, other.from.OnDelete)
+ }
+ } else {
+ n.to.OnDelete = other.to.OnDelete
+ }
+ }
+
+ if other.from.OnUpdate != other.to.OnUpdate {
+ if n.from.OnUpdate != n.to.OnUpdate {
+ if n.to.OnUpdate != other.to.OnUpdate {
+ return true, fmt.Sprintf("conflict foreign key on update, one is %s, the other is %s", n.from.OnUpdate, other.from.OnUpdate)
+ }
+ } else {
+ n.to.OnUpdate = other.to.OnUpdate
+ }
+ }
-func (n *metadataDiffForeignKeyNode) applyDiffTo(target proto.Message) error {
- tableTarget, ok := target.(*storepb.TableMetadata)
- if !ok {
- return errors.Errorf("target is not a table metadata, but %T", target)
+ if !reflect.DeepEqual(other.from.Columns, other.to.Columns) {
+ if !reflect.DeepEqual(n.from.Columns, n.to.Columns) {
+ if !reflect.DeepEqual(n.to.Columns, other.to.Columns) {
+ return true, fmt.Sprintf("conflict foreign key columns, one is %v, the other is %v", n.from.Columns, other.from.Columns)
+ }
+ } else {
+ n.to.Columns = other.to.Columns
+ }
+ }
}
+ return false, ""
+}
+
+func (n *metadataDiffForeignKeyNode) applyDiffTo(target *storepb.TableMetadata) error {
switch n.action {
case diffActionCreate:
- tableTarget.ForeignKeys = append(tableTarget.ForeignKeys, n.to)
+ target.ForeignKeys = append(target.ForeignKeys, n.to)
case diffActionDrop:
- for i, foreignKey := range tableTarget.ForeignKeys {
+ for i, foreignKey := range target.ForeignKeys {
if foreignKey.Name == n.name {
- tableTarget.ForeignKeys = append(tableTarget.ForeignKeys[:i], tableTarget.ForeignKeys[i+1:]...)
+ target.ForeignKeys = append(target.ForeignKeys[:i], target.ForeignKeys[i+1:]...)
break
}
}
case diffActionUpdate:
- for i, foreignKey := range tableTarget.ForeignKeys {
+ for i, foreignKey := range target.ForeignKeys {
if foreignKey.Name == n.name {
- tableTarget.ForeignKeys[i] = n.to
+ target.ForeignKeys[i] = n.to
break
}
}
@@ -350,235 +834,273 @@ func (n *metadataDiffForeignKeyNode) applyDiffTo(target proto.Message) error {
return nil
}
-func diffMetadata(from, to *storepb.DatabaseSchemaMetadata) (*metadataDiffRootNode, error) {
- if from == nil || to == nil {
- return nil, errors.New("from and to database metadata must not be nil")
+// diffMetadata returns the diff between base and head metadata, it always returns a non-nil root node if no error occurs.
+func diffMetadata(base, head *storepb.DatabaseSchemaMetadata) (*metadataDiffRootNode, error) {
+ if base == nil && head == nil {
+ return nil, errors.New("from and to metadata must not be nil")
}
root := &metadataDiffRootNode{
schemas: make(map[string]*metadataDiffSchemaNode),
}
- fromSchemaMap := make(map[string]*storepb.SchemaMetadata)
- for _, schema := range from.Schemas {
- fromSchemaMap[schema.Name] = schema
+ schemaNamesMap := make(map[string]bool)
+ baseSchemaMap := make(map[string]*storepb.SchemaMetadata)
+ if base != nil {
+ for _, schema := range base.Schemas {
+ baseSchemaMap[schema.Name] = schema
+ schemaNamesMap[schema.Name] = true
+ }
}
- for _, schema := range to.Schemas {
- schemaInFrom, in := fromSchemaMap[schema.Name]
- if !in {
- root.schemas[schema.Name] = &metadataDiffSchemaNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionCreate,
- },
- name: schema.Name,
- to: schema,
- }
- continue
+ headSchemaMap := make(map[string]*storepb.SchemaMetadata)
+ if head != nil {
+ for _, schema := range head.Schemas {
+ headSchemaMap[schema.Name] = schema
+ schemaNamesMap[schema.Name] = true
}
- diffNode, err := diffSchemaMetadata(schemaInFrom, schema)
+ }
+
+ for schemaName := range schemaNamesMap {
+ baseSchema, headSchema := baseSchemaMap[schemaName], headSchemaMap[schemaName]
+ diffNode, err := diffSchemaMetadata(baseSchema, headSchema)
if err != nil {
- return nil, errors.Wrapf(err, "failed to diff schema %q", schema.Name)
+ return nil, errors.Wrapf(err, "failed to diff schema %q", schemaName)
}
if diffNode != nil {
- root.schemas[schema.Name] = diffNode
+ root.schemas[schemaName] = diffNode
}
- delete(fromSchemaMap, schema.Name)
}
- for _, remainingSchema := range fromSchemaMap {
- root.schemas[remainingSchema.Name] = &metadataDiffSchemaNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionDrop,
- },
- name: remainingSchema.Name,
- from: remainingSchema,
- }
- }
return root, nil
}
-func diffSchemaMetadata(from, to *storepb.SchemaMetadata) (*metadataDiffSchemaNode, error) {
- if from == nil || to == nil {
- return nil, errors.New("from and to schema metadata must not be nil")
+func diffSchemaMetadata(base, head *storepb.SchemaMetadata) (*metadataDiffSchemaNode, error) {
+ if base == nil && head == nil {
+ return nil, nil
+ }
+ var name string
+ action := diffActionUpdate
+ if base == nil {
+ action = diffActionCreate
+ name = head.Name
+ } else if head == nil {
+ action = diffActionDrop
+ name = base.Name
+ } else {
+ name = base.Name
}
schemaNode := &metadataDiffSchemaNode{
- name: to.Name,
- from: from,
- to: to,
-
- tables: make(map[string]metadataDiffNode),
+ metadataDiffBaseNode: metadataDiffBaseNode{
+ action: action,
+ },
+ name: name,
+ base: base,
+ head: head,
+ tables: make(map[string]*metadataDiffTableNode),
}
- fromTableMap := make(map[string]*storepb.TableMetadata)
- for _, table := range from.Tables {
- fromTableMap[table.Name] = table
+ tableNamesMap := make(map[string]bool)
+
+ baseTableMap := make(map[string]*storepb.TableMetadata)
+ if base != nil {
+ for _, table := range base.Tables {
+ baseTableMap[table.Name] = table
+ tableNamesMap[table.Name] = true
+ }
}
- for _, table := range to.Tables {
- tableInFrom, in := fromTableMap[table.Name]
- if !in {
- schemaNode.tables[table.Name] = &metadataDiffTableNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionCreate,
- },
- name: table.Name,
- to: table,
- }
- continue
+ headTableMap := make(map[string]*storepb.TableMetadata)
+ if head != nil {
+ for _, table := range head.Tables {
+ headTableMap[table.Name] = table
+ tableNamesMap[table.Name] = true
}
- diffNode, err := diffTableMetadata(tableInFrom, table)
+ }
+
+ for tableName := range tableNamesMap {
+ baseTable, headTable := baseTableMap[tableName], headTableMap[tableName]
+ diffNode, err := diffTableMetadata(baseTable, headTable)
if err != nil {
- return nil, errors.Wrapf(err, "failed to diff table %q", table.Name)
+ return nil, errors.Wrapf(err, "failed to diff table %q", tableName)
}
if diffNode != nil {
- schemaNode.tables[table.Name] = diffNode
+ schemaNode.tables[tableName] = diffNode
}
- delete(fromTableMap, table.Name)
}
- for _, remainingTable := range fromTableMap {
- schemaNode.tables[remainingTable.Name] = &metadataDiffTableNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionDrop,
- },
- name: remainingTable.Name,
- from: remainingTable,
+ if action == diffActionUpdate {
+ if len(schemaNode.tables) == 0 {
+ return nil, nil
}
}
- if len(schemaNode.tables) > 0 {
- schemaNode.action = diffActionUpdate
- return schemaNode, nil
- }
- return nil, nil
+ return schemaNode, nil
}
-func diffTableMetadata(from, to *storepb.TableMetadata) (*metadataDiffTableNode, error) {
- if from == nil || to == nil {
+func diffTableMetadata(base, head *storepb.TableMetadata) (*metadataDiffTableNode, error) {
+ if base == nil && head == nil {
return nil, errors.New("from and to table metadata must not be nil")
}
- tableNode := &metadataDiffTableNode{
- name: to.Name,
- from: from,
- to: to,
-
- columns: make(map[string]metadataDiffNode),
- foreignKeys: make(map[string]metadataDiffNode),
+ var name string
+ action := diffActionUpdate
+ if base == nil {
+ action = diffActionCreate
+ name = head.Name
+ } else if head == nil {
+ action = diffActionDrop
+ name = base.Name
+ } else {
+ name = base.Name
}
- fromColumnMap := make(map[string]*storepb.ColumnMetadata)
- for _, column := range from.Columns {
- fromColumnMap[column.Name] = column
+ tableNode := &metadataDiffTableNode{
+ metadataDiffBaseNode: metadataDiffBaseNode{
+ action: action,
+ },
+ name: name,
+ from: base,
+ to: head,
+ columns: make(map[string]*metadataDiffColumnNode),
+ foreignKeys: make(map[string]*metadataDiffForeignKeyNode),
+ indexes: make(map[string]*metadataDiffIndexNode),
}
- fromForeignKeyMap := make(map[string]*storepb.ForeignKeyMetadata)
- for _, foreignKey := range from.ForeignKeys {
- fromForeignKeyMap[foreignKey.Name] = foreignKey
+
+ columnNamesMap := make(map[string]bool)
+
+ baseColumnMap := make(map[string]*storepb.ColumnMetadata)
+ if base != nil {
+ for _, column := range base.Columns {
+ baseColumnMap[column.Name] = column
+ columnNamesMap[column.Name] = true
+ }
}
- for _, column := range to.Columns {
- columnInFrom, in := fromColumnMap[column.Name]
- if !in {
- tableNode.columns[column.Name] = &metadataDiffColumnNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionCreate,
- },
- name: column.Name,
- to: column,
- }
- continue
+ headColumnMap := make(map[string]*storepb.ColumnMetadata)
+ if head != nil {
+ for _, column := range head.Columns {
+ headColumnMap[column.Name] = column
+ columnNamesMap[column.Name] = true
}
- diffNodeMeta, err := diffColumnMetadata(columnInFrom, column)
+ }
+
+ for columnName := range columnNamesMap {
+ baseColumn, headColumn := baseColumnMap[columnName], headColumnMap[columnName]
+ diffNode, err := diffColumnMetadata(baseColumn, headColumn)
if err != nil {
- return nil, errors.Wrapf(err, "failed to diff column %q", column.Name)
+ return nil, errors.Wrapf(err, "failed to diff column %q", columnName)
}
- if diffNodeMeta != nil {
- tableNode.columns[column.Name] = diffNodeMeta
+ if diffNode != nil {
+ tableNode.columns[columnName] = diffNode
}
- delete(fromColumnMap, column.Name)
}
- for _, foreignKey := range to.ForeignKeys {
- foreignKeyInFrom, in := fromForeignKeyMap[foreignKey.Name]
- if !in {
- tableNode.foreignKeys[foreignKey.Name] = &metadataDiffForeignKeyNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionCreate,
- },
- name: foreignKey.Name,
- to: foreignKey,
- }
- continue
+ foreignKeyNamesMap := make(map[string]bool)
+
+ baseForeignKeyMap := make(map[string]*storepb.ForeignKeyMetadata)
+ if base != nil {
+ for _, foreignKey := range base.ForeignKeys {
+ baseForeignKeyMap[foreignKey.Name] = foreignKey
+ foreignKeyNamesMap[foreignKey.Name] = true
+ }
+ }
+
+ headForeignKeyMap := make(map[string]*storepb.ForeignKeyMetadata)
+ if head != nil {
+ for _, foreignKey := range head.ForeignKeys {
+ headForeignKeyMap[foreignKey.Name] = foreignKey
+ foreignKeyNamesMap[foreignKey.Name] = true
}
- diffNodeMeta, err := diffForeignKeyMetadata(foreignKeyInFrom, foreignKey)
+ }
+
+ for foreignKeyName := range foreignKeyNamesMap {
+ baseForeignKey, headForeignKey := baseForeignKeyMap[foreignKeyName], headForeignKeyMap[foreignKeyName]
+ diffNode, err := diffForeignKeyMetadata(baseForeignKey, headForeignKey)
if err != nil {
- return nil, errors.Wrapf(err, "failed to diff foreign key %q", foreignKey.Name)
+ return nil, errors.Wrapf(err, "failed to diff foreign key %q", foreignKeyName)
}
- if diffNodeMeta != nil {
- tableNode.foreignKeys[foreignKey.Name] = diffNodeMeta
+ if diffNode != nil {
+ tableNode.foreignKeys[foreignKeyName] = diffNode
}
- delete(fromForeignKeyMap, foreignKey.Name)
}
- for _, remainingColumn := range fromColumnMap {
- tableNode.columns[remainingColumn.Name] = &metadataDiffColumnNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionDrop,
- },
- name: remainingColumn.Name,
- from: remainingColumn,
+ if action == diffActionUpdate {
+ if len(tableNode.columns) == 0 && len(tableNode.foreignKeys) == 0 {
+ return nil, nil
}
}
- for _, remainingForeignKey := range fromForeignKeyMap {
- tableNode.foreignKeys[remainingForeignKey.Name] = &metadataDiffForeignKeyNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionDrop,
- },
- name: remainingForeignKey.Name,
- from: remainingForeignKey,
- }
+ return tableNode, nil
+}
+
+func diffColumnMetadata(base, head *storepb.ColumnMetadata) (*metadataDiffColumnNode, error) {
+ if base == nil && head == nil {
+ return nil, errors.New("base and head column metadata cannot be nil both")
}
- if len(tableNode.columns) > 0 || len(tableNode.foreignKeys) > 0 {
- tableNode.action = diffActionUpdate
- return tableNode, nil
+ var name string
+ action := diffActionUpdate
+ if base == nil {
+ action = diffActionCreate
+ name = head.Name
+ } else if head == nil {
+ action = diffActionDrop
+ name = base.Name
+ } else {
+ name = base.Name
}
- return nil, nil
-}
-func diffColumnMetadata(from, to *storepb.ColumnMetadata) (*metadataDiffColumnNode, error) {
- if from == nil || to == nil {
- return nil, errors.New("from and to column metadata must not be nil")
+ columnNode := &metadataDiffColumnNode{
+ metadataDiffBaseNode: metadataDiffBaseNode{
+ action: action,
+ },
+ name: name,
+ from: base,
+ to: head,
}
- if !reflect.DeepEqual(from, to) {
- return &metadataDiffColumnNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionUpdate,
- },
- name: to.Name,
- from: from,
- }, nil
+ if action == diffActionUpdate {
+ if !reflect.DeepEqual(base, head) {
+ return columnNode, nil
+ }
+ return nil, nil
}
- return nil, nil
+ return columnNode, nil
}
-func diffForeignKeyMetadata(from, to *storepb.ForeignKeyMetadata) (*metadataDiffForeignKeyNode, error) {
- if from == nil || to == nil {
- return nil, errors.New("from and to foreign key metadata must not be nil")
+func diffForeignKeyMetadata(base, head *storepb.ForeignKeyMetadata) (*metadataDiffForeignKeyNode, error) {
+ if base == nil && head == nil {
+ return nil, errors.New("base and head foreign key metadata cannot be nil both")
}
- if !reflect.DeepEqual(from, to) {
- return &metadataDiffForeignKeyNode{
- metadataDiffBaseNode: metadataDiffBaseNode{
- action: diffActionUpdate,
- },
- name: to.Name,
- from: from,
- }, nil
+ var name string
+ action := diffActionUpdate
+ if base == nil {
+ action = diffActionCreate
+ name = head.Name
+ } else if head == nil {
+ action = diffActionDrop
+ name = base.Name
+ } else {
+ name = base.Name
+ }
+
+ fkNode := &metadataDiffForeignKeyNode{
+ metadataDiffBaseNode: metadataDiffBaseNode{
+ action: action,
+ },
+ name: name,
+ from: base,
+ to: head,
+ }
+
+ if action == diffActionUpdate {
+ if !reflect.DeepEqual(base, head) {
+ return fkNode, nil
+ }
+ return nil, nil
}
- return nil, nil
+ return fkNode, nil
}
diff --git a/backend/api/v1/schema_design_merge_test.go b/backend/api/v1/schema_design_merge_test.go
index 60313ebdbb..5a59e133f8 100644
--- a/backend/api/v1/schema_design_merge_test.go
+++ b/backend/api/v1/schema_design_merge_test.go
@@ -1,388 +1,76 @@
package v1
import (
+ "os"
+ "strings"
"testing"
+ "github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
- "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/testing/protocmp"
+ "gopkg.in/yaml.v3"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-func TestIsDiffConflict(t *testing.T) {
+func TestTryMerge(t *testing.T) {
type testCase struct {
- description string
- base *storepb.DatabaseSchemaMetadata
- head *storepb.DatabaseSchemaMetadata
- target *storepb.DatabaseSchemaMetadata
- want bool
- }
-
- defaultBase := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- },
- },
- },
+ Description string `yaml:"description"`
+ // Merge Head to Base with Ancestor as the common ancestor.
+ // Base is the base storepb.DatabaseSchemaMetadata protojson marshalled string.
+ Ancestor string `yaml:"ancestor"`
+ // Head is the head storepb.DatabaseSchemaMetadata protojson marshalled string.
+ Head string `yaml:"head"`
+ // Ancestor is the ancestor storepb.DatabaseSchemaMetadata protojson marshalled string.
+ Base string `yaml:"base"`
+ // Expected is the expected storepb.DatabaseSchemaMetadata protojson marshalled string.
+ Expected string `yaml:"expected"`
}
- testCases := []testCase{
- {
- description: "create different table with different name should not conflict",
- base: defaultBase,
- head: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- {
- Name: "salary",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "employee_id",
- Type: "int",
- },
- {
- Name: "amount",
- Type: "int",
- },
- },
- },
- },
- },
- },
- },
- target: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- {
- Name: "office",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "address",
- Type: "text",
- },
- },
- },
- },
- },
- },
- },
- want: false,
- },
- }
+ const record = false
a := require.New(t)
- for _, tc := range testCases {
- diffBetweenBaseAndHead, err := diffMetadata(tc.base, tc.head)
- a.NoError(err, tc.description)
- a.NotNil(diffBetweenBaseAndHead, tc.description)
- diffBetweenBaseAndTarget, err := diffMetadata(tc.base, tc.target)
- a.NoError(err, tc.description)
- a.NotNil(diffBetweenBaseAndTarget, tc.description)
+ testFilepath := "testdata/schema_design_merge/try_merge.yaml"
+ content, err := os.ReadFile(testFilepath)
+ a.NoError(err)
+ testCases := []testCase{}
+ err = yaml.Unmarshal(content, &testCases)
+ a.NoError(err)
- isConflict, _ := diffBetweenBaseAndTarget.isConflictWith(diffBetweenBaseAndHead)
- a.Equal(tc.want, isConflict, tc.description)
- }
-}
+ for idx, tc := range testCases {
+ ancestorSchemaMetadata := new(storepb.DatabaseSchemaMetadata)
+ err = protojson.Unmarshal([]byte(tc.Ancestor), ancestorSchemaMetadata)
+ a.NoErrorf(err, "test case %d: %s", idx, tc.Description)
+ headSchemaMetadata := new(storepb.DatabaseSchemaMetadata)
+ err = protojson.Unmarshal([]byte(tc.Head), headSchemaMetadata)
+ a.NoErrorf(err, "test case %d: %s", idx, tc.Description)
+ baseSchemaMetadata := new(storepb.DatabaseSchemaMetadata)
+ err := protojson.Unmarshal([]byte(tc.Base), baseSchemaMetadata)
+ a.NoErrorf(err, "test case %d: %s", idx, tc.Description)
+ expectedSchemaMetadata := new(storepb.DatabaseSchemaMetadata)
+ err = protojson.Unmarshal([]byte(tc.Expected), expectedSchemaMetadata)
+ a.NoErrorf(err, "test case %d: %s", idx, tc.Description)
+ mergedSchemaMetadata, err := tryMerge(ancestorSchemaMetadata, headSchemaMetadata, baseSchemaMetadata)
+ a.NoErrorf(err, "test case %d: %s", idx, tc.Description)
-func TestTryMerge(t *testing.T) {
- type testCase struct {
- description string
- base *storepb.DatabaseSchemaMetadata
- head *storepb.DatabaseSchemaMetadata
- target *storepb.DatabaseSchemaMetadata
- want *storepb.DatabaseSchemaMetadata
- }
-
- defaultBase := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- },
- },
- },
+ s := protojson.MarshalOptions{Multiline: true, Indent: " "}.Format(mergedSchemaMetadata)
+ a.NoErrorf(err, "test case %d: %s", idx, tc.Description)
+ if record {
+ testCases[idx].Expected = strings.TrimSpace(string(s))
+ } else if diff := cmp.Diff(expectedSchemaMetadata, mergedSchemaMetadata, protocmp.Transform()); diff != "" {
+ a.Failf("Failed", "mismatch (-want +got):\n%s", diff)
+ }
}
- testCases := []testCase{
- {
- description: "create different table with different name should not conflict",
- base: defaultBase,
- head: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- {
- Name: "salary",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "employee_id",
- Type: "int",
- },
- {
- Name: "amount",
- Type: "int",
- },
- },
- },
- },
- },
- },
- },
- target: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- {
- Name: "office",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "address",
- Type: "text",
- },
- },
- },
- },
- },
- },
- },
- want: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- },
- },
- {
- Name: "office",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "address",
- Type: "text",
- },
- },
- },
- {
- Name: "salary",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "employee_id",
- Type: "int",
- },
- {
- Name: "amount",
- Type: "int",
- },
- },
- },
- },
- },
- },
- },
- },
- {
- description: "add different column in the same table",
- base: defaultBase,
- head: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- {
- Name: "salary",
- Type: "int",
- },
- },
- },
- },
- },
- },
- },
- target: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- {
- Name: "phone",
- Type: "text",
- },
- },
- },
- },
- },
- },
- },
- want: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "",
- Tables: []*storepb.TableMetadata{
- {
- Name: "employees",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "int",
- },
- {
- Name: "name",
- Type: "text",
- },
- {
- Name: "phone",
- Type: "text",
- },
- {
- Name: "salary",
- Type: "int",
- },
- },
- },
- },
- },
- },
- },
- },
- }
-
- a := require.New(t)
- for _, tc := range testCases {
- got, err := tryMerge(tc.base, tc.head, tc.target)
- a.NoError(err, tc.description)
- a.NotNil(got, tc.description)
- equal := proto.Equal(tc.want, got)
- a.True(equal, tc.description)
+ if record {
+ f, err := os.OpenFile(testFilepath, os.O_WRONLY|os.O_TRUNC, 0644)
+ a.NoError(err)
+ defer f.Close()
+ content, err := yaml.Marshal(testCases)
+ a.NoError(err)
+ _, err = f.Write(content)
+ a.NoError(err)
}
}
diff --git a/backend/api/v1/testdata/schema_design_merge/try_merge.yaml b/backend/api/v1/testdata/schema_design_merge/try_merge.yaml
new file mode 100644
index 0000000000..d6cd6ffb23
--- /dev/null
+++ b/backend/api/v1/testdata/schema_design_merge/try_merge.yaml
@@ -0,0 +1,611 @@
+- description: |-
+ Complex Case:
+ Head:
+ - Table `employee`:
+ - DROP COLUMN `name`.
+ - ADD COLUMN `gender char(1)`.
+ - ADD COLUMN `credit_card_number VARCHAR(255)`.
+ - Table `salary`:
+ - DROP TABLE `salary`.
+ - Table `address`:
+ - CREATE TABLE `address` (id INT PRIMARY KEY, employee_id INT REFERENCES employee(id)).
+
+ Base:
+ - Table `employee`:
+ - ADD COLUMN `gender char(1)`.
+ - ADD COLUMN `debit_card_number VARCHAR(255)`.
+ - Table `salary`:
+ - NO CHANGE.
+ - Table `hiring_date`:
+ - CREATE TABLE `hiring_date`(id INT, employee_id INT).
+ - Table `address`:
+ - CREATE TABLE `address` (id INT PRIMARY KEY, address VARCHAR(255), employee_id INT REFERENCES employee(id)).
+
+ What we expect:
+ - Table `employee`:
+ - DROP COLUMN `name`.
+ - ADD COLUMN `credit_card_number VARCHAR(255)`.
+ - ADD COLUMN `debit_card_number VARCHAR(255)`.
+ - ADD COLUMN `gender char(1)`.
+ - Table `salary`:
+ - DROP TABLE `salary`.
+ - Table `hiring_date`:
+ - CREATE TABLE `hiring_date`(id INT, employee_id INT).
+ - Table `address`:
+ - CREATE TABLE `address` (id INT PRIMARY KEY, employee_id INT REFERENCES employee(id), address VARCHAR(255)).
+ ancestor: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "name",
+ "position": 2,
+ "type": "varchar(255)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id"
+ ],
+ "type": "btree",
+ "unique": true,
+ "primary": true
+ }
+ ],
+ "indexSize": "8192"
+ },
+ {
+ "name": "salary",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ head: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "gender",
+ "position": 2,
+ "type": "char(1)"
+ },
+ {
+ "name": "credit_card_number",
+ "position": 3,
+ "type": "varchar(255)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id"
+ ],
+ "type": "btree",
+ "unique": true,
+ "primary": true
+ }
+ ],
+ "indexSize": "8192"
+ },
+ {
+ "name": "address",
+ "columns": [
+ {
+ "name": "id",
+ "type": "integer"
+ },
+ {
+ "name": "employee_id",
+ "type": "integer"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id"
+ ],
+ "primary": true
+ }
+ ],
+ "foreignKeys": [
+ {
+ "name": "address-fk-sc4rcmkp",
+ "columns": [
+ "employee_id"
+ ],
+ "referencedSchema": "public",
+ "referencedTable": "employee",
+ "referencedColumns": [
+ "id"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ base: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "name",
+ "position": 2,
+ "type": "varchar(255)"
+ },
+ {
+ "name": "gender",
+ "position": 3,
+ "type": "char(1)"
+ },
+ {
+ "name": "debit_card_number",
+ "position": 4,
+ "type": "varchar(255)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id"
+ ],
+ "type": "btree",
+ "unique": true,
+ "primary": true
+ }
+ ],
+ "indexSize": "8192"
+ },
+ {
+ "name": "salary",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ },
+ {
+ "name": "hiring_date",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "employee_id",
+ "position": 2,
+ "type": "integer"
+ }
+ ]
+ },
+ {
+ "name": "address",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "address",
+ "position": 2,
+ "type": "varchar(255)"
+ },
+ {
+ "name": "employee_id",
+ "position": 3,
+ "type": "integer"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id"
+ ],
+ "primary": true
+ }
+ ],
+ "foreignKeys": [
+ {
+ "name": "address-fk-sc4rcmkp",
+ "columns": [
+ "employee_id"
+ ],
+ "referencedSchema": "public",
+ "referencedTable": "employee",
+ "referencedColumns": [
+ "id"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ expected: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "credit_card_number",
+ "position": 2,
+ "type": "varchar(255)"
+ },
+ {
+ "name": "debit_card_number",
+ "position": 3,
+ "type": "varchar(255)"
+ },
+ {
+ "name": "gender",
+ "position": 4,
+ "type": "char(1)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "id"
+ ],
+ "type": "btree",
+ "unique": true,
+ "primary": true
+ }
+ ]
+ },
+ {
+ "name": "address",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "employee_id",
+ "position": 2,
+ "type": "integer"
+ },
+ {
+ "name": "address",
+ "position": 3,
+ "type": "varchar(255)"
+ }
+ ],
+ "foreignKeys": [
+ {
+ "name": "address-fk-sc4rcmkp",
+ "columns": [
+ "employee_id"
+ ],
+ "referencedSchema": "public",
+ "referencedTable": "employee",
+ "referencedColumns": [
+ "id"
+ ]
+ }
+ ]
+ },
+ {
+ "name": "hiring_date",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "employee_id",
+ "position": 2,
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+- description: Add different column on the same table
+ ancestor: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ head: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "name",
+ "position": 2,
+ "type": "varchar(255)"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ base: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "gender",
+ "position": 2,
+ "type": "char(1)"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ expected: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ },
+ {
+ "name": "gender",
+ "position": 2,
+ "type": "char(1)"
+ },
+ {
+ "name": "name",
+ "position": 3,
+ "type": "varchar(255)"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+- description: Create different table
+ ancestor: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ head: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ },
+ {
+ "name": "address",
+ "columns": [
+ {
+ "name": "id",
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ base: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ },
+ {
+ "name": "salary",
+ "columns": [
+ {
+ "name": "id",
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }
+ expected: |-
+ {
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "employee",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ },
+ {
+ "name": "address",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ },
+ {
+ "name": "salary",
+ "columns": [
+ {
+ "name": "id",
+ "position": 1,
+ "type": "integer"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "characterSet": "UTF8",
+ "collation": "en_US.utf8"
+ }