Added diff to the tool
Some checks are pending
CI / Test (1.25) (push) Waiting to run
CI / Lint (push) Waiting to run
CI / Build (push) Waiting to run
CI / Test (1.23) (push) Waiting to run
CI / Test (1.24) (push) Waiting to run

This commit is contained in:
Hein
2025-12-18 13:38:32 +02:00
parent bed4f5d3bd
commit fcbceaf434
10 changed files with 1454 additions and 31 deletions

View File

@@ -212,7 +212,7 @@ func (r *Reader) getReceiverType(expr ast.Expr) string {
}
// parseTableNameMethod parses a TableName() method and extracts the table and schema name
func (r *Reader) parseTableNameMethod(funcDecl *ast.FuncDecl) (string, string) {
func (r *Reader) parseTableNameMethod(funcDecl *ast.FuncDecl) (tableName string, schemaName string) {
if funcDecl.Body == nil {
return "", ""
}
@@ -347,7 +347,7 @@ func (r *Reader) isRelationship(tag string) bool {
}
// extractTableNameFromTag extracts table and schema from bun tag
func (r *Reader) extractTableNameFromTag(tag string) (string, string) {
func (r *Reader) extractTableNameFromTag(tag string) (tableName string, schemaName string) {
// Extract bun tag value
re := regexp.MustCompile(`bun:"table:([^"]+)"`)
matches := re.FindStringSubmatch(tag)
@@ -439,6 +439,7 @@ func (r *Reader) parseColumn(fieldName string, fieldType ast.Expr, tag string, s
if !strings.Contains(bunTag, "notnull") {
// If notnull is not explicitly set, it might still be nullable
// This is a heuristic - we default to nullable unless specified
return column
}
}
@@ -457,16 +458,18 @@ func (r *Reader) extractBunTag(tag string) string {
// parseTypeWithLength parses a type string and extracts length if present
// e.g., "varchar(255)" returns ("varchar", 255)
func (r *Reader) parseTypeWithLength(typeStr string) (string, int) {
func (r *Reader) parseTypeWithLength(typeStr string) (baseType string, length int) {
// Check for type with length: varchar(255), char(10), etc.
re := regexp.MustCompile(`^([a-zA-Z\s]+)\((\d+)\)$`)
matches := re.FindStringSubmatch(typeStr)
if len(matches) == 3 {
length := 0
fmt.Sscanf(matches[2], "%d", &length)
return strings.TrimSpace(matches[1]), length
if _, err := fmt.Sscanf(matches[2], "%d", &length); err == nil {
baseType = strings.TrimSpace(matches[1])
return
}
}
return typeStr, 0
baseType = typeStr
return
}
// goTypeToSQL maps Go types to SQL types

View File

@@ -344,11 +344,12 @@ func (r *Reader) parseIndex(line, tableName, schemaName string) *models.Index {
for _, col := range strings.Split(columnsStr, ",") {
columns = append(columns, stripQuotes(strings.TrimSpace(col)))
}
} else {
} else if strings.Contains(line, "[") {
// Single column format: columnname [attributes]
// Extract column name before the bracket
if strings.Contains(line, "[") {
colName := strings.TrimSpace(line[:strings.Index(line, "[")])
idx := strings.Index(line, "[")
if idx > 0 {
colName := strings.TrimSpace(line[:idx])
if colName != "" {
columns = []string{stripQuotes(colName)}
}

View File

@@ -212,7 +212,7 @@ func (r *Reader) getReceiverType(expr ast.Expr) string {
}
// parseTableNameMethod parses a TableName() method and extracts the table and schema name
func (r *Reader) parseTableNameMethod(funcDecl *ast.FuncDecl) (string, string) {
func (r *Reader) parseTableNameMethod(funcDecl *ast.FuncDecl) (tableName string, schemaName string) {
if funcDecl.Body == nil {
return "", ""
}
@@ -341,12 +341,12 @@ func (r *Reader) isGORMModel(field *ast.Field) bool {
func (r *Reader) isRelationship(tag string) bool {
gormTag := r.extractGormTag(tag)
return strings.Contains(gormTag, "foreignKey:") ||
strings.Contains(gormTag, "references:") ||
strings.Contains(gormTag, "many2many:")
strings.Contains(gormTag, "references:") ||
strings.Contains(gormTag, "many2many:")
}
// extractTableFromGormTag extracts table and schema from gorm tag
func (r *Reader) extractTableFromGormTag(tag string) (string, string) {
func (r *Reader) extractTableFromGormTag(tag string) (tablename string, schemaName string) {
// This is typically set via TableName() method, not in tags
// We'll return empty strings and rely on deriveTableName
return "", ""
@@ -439,12 +439,12 @@ func (r *Reader) extractGormTag(tag string) string {
// parseTypeWithLength parses a type string and extracts length if present
// e.g., "varchar(255)" returns ("varchar", 255)
func (r *Reader) parseTypeWithLength(typeStr string) (string, int) {
func (r *Reader) parseTypeWithLength(typeStr string) (baseType string, length int) {
// Check for type with length: varchar(255), char(10), etc.
// Also handle precision/scale: numeric(10,2)
if strings.Contains(typeStr, "(") {
idx := strings.Index(typeStr, "(")
baseType := strings.TrimSpace(typeStr[:idx])
baseType = strings.TrimSpace(typeStr[:idx])
// Extract numbers from parentheses
parens := typeStr[idx+1:]
@@ -454,12 +454,13 @@ func (r *Reader) parseTypeWithLength(typeStr string) (string, int) {
// For now, just handle single number (length)
if !strings.Contains(parens, ",") {
length := 0
fmt.Sscanf(parens, "%d", &length)
return baseType, length
if _, err := fmt.Sscanf(parens, "%d", &length); err == nil {
return
}
}
}
return typeStr, 0
baseType = typeStr
return
}
// parseGormTag parses a gorm tag string into a map