-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom NullBytea type for riverdatabasesql
As #650 illustrates, the riverdatabasesql driver has issues with encoding arrays of byte arrays (`bytea[]`) and just byte arrays in general because the underlying pq implementation used by sqlc doesn't differentiate between nil vs empty slices. Fix this with a custom type. Fixes #650.
- Loading branch information
Showing
5 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
riverdriver/riverdatabasesql/internal/dbsqlc/river_job.sql.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
riverdriver/riverdatabasesql/internal/pgtypealias/null_bytea.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package pgtypealias | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
) | ||
|
||
// NullBytea is a custom type for PostgreSQL bytea that returns SQL NULL when | ||
// the underlying slice is nil or empty. This override takes over for the base | ||
// type `bytea`, so that when sqlc generates code for arrays of bytea, each | ||
// element is a NullBytea and properly handles nil values. This is in contrast | ||
// to the default behavior of pq.Array in this scenario. | ||
// | ||
// See https://github.com/riverqueue/river/issues/650 for more information. | ||
type NullBytea []byte //nolint:recvcheck | ||
|
||
// Value implements the driver.Valuer interface. It returns nil when the | ||
// underlying slice is nil or empty, ensuring that missing values are sent as | ||
// SQL NULL. | ||
func (nb NullBytea) Value() (driver.Value, error) { | ||
if len(nb) == 0 { | ||
return nil, nil //nolint:nilnil | ||
} | ||
return []byte(nb), nil | ||
} | ||
|
||
// Scan implements the sql.Scanner interface. | ||
func (nb *NullBytea) Scan(src interface{}) error { | ||
if src == nil { | ||
*nb = nil | ||
return nil | ||
} | ||
b, ok := src.([]byte) | ||
if !ok { | ||
return fmt.Errorf("nullBytea.Scan: got %T, expected []byte", src) | ||
} | ||
*nb = append((*nb)[0:0], b...) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters