Skip to content

v4.5.0

Compare
Choose a tag to compare
@xiam xiam released this 03 Jan 17:37
· 53 commits to v4 since this release
28dd6cd

Release notes

The github.com/lib/pq package that we used as driver for PostgreSQL and CockroachDB went under maintenance mode, the package maintainers recommended users to switch to github.com/jackc/pgx. We followed that recommendation, and this release uses github.com/jackc/pgx as driver for both PostgreSQL and CockroachDB.

For some codebases, the update will be completely transparent, but others cases might require slight modifications to their code, specially if they're using custom field types. If you're upgrading to v4.5.0 make sure you're either using the special types with driver.Value and sql.Scanner methods provided by the github.com/upper/db/v4/adapter/postgresql package, or provide your own ones.

If the change is too complicated, you'll be able to use github.com/lib/pq for a while by including the pgx build constraint along the other build tags. See:

go build -tags=pgx ...

however, this is a temporary solution, support for the github.com/lib/pq driver is going to be definitely removed on February 28th 2022.

Upgrading your codebase

If you upgraded to v4.5.0 and your code doesn't compile anymore or if it throws runtime errors around conversions between Go types and database types you'll have to tweak it a bit. Basically, you'll have to make sure you're providing scanners and values for all your types. Basic Go types, like int, string, bool or even a slice of basic types won't require any special handling, but some other types like *[]intor []uint will do.

For instance, if your struct looks like this:

type Foo struct {
	ID     uint64    `db:"id"`
	Names  *[]string `db:"names"`
}

you'll have to convert it into:

import (
	"github.com/upper/db/v4/adapter/postgresql"
)

type Foo struct {
	ID      uint64                  `db:"id"` // no need to be updated
	Names   *postgresql.StringArray `db:"names"` // changed
}

The table below has some of the most common equivalences between old types and new types:

instead of use
*[]string *postgresql.StringArray
*[]int64 *postgresql.Int64Array
*[]float64 *postgresql.Float64Array
*[]float32 *postgresql.Float32Array
*[]bool *postgresql.BoolArray
*map[string]interface{} *postgresql.JSONBMap
*[]interface{} *postgresql.JSONBArray

If you're using custom types that are not provided in the table above, like map[string]bool, you'll have to provide your own driver.Value and sql.Scanner methods.

If the destination type is JSONB you can use postgresql.JSONBValue and postgresql.ScanJSONB to make those conversions easier, see the example below:

package foo

import (
	"github.com/upper/db/v4/adapter/postgresql"
)

type CustomJSONB map[string]bool

func (c CustomJSONB) Value() (driver.Value, error) {
	return postgresql.JSONBValue(c)
}

func (c *CustomJSONB) Scan(src interface{}) error {
	return postgresql.ScanJSONB(c, src)
}

You can also use the *postgresql.JSONBConverter type to add automatic conversions to some types, like this:

package foo

import (
	"github.com/upper/db/v4/adapter/postgresql"
)

type CustomJSONB struct {
	N string  `json:"name"`
	V float64 `json:"value"`

	*postgresql.JSONBConverter
}

type CustomJSONBObjectArray []postgresql.CustomJSONB

func (CustomJSONBObjectArray) ConvertValue(in interface{}) interface {
	sql.Scanner
	driver.Valuer
} {
	return &postgresql.JSONB{in}
}

type CustomJSONBObjectMap map[string]CustomJSONB

func (c CustomJSONBObjectMap) Value() (driver.Value, error) {
	return postgresql.JSONBValue(c)
}

func (c *CustomJSONBObjectMap) Scan(src interface{}) error {
	return postgresql.ScanJSONB(c, src)
}

Thanks for using db/v4!

What's Changed

  • Replace lib/pq with pgx by @xiam in #627
  • Fix sqlite.New panic by @sdvcrx in #635
  • Fix condition that forced the statement cache to be skipped by @xiam in #638

New Contributors

Acknowledgements

Special thanks to @pkieltyka and Horizon Blockchain Games for their continuos support over the years! Thank you very much

Full Changelog: v4.2.1...v4.5.0