Skip to content

Commit

Permalink
chore(indexer/postgres): update to changes on main (#21077)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Jul 25, 2024
1 parent 683371f commit a020729
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 65 deletions.
4 changes: 2 additions & 2 deletions indexer/postgres/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (tm *ObjectIndexer) createColumnDefinition(writer io.Writer, field schema.F
} else {
switch field.Kind {
case schema.EnumKind:
_, err = fmt.Fprintf(writer, "%q", enumTypeName(tm.moduleName, field.EnumDefinition))
_, err = fmt.Fprintf(writer, "%q", enumTypeName(tm.moduleName, field.EnumType))
if err != nil {
return err
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func simpleColumnType(kind schema.Kind) string {
return "JSONB"
case schema.DurationKind:
return "BIGINT"
case schema.Bech32AddressKind:
case schema.AddressKind:
return "TEXT"
default:
return ""
Expand Down
30 changes: 3 additions & 27 deletions indexer/postgres/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// CreateEnumType creates an enum type in the database.
func (m *ModuleIndexer) CreateEnumType(ctx context.Context, conn DBConn, enum schema.EnumDefinition) error {
func (m *ModuleIndexer) CreateEnumType(ctx context.Context, conn DBConn, enum schema.EnumType) error {
typeName := enumTypeName(m.moduleName, enum)
row := conn.QueryRowContext(ctx, "SELECT 1 FROM pg_type WHERE typname = $1", typeName)
var res interface{}
Expand Down Expand Up @@ -39,7 +39,7 @@ func (m *ModuleIndexer) CreateEnumType(ctx context.Context, conn DBConn, enum sc
}

// CreateEnumTypeSql generates a CREATE TYPE statement for the enum definition.
func CreateEnumTypeSql(writer io.Writer, moduleName string, enum schema.EnumDefinition) error {
func CreateEnumTypeSql(writer io.Writer, moduleName string, enum schema.EnumType) error {
_, err := fmt.Fprintf(writer, "CREATE TYPE %q AS ENUM (", enumTypeName(moduleName, enum))
if err != nil {
return err
Expand All @@ -63,30 +63,6 @@ func CreateEnumTypeSql(writer io.Writer, moduleName string, enum schema.EnumDefi
}

// enumTypeName returns the name of the enum type scoped to the module.
func enumTypeName(moduleName string, enum schema.EnumDefinition) string {
func enumTypeName(moduleName string, enum schema.EnumType) string {
return fmt.Sprintf("%s_%s", moduleName, enum.Name)
}

// createEnumTypesForFields creates enum types for all the fields that have enum kind in the module schema.
func (m *ModuleIndexer) createEnumTypesForFields(ctx context.Context, conn DBConn, fields []schema.Field) error {
for _, field := range fields {
if field.Kind != schema.EnumKind {
continue
}

if _, ok := m.definedEnums[field.EnumDefinition.Name]; ok {
// if the enum type is already defined, skip
// we assume validation already happened
continue
}

err := m.CreateEnumType(ctx, conn, field.EnumDefinition)
if err != nil {
return err
}

m.definedEnums[field.EnumDefinition.Name] = field.EnumDefinition
}

return nil
}
2 changes: 2 additions & 0 deletions indexer/postgres/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ go 1.12
// This module should only use the golang standard library (database/sql)
// and cosmossdk.io/indexer/base.
require cosmossdk.io/schema v0.1.1

replace cosmossdk.io/schema => ../../schema
2 changes: 0 additions & 2 deletions indexer/postgres/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
cosmossdk.io/schema v0.1.1 h1:I0M6pgI7R10nq+/HCQfbO6BsGBZA8sQy+duR1Y3aKcA=
cosmossdk.io/schema v0.1.1/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
34 changes: 19 additions & 15 deletions indexer/postgres/internal/testdata/example_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@ func init() {

switch i {
case schema.EnumKind:
field.EnumDefinition = MyEnum
case schema.Bech32AddressKind:
field.AddressPrefix = "foo"
field.EnumType = MyEnum
default:
}

AllKindsObject.ValueFields = append(AllKindsObject.ValueFields, field)
}

ExampleSchema = schema.ModuleSchema{
ObjectTypes: []schema.ObjectType{
AllKindsObject,
SingletonObject,
VoteObject,
},
ExampleSchema = mustModuleSchema([]schema.ObjectType{
AllKindsObject,
SingletonObject,
VoteObject,
})
}

func mustModuleSchema(objectTypes []schema.ObjectType) schema.ModuleSchema {
s, err := schema.NewModuleSchema(objectTypes)
if err != nil {
panic(err)
}
return s
}

var SingletonObject = schema.ObjectType{
Expand All @@ -60,9 +64,9 @@ var SingletonObject = schema.ObjectType{
Nullable: true,
},
{
Name: "an_enum",
Kind: schema.EnumKind,
EnumDefinition: MyEnum,
Name: "an_enum",
Kind: schema.EnumKind,
EnumType: MyEnum,
},
},
}
Expand All @@ -76,14 +80,14 @@ var VoteObject = schema.ObjectType{
},
{
Name: "address",
Kind: schema.Bech32AddressKind,
Kind: schema.AddressKind,
},
},
ValueFields: []schema.Field{
{
Name: "vote",
Kind: schema.EnumKind,
EnumDefinition: schema.EnumDefinition{
EnumType: schema.EnumType{
Name: "vote_type",
Values: []string{"yes", "no", "abstain"},
},
Expand All @@ -92,7 +96,7 @@ var VoteObject = schema.ObjectType{
RetainDeletions: true,
}

var MyEnum = schema.EnumDefinition{
var MyEnum = schema.EnumType{
Name: "my_enum",
Values: []string{"a", "b", "c"},
}
32 changes: 15 additions & 17 deletions indexer/postgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ModuleIndexer struct {
moduleName string
schema schema.ModuleSchema
tables map[string]*ObjectIndexer
definedEnums map[string]schema.EnumDefinition
definedEnums map[string]schema.EnumType
options Options
}

Expand All @@ -22,37 +22,35 @@ func NewModuleIndexer(moduleName string, modSchema schema.ModuleSchema, options
moduleName: moduleName,
schema: modSchema,
tables: map[string]*ObjectIndexer{},
definedEnums: map[string]schema.EnumDefinition{},
definedEnums: map[string]schema.EnumType{},
options: options,
}
}

// InitializeSchema creates tables for all object types in the module schema and creates enum types.
func (m *ModuleIndexer) InitializeSchema(ctx context.Context, conn DBConn) error {
// create enum types
for _, typ := range m.schema.ObjectTypes {
err := m.createEnumTypesForFields(ctx, conn, typ.KeyFields)
if err != nil {
return err
}

err = m.createEnumTypesForFields(ctx, conn, typ.ValueFields)
if err != nil {
return err
}
var err error
m.schema.EnumTypes(func(enumType schema.EnumType) bool {
err = m.CreateEnumType(ctx, conn, enumType)
return err == nil
})
if err != nil {
return err
}

// create tables for all object types
for _, typ := range m.schema.ObjectTypes {
m.schema.ObjectTypes(func(typ schema.ObjectType) bool {
tm := NewObjectIndexer(m.moduleName, typ, m.options)
m.tables[typ.Name] = tm
err := tm.CreateTable(ctx, conn)
err = tm.CreateTable(ctx, conn)
if err != nil {
return fmt.Errorf("failed to create table for %s in module %s: %v", typ.Name, m.moduleName, err) //nolint:errorlint // using %v for go 1.12 compat
err = fmt.Errorf("failed to create table for %s in module %s: %v", typ.Name, m.moduleName, err) //nolint:errorlint // using %v for go 1.12 compat
}
}
return err == nil
})

return nil
return err
}

// ObjectIndexers returns the object indexers for the module.
Expand Down
2 changes: 2 additions & 0 deletions indexer/postgres/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ require (

replace cosmossdk.io/indexer/postgres => ../.

replace cosmossdk.io/schema => ../../../schema

go 1.22
2 changes: 0 additions & 2 deletions indexer/postgres/tests/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cosmossdk.io/schema v0.1.1 h1:I0M6pgI7R10nq+/HCQfbO6BsGBZA8sQy+duR1Y3aKcA=
cosmossdk.io/schema v0.1.1/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down

0 comments on commit a020729

Please sign in to comment.