diff --git a/internal/dbtest/pg_test.go b/internal/dbtest/pg_test.go index 3b59f33af..37ea8b3f9 100644 --- a/internal/dbtest/pg_test.go +++ b/internal/dbtest/pg_test.go @@ -5,6 +5,7 @@ import ( "net" "reflect" "testing" + "time" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -315,4 +316,19 @@ func TestPGDate(t *testing.T) { err := db.NewSelect().ColumnExpr("'2021-09-15'::DATE").Scan(ctx, &str) require.NoError(t, err) require.Equal(t, "2021-09-15", str) + + str = "" + err = db.NewSelect().ColumnExpr("CURRENT_TIMESTAMP::date").Scan(ctx, &str) + require.NoError(t, err) + require.NotZero(t, str) + + var tm time.Time + err = db.NewSelect().ColumnExpr("CURRENT_TIMESTAMP::date").Scan(ctx, &tm) + require.NoError(t, err) + require.NotZero(t, tm) + + var nullTime bun.NullTime + err = db.NewSelect().ColumnExpr("CURRENT_TIMESTAMP::date").Scan(ctx, &nullTime) + require.NoError(t, err) + require.False(t, nullTime.IsZero()) } diff --git a/schema/sqltype.go b/schema/sqltype.go index 23322a1e1..72609cf7a 100644 --- a/schema/sqltype.go +++ b/schema/sqltype.go @@ -109,17 +109,23 @@ func (tm *NullTime) Scan(src interface{}) error { } switch src := src.(type) { + case time.Time: + tm.Time = src + return nil + case string: + newtm, err := internal.ParseTime(src) + if err != nil { + return err + } + tm.Time = newtm + return nil case []byte: newtm, err := internal.ParseTime(internal.String(src)) if err != nil { return err } - tm.Time = newtm return nil - case time.Time: - tm.Time = src - return nil default: return fmt.Errorf("bun: can't scan %#v into NullTime", src) }