MCPcopy Create free account
hub / github.com/bytebase/bytebase / compareCheckConstraints

Function compareCheckConstraints

backend/plugin/schema/differ.go:1102–1147  ·  view source on GitHub ↗

compareCheckConstraints compares two lists of check constraints.

(engine storepb.Engine, oldChecks, newChecks []*storepb.CheckConstraintMetadata)

Source from the content-addressed store, hash-verified

1100
1101// compareCheckConstraints compares two lists of check constraints.
1102func compareCheckConstraints(engine storepb.Engine, oldChecks, newChecks []*storepb.CheckConstraintMetadata) []*CheckConstraintDiff {
1103 var changes []*CheckConstraintDiff
1104
1105 oldCheckMap := make(map[string]*storepb.CheckConstraintMetadata)
1106 for _, check := range oldChecks {
1107 oldCheckMap[check.Name] = check
1108 }
1109
1110 newCheckMap := make(map[string]*storepb.CheckConstraintMetadata)
1111 for _, check := range newChecks {
1112 newCheckMap[check.Name] = check
1113 }
1114
1115 // Check for dropped check constraints
1116 for checkName, oldCheck := range oldCheckMap {
1117 if _, exists := newCheckMap[checkName]; !exists {
1118 changes = append(changes, &CheckConstraintDiff{
1119 Action: MetadataDiffActionDrop,
1120 OldCheckConstraint: oldCheck,
1121 })
1122 }
1123 }
1124
1125 // Check for new and modified check constraints
1126 for checkName, newCheck := range newCheckMap {
1127 oldCheck, exists := oldCheckMap[checkName]
1128 if !exists {
1129 changes = append(changes, &CheckConstraintDiff{
1130 Action: MetadataDiffActionCreate,
1131 NewCheckConstraint: newCheck,
1132 })
1133 } else if !checkConstraintsEqual(engine, oldCheck, newCheck) {
1134 // Drop the old constraint and recreate the new one instead of altering
1135 changes = append(changes, &CheckConstraintDiff{
1136 Action: MetadataDiffActionDrop,
1137 OldCheckConstraint: oldCheck,
1138 })
1139 changes = append(changes, &CheckConstraintDiff{
1140 Action: MetadataDiffActionCreate,
1141 NewCheckConstraint: newCheck,
1142 })
1143 }
1144 }
1145
1146 return changes
1147}
1148
1149// checkConstraintsEqual checks if two check constraints are equal.
1150func checkConstraintsEqual(engine storepb.Engine, check1, check2 *storepb.CheckConstraintMetadata) bool {

Callers 1

compareTableDetailsFunction · 0.70

Calls 1

checkConstraintsEqualFunction · 0.85

Tested by

no test coverage detected