chore(indexer/postgres): update to changes on main (#21077)

This commit is contained in:
Aaron Craelius 2024-07-25 18:36:17 +02:00 committed by GitHub
parent 683371f779
commit a0207294c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 43 additions and 65 deletions

View File

@ -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
}
@ -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 ""

View File

@ -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{}
@ -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
@ -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
}

View File

@ -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

View File

@ -1,2 +0,0 @@
cosmossdk.io/schema v0.1.1 h1:I0M6pgI7R10nq+/HCQfbO6BsGBZA8sQy+duR1Y3aKcA=
cosmossdk.io/schema v0.1.1/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=

View File

@ -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{
@ -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,
},
},
}
@ -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"},
},
@ -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"},
}

View File

@ -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
}
@ -22,7 +22,7 @@ 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,
}
}
@ -30,29 +30,27 @@ func NewModuleIndexer(moduleName string, modSchema schema.ModuleSchema, 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.

View File

@ -30,4 +30,6 @@ require (
replace cosmossdk.io/indexer/postgres => ../.
replace cosmossdk.io/schema => ../../../schema
go 1.22

View File

@ -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=