From 1e75963c1639341f5db35bc237eaf26841a55a83 Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 14 Oct 2023 20:11:00 +0200 Subject: [PATCH] Fix: hasura table names for bun --- hasura/hasura.go | 9 ++------- hasura/hasura_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/hasura/hasura.go b/hasura/hasura.go index 5c7a530..c66cd42 100644 --- a/hasura/hasura.go +++ b/hasura/hasura.go @@ -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:") } } } diff --git a/hasura/hasura_test.go b/hasura/hasura_test.go index 51f3371..935e6f7 100644 --- a/hasura/hasura_test.go +++ b/hasura/hasura_test.go @@ -6,6 +6,7 @@ import ( "github.com/dipdup-net/go-lib/config" "github.com/stretchr/testify/assert" + "github.com/uptrace/bun" ) type testTable struct { @@ -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 @@ -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 {