feat(migration): add fallback for column type conversion failures
* implement renaming of old column and adding new column on conversion failure * update templates and tests to support new behavior
This commit is contained in:
@@ -98,6 +98,18 @@ type AlterColumnTypeData struct {
|
||||
UsingExpr string
|
||||
}
|
||||
|
||||
// AlterColumnTypeWithFallbackData contains data for the alter column type
|
||||
// template that falls back to renaming the old column and adding a fresh
|
||||
// one when the in-place type conversion is not possible.
|
||||
type AlterColumnTypeWithFallbackData struct {
|
||||
SchemaName string
|
||||
TableName string
|
||||
ColumnName string
|
||||
NewType string
|
||||
UsingExpr string
|
||||
OldColumnName string
|
||||
}
|
||||
|
||||
type AlterColumnTypeWithCheckData struct {
|
||||
SchemaName string
|
||||
TableName string
|
||||
@@ -320,6 +332,17 @@ func (te *TemplateExecutor) ExecuteAlterColumnType(data AlterColumnTypeData) (st
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// ExecuteAlterColumnTypeWithFallback executes the alter column type template
|
||||
// that renames the old column and adds a new one when the conversion fails.
|
||||
func (te *TemplateExecutor) ExecuteAlterColumnTypeWithFallback(data AlterColumnTypeWithFallbackData) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
err := te.templates.ExecuteTemplate(&buf, "alter_column_type_with_fallback.tmpl", data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to execute alter_column_type_with_fallback template: %w", err)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func (te *TemplateExecutor) ExecuteAlterColumnTypeWithCheck(data AlterColumnTypeWithCheckData) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
err := te.templates.ExecuteTemplate(&buf, "alter_column_type_with_check.tmpl", data)
|
||||
|
||||
Reference in New Issue
Block a user