Skip to content

Implement support for enum ordering #2111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: GetAll :many
SELECT * FROM foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TYPE enum_type AS ENUM ('first', 'last');
ALTER TYPE enum_type ADD VALUE 'afterlast' AFTER 'last';
ALTER TYPE enum_type ADD VALUE 'third' AFTER 'first';
ALTER TYPE enum_type ADD VALUE 'fourth' BEFORE 'last';
ALTER TYPE enum_type ADD VALUE 'fifth' AFTER 'fourth';
ALTER TYPE enum_type ADD VALUE 'second' BEFORE 'third';
ALTER TYPE enum_type ADD VALUE 'beforefirst' BEFORE 'first';

CREATE TABLE foo (
id SERIAL PRIMARY KEY
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "2",
"sql": [
{
"engine": "postgresql",
"schema": "schema.sql",
"queries": "query.sql",
"gen": {
"go": {
"out" : "go",
"package" : "db",
"emit_interface": true,
"emit_all_enum_values": true,
"emit_enum_valid_method": true
}
}
}
]
}
3 changes: 3 additions & 0 deletions internal/engine/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func translate(node *nodes.Node) (ast.Node, error) {
return &ast.AlterTypeAddValueStmt{
Type: rel.TypeName(),
NewValue: makeString(n.NewVal),
NewValHasNeighbor: len(n.NewValNeighbor) > 0,
NewValNeighbor: makeString(n.NewValNeighbor),
NewValIsAfter: n.NewValIsAfter,
SkipIfNewValExists: n.SkipIfNewValExists,
}, nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/sql/ast/alter_type_add_value_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ast
type AlterTypeAddValueStmt struct {
Type *TypeName
NewValue *string
NewValHasNeighbor bool
NewValNeighbor *string
NewValIsAfter bool
SkipIfNewValExists bool
}

Expand Down
38 changes: 32 additions & 6 deletions internal/sql/catalog/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package catalog
import (
"errors"
"fmt"

"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
)
Expand Down Expand Up @@ -197,20 +196,47 @@ func (c *Catalog) alterTypeAddValue(stmt *ast.AlterTypeAddValueStmt) error {
return fmt.Errorf("type is not an enum: %T", stmt.Type)
}

newIndex := -1
existingIndex := -1
for i, val := range enum.Vals {
if val == *stmt.NewValue {
newIndex = i
existingIndex = i
}
}
if newIndex >= 0 {

if existingIndex >= 0 {
if !stmt.SkipIfNewValExists {
return fmt.Errorf("type %T already has value %s", stmt.Type, *stmt.NewValue)
return fmt.Errorf("enum %s already has value %s", enum.Name, *stmt.NewValue)
} else {
return nil
}
}
enum.Vals = append(enum.Vals, *stmt.NewValue)

if stmt.NewValHasNeighbor {
insertIndex := -1
for i, val := range enum.Vals {
if val == *stmt.NewValNeighbor {
if stmt.NewValIsAfter {
insertIndex = i + 1
} else {
insertIndex = i
}
}
}

if insertIndex == -1 {
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 {
enum.Vals = append(enum.Vals, *stmt.NewValue)
}

return nil
}

Expand Down