Skip to content

Commit

Permalink
sql/postgres: unexport internal and temporary EnumType struct (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
masseelch authored Jan 20, 2022
1 parent 4306a49 commit f752c3e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions sql/postgres/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (d *diff) typeChanged(from, to *schema.Column) (bool, error) {
*schema.IntegerType, *schema.JSONType, *schema.SpatialType, *schema.StringType,
*schema.TimeType, *BitType, *NetworkType, *UserDefinedType:
changed = mustFormat(toT) != mustFormat(fromT)
case *EnumType:
case *enumType:
toT := toT.(*schema.EnumType)
changed = fromT.T != toT.T || !sqlx.ValuesEqual(fromT.Values, toT.Values)
case *schema.EnumType:
Expand Down Expand Up @@ -250,7 +250,7 @@ func (d *diff) normalize(table *schema.Table) {
// is equivalent to character(1).
t.Size = 1
}
case *EnumType:
case *enumType:
c.Type.Type = &schema.EnumType{T: t.T, Values: t.Values}
case *SerialType:
// The definition of "<column> <serial type>" is equivalent to specifying:
Expand Down
11 changes: 6 additions & 5 deletions sql/postgres/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func columnType(c *columnDesc) schema.Type {
// values are filled in batch after the rows above is closed.
// https://www.postgresql.org/docs/current/catalog-pg-type.html
if c.typtype == "e" {
typ = &EnumType{T: c.udt, ID: c.typid}
typ = &enumType{T: c.udt, ID: c.typid}
}
default:
typ = &schema.UnsupportedType{T: t}
Expand All @@ -293,15 +293,15 @@ func (i *inspect) enumValues(ctx context.Context, columns []*schema.Column) erro
query = "SELECT enumtypid, enumlabel FROM pg_enum WHERE enumtypid IN ("
)
for _, c := range columns {
if enum, ok := c.Type.Type.(*EnumType); ok {
if enum, ok := c.Type.Type.(*enumType); ok {
if _, ok := ids[enum.ID]; !ok {
if len(args) > 0 {
query += ", "
}
args = append(args, enum.ID)
query += fmt.Sprintf("$%d", len(args))
}
// Convert the intermediate type to the standard schema.EnumType.
// Convert the intermediate type to the standard schema.enumType.
e := &schema.EnumType{T: enum.T}
c.Type.Type = e
c.Type.Raw = enum.T
Expand Down Expand Up @@ -587,8 +587,9 @@ type (
T string
}

// EnumType represents an enum type.
EnumType struct {
// enumType represents an enum type. It serves aa intermediate representation of a Postgres enum type,
// to temporary save TypeID and TypeName of an enum column until the enum values can be extracted.
enumType struct {
schema.Type
T string // Type name.
ID int64 // Type id.
Expand Down
2 changes: 1 addition & 1 deletion sql/postgres/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func convertEnums(tbls []*sqlspec.Table, enums []*Enum, sch *schema.Schema) erro
if !ok {
return fmt.Errorf("postgrs: column %q not found in table %q", col.Name, t.Name)
}
c.Type.Type = &EnumType{
c.Type.Type = &schema.EnumType{
T: e.Name,
Values: e.Values,
}
Expand Down
2 changes: 1 addition & 1 deletion sql/postgres/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ enum "account_type" {
{
Name: "type",
Type: &schema.ColumnType{
Type: &EnumType{
Type: &schema.EnumType{
T: "account_type",
Values: []string{"private", "business"},
},
Expand Down

0 comments on commit f752c3e

Please sign in to comment.