Skip to content

Commit

Permalink
Fix: hasura table names for bun
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 14, 2023
1 parent 14f31d2 commit 1e75963
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
9 changes: 2 additions & 7 deletions hasura/hasura.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,10 @@ func formatSelectPermissions(limit uint64, allowAggs bool, role string, columns

func getTableName(value reflect.Value, typ reflect.Type) string {
if _, ok := typ.MethodByName("TableName"); !ok {
if field, exists := typ.FieldByName("tableName"); exists {
if tag := field.Tag.Get("pg"); tag != "" {
if values := strings.Split(tag, ","); len(values) > 0 {
return values[0]
}
}
if field, exists := typ.FieldByName("BaseModel"); exists {
if tag := field.Tag.Get("bun"); tag != "" {
if values := strings.Split(tag, ","); len(values) > 0 {
return values[0]
return strings.TrimPrefix(values[0], "table:")
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions hasura/hasura_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/dipdup-net/go-lib/config"
"github.com/stretchr/testify/assert"
"github.com/uptrace/bun"
)

type testTable struct {
Expand Down Expand Up @@ -45,6 +46,10 @@ func (testTable4) TableName() int64 {
return 0
}

type testTable5 struct {
bun.BaseModel `bun:"table:test_name"`
}

func Test_getTableName(t *testing.T) {
tests := []struct {
name string
Expand All @@ -67,6 +72,10 @@ func Test_getTableName(t *testing.T) {
name: "Test 4",
model: &testTable4{},
want: "test_table4",
}, {
name: "Test 5",
model: &testTable5{},
want: "test_name",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 1e75963

Please sign in to comment.