Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions internal/sql/catalog/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,31 @@ func (c *Catalog) alterTypeAddValue(stmt *ast.AlterTypeAddValueStmt) error {
}
}

insertIndex := len(enum.Vals)
if stmt.NewValHasNeighbor {
insertIndex := -1
foundNeighbor := false
for i, val := range enum.Vals {
if val == *stmt.NewValNeighbor {
if stmt.NewValIsAfter {
insertIndex = i + 1
} else {
insertIndex = i
}
foundNeighbor = true
break
}
}

if insertIndex == -1 {
if !foundNeighbor {
return fmt.Errorf("enum %s unable to find existing neighbor value %s for new value %s", enum.Name, *stmt.NewValNeighbor, *stmt.NewValue)
}
}

if insertIndex == len(enum.Vals) {
enum.Vals = append(enum.Vals, *stmt.NewValue)
} else {
enum.Vals = append(enum.Vals[:insertIndex+1], enum.Vals[insertIndex:]...)
enum.Vals[insertIndex] = *stmt.NewValue
}
} else {
if insertIndex == len(enum.Vals) {
enum.Vals = append(enum.Vals, *stmt.NewValue)
} else {
enum.Vals = append(enum.Vals[:insertIndex+1], enum.Vals[insertIndex:]...)
enum.Vals[insertIndex] = *stmt.NewValue
}

return nil
Expand Down