Skip to content

Commit

Permalink
fix(mysql): correct default value handling for VARCHAR columns in Ini… (
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentRebs authored Oct 11, 2024
1 parent af6c82e commit 09c999d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
46 changes: 45 additions & 1 deletion backend/pkg/sqlmanager/mysql/mysql-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"golang.org/x/sync/errgroup"
)

const (
columnDefaultDefault = "Default"
columnDefaultString = "String"
)

type MysqlManager struct {
querier mysql_queries.Querier
pool mysql_queries.DBTX
Expand All @@ -39,10 +44,21 @@ func (m *MysqlManager) GetDatabaseSchema(ctx context.Context) ([]*sqlmanager_sha
generatedTypeCopy := row.Extra.String
generatedType = &generatedTypeCopy
}

columnDefaultStr, err := convertUInt8ToString(row.ColumnDefault)
if err != nil {
return nil, err
}

var columnDefaultType *string
if row.Extra.Valid && columnDefaultStr != "" && row.Extra.String == "" {
val := columnDefaultString // With this type columnDefaultStr will be surrounded by quotes when translated to SQL
columnDefaultType = &val
} else if row.Extra.Valid && columnDefaultStr != "" && row.Extra.String != "" {
val := columnDefaultDefault // With this type columnDefaultStr will be surrounded by parentheses when translated to SQL
columnDefaultType = &val
}

charMaxLength := -1
if row.CharacterMaximumLength.Valid {
charMaxLength = int(row.CharacterMaximumLength.Int64)
Expand All @@ -68,6 +84,7 @@ func (m *MysqlManager) GetDatabaseSchema(ctx context.Context) ([]*sqlmanager_sha
ColumnName: row.ColumnName,
DataType: row.DataType,
ColumnDefault: columnDefaultStr,
ColumnDefaultType: columnDefaultType,
IsNullable: row.IsNullable,
GeneratedType: generatedType,
CharacterMaximumLength: charMaxLength,
Expand Down Expand Up @@ -285,6 +302,19 @@ func (m *MysqlManager) GetTableInitStatements(ctx context.Context, tables []*sql
if err != nil {
return nil, err
}
var columnDefaultType *string
if identityType != nil && columnDefaultStr != "" && *identityType == "" {
val := columnDefaultString // With this type columnDefaultStr will be surrounded by quotes when translated to SQL
columnDefaultType = &val
} else if identityType != nil && columnDefaultStr != "" && *identityType != "" {
val := columnDefaultDefault // With this type columnDefaultStr will be surrounded by parentheses when translated to SQL
columnDefaultType = &val
}
columnDefaultStr, err = EscapeMysqlDefaultColumn(columnDefaultStr, columnDefaultType)
if err != nil {
return nil, err
}

genExp, err := convertUInt8ToString(record.GenerationExp)
if err != nil {
return nil, err
Expand Down Expand Up @@ -354,7 +384,7 @@ func buildTableCol(record *buildTableColRequest) string {
}

if record.ColumnDefault != "" {
pieces = append(pieces, fmt.Sprintf("DEFAULT (%s)", record.ColumnDefault))
pieces = append(pieces, fmt.Sprintf("DEFAULT %s", record.ColumnDefault))
}

if record.IdentityType != nil && *record.IdentityType == "auto_increment" {
Expand Down Expand Up @@ -797,6 +827,20 @@ func EscapeMysqlColumn(col string) string {
return fmt.Sprintf("`%s`", col)
}

func EscapeMysqlDefaultColumn(defaultColumnValue string, defaultColumnType *string) (string, error) {
defaultColumnTypes := []string{columnDefaultString, columnDefaultDefault}
if defaultColumnType == nil {
return defaultColumnValue, nil
}
if *defaultColumnType == columnDefaultString {
return fmt.Sprintf("'%s'", defaultColumnValue), nil
}
if *defaultColumnType == columnDefaultDefault {
return fmt.Sprintf("(%s)", defaultColumnValue), nil
}
return fmt.Sprintf("(%s)", defaultColumnValue), fmt.Errorf("unsupported default column type: %s, currently supported types are: %v", *defaultColumnType, defaultColumnTypes)
}

func GetMysqlColumnOverrideAndResetProperties(columnInfo *sqlmanager_shared.ColumnInfo) (needsOverride, needsReset bool) {
needsOverride = false
needsReset = false
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/sqlmanager/shared/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type DatabaseSchemaRow struct {
ColumnName string
DataType string
ColumnDefault string
ColumnDefaultType *string
IsNullable string
CharacterMaximumLength int
NumericPrecision int
Expand Down

0 comments on commit 09c999d

Please sign in to comment.