feat(merge): enhance type conflict detection for columns
* Introduced extractTypeParts function to handle embedded dimensions in type strings. * Updated columnTypeConflict to utilize new type extraction logic. * Improved PostgreSQL type normalization and handling in various components.
This commit is contained in:
+36
-4
@@ -5,9 +5,11 @@ package merge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/pgsql"
|
||||
)
|
||||
|
||||
// MergeResult represents the result of a merge operation
|
||||
@@ -449,10 +451,40 @@ func columnTypeConflict(target, source *models.Column) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
return normalizeType(target.Type) != normalizeType(source.Type) ||
|
||||
target.Length != source.Length ||
|
||||
target.Precision != source.Precision ||
|
||||
target.Scale != source.Scale
|
||||
tType, tLen, tPrec, tScale := extractTypeParts(target)
|
||||
sType, sLen, sPrec, sScale := extractTypeParts(source)
|
||||
|
||||
return tType != sType || tLen != sLen || tPrec != sPrec || tScale != sScale
|
||||
}
|
||||
|
||||
// extractTypeParts returns the canonical base type and dimensions for a column,
|
||||
// handling the case where dimensions are embedded in the type string (e.g. "char(2)")
|
||||
// rather than stored in the separate Length/Precision/Scale fields.
|
||||
func extractTypeParts(col *models.Column) (baseType string, length, precision, scale int) {
|
||||
typeName := strings.ToLower(strings.TrimSpace(col.Type))
|
||||
length, precision, scale = col.Length, col.Precision, col.Scale
|
||||
|
||||
if idx := strings.Index(typeName, "("); idx >= 0 {
|
||||
inner := strings.TrimRight(strings.TrimSpace(typeName[idx+1:]), ")")
|
||||
typeName = strings.TrimSpace(typeName[:idx])
|
||||
parts := strings.Split(inner, ",")
|
||||
if len(parts) == 2 {
|
||||
if p, err := strconv.Atoi(strings.TrimSpace(parts[0])); err == nil && p > 0 && precision == 0 {
|
||||
precision = p
|
||||
}
|
||||
if s, err := strconv.Atoi(strings.TrimSpace(parts[1])); err == nil && s > 0 && scale == 0 {
|
||||
scale = s
|
||||
}
|
||||
} else if len(parts) == 1 {
|
||||
if l, err := strconv.Atoi(strings.TrimSpace(parts[0])); err == nil && l > 0 && length == 0 && precision == 0 {
|
||||
length = l
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typeName = pgsql.NormalizePGType(typeName)
|
||||
|
||||
return typeName, length, precision, scale
|
||||
}
|
||||
|
||||
func normalizeType(value string) string {
|
||||
|
||||
Reference in New Issue
Block a user