Skip to content

Commit

Permalink
fix(pgdialect): auto-enable array support if the sql type is an array
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 7, 2021
1 parent fa38e04 commit 62c1012
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dialect/pgdialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgdialect
import (
"database/sql"
"strconv"
"strings"

"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/feature"
Expand Down Expand Up @@ -68,7 +69,7 @@ func (d *Dialect) onField(field *schema.Field) {
}
}

if field.Tag.HasOption("array") {
if field.Tag.HasOption("array") || strings.HasSuffix(field.UserSQLType, "[]") {
field.Append = d.arrayAppender(field.StructField.Type)
field.Scan = arrayScanner(field.StructField.Type)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/dbtest/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,27 @@ func TestPGBytea(t *testing.T) {
require.Equal(t, []byte("hello"), model.Bytes)
}

func TestPGByteaArray(t *testing.T) {
type Model struct {
BytesSlice [][]byte `bun:"type:bytea[]"`
}

db := pg(t)
defer db.Close()

err := db.ResetModel(ctx, (*Model)(nil))
require.NoError(t, err)

model1 := &Model{BytesSlice: [][]byte{[]byte("hello"), []byte("world")}}
_, err = db.NewInsert().Model(model1).Exec(ctx)
require.NoError(t, err)

model2 := new(Model)
err = db.NewSelect().Model(model2).Scan(ctx)
require.NoError(t, err)
require.Equal(t, model1.BytesSlice, model2.BytesSlice)
}

func TestPGDate(t *testing.T) {
db := pg(t)
defer db.Close()
Expand Down

0 comments on commit 62c1012

Please sign in to comment.