-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert SQL statements of the form: ```sql ALTER TABLE foo ALTER COLUMN a [SET DATA] TYPE text ``` to the equivalent `pgroll` migration: ```json [ { "alter_column": { "column": "a", "down": "TODO: Implement SQL data migration", "table": "foo", "type": "text", "up": "TODO: Implement SQL data migration" } } ] ``` Part of #504
- Loading branch information
1 parent
84661eb
commit c756988
Showing
5 changed files
with
102 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sql2pgroll | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
pgq "github.com/pganalyze/pg_query_go/v6" | ||
) | ||
|
||
// convertTypeName converts a TypeName node to a string. | ||
func convertTypeName(typeName *pgq.TypeName) string { | ||
ignoredTypeParts := map[string]bool{ | ||
"pg_catalog": true, | ||
} | ||
|
||
// Build the type name, including any schema qualifiers | ||
typeParts := make([]string, 0, len(typeName.Names)) | ||
for _, node := range typeName.Names { | ||
typePart := node.GetString_().GetSval() | ||
if _, ok := ignoredTypeParts[typePart]; ok { | ||
continue | ||
} | ||
typeParts = append(typeParts, typePart) | ||
} | ||
|
||
// Build the type modifiers, such as precision and scale for numeric types | ||
var typeMods []string | ||
for _, node := range typeName.Typmods { | ||
if x, ok := node.GetAConst().Val.(*pgq.A_Const_Ival); ok { | ||
typeMods = append(typeMods, fmt.Sprintf("%d", x.Ival.GetIval())) | ||
} | ||
} | ||
var typeModifier string | ||
if len(typeMods) > 0 { | ||
typeModifier = fmt.Sprintf("(%s)", strings.Join(typeMods, ",")) | ||
} | ||
|
||
// Build the array bounds for array types | ||
var arrayBounds string | ||
for _, node := range typeName.ArrayBounds { | ||
bound := node.GetInteger().GetIval() | ||
if bound == -1 { | ||
arrayBounds = "[]" | ||
} else { | ||
arrayBounds = fmt.Sprintf("%s[%d]", arrayBounds, bound) | ||
} | ||
} | ||
|
||
return strings.Join(typeParts, ".") + typeModifier + arrayBounds | ||
} |