Documentation
¶
Index ¶
- func CopyProtocolUpsert[T any](ctx context.Context, tx pgx.Tx, tableName string, records []T) error
- func CreateConnectionString(values map[string]string) string
- func NamesFromRecord(x interface{}) []string
- func NamesValuesFromRecord(x interface{}) ([]string, []interface{})
- func OpenPgxPool(config configuration.PostgresConfig) (*pgxpool.Pool, error)
- func ParseNullInt32(nullInt sql.NullInt32) *int32
- func ParseNullString(nullString sql.NullString) *string
- func ParseNullStringDefault(nullString sql.NullString) string
- func ParseNullTime(nullTime sql.NullTime) *time.Time
- func ReadInt(rows pgx.Rows) (int, error)
- func UniqueTableName(table string) string
- func UpdateDatabase(ctx context.Context, db pgxtype.Querier, migrations []Migration) error
- func Upsert[T any](ctx context.Context, db *pgxpool.Pool, tableName string, records []T) error
- func ValuesFromRecord(x interface{}) []interface{}
- func WithTestDb(migrations []Migration, action func(db *pgxpool.Pool) error) error
- func WithTestDbCustom(migrations []Migration, config configuration.PostgresConfig, ...) error
- type Migration
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyProtocolUpsert ¶ added in v0.3.40
func CreateConnectionString ¶ added in v0.3.40
func NamesFromRecord ¶ added in v0.3.40
func NamesFromRecord(x interface{}) []string
NamesFromRecord returns a slice composed of the field names in a struct marked with "db" tags.
For example, if x is an instance of a struct with definition
type Rectangle struct { Width int `db:"width"` Height int `db:"height"` },
it returns ["width", "height"].
func NamesValuesFromRecord ¶ added in v0.3.40
func NamesValuesFromRecord(x interface{}) ([]string, []interface{})
NamesValuesFromRecord returns a slice composed of the field names and another composed of the corresponding values for fields of a struct marked with "db" tags.
For example, if x is an instance of a struct with definition
type Rectangle struct { Width int `db:"width"` Height int `db:"height"` },
where Width = 10 and Height = 5, it returns ["width", "height"], [10, 5].
This function does not handle pointers to structs, i.e., x must be Rectangle{} and not &Rectangle{}.
func OpenPgxPool ¶ added in v0.3.40
func OpenPgxPool(config configuration.PostgresConfig) (*pgxpool.Pool, error)
func ParseNullInt32 ¶ added in v0.3.44
func ParseNullString ¶ added in v0.3.44
func ParseNullString(nullString sql.NullString) *string
func ParseNullStringDefault ¶ added in v0.3.44
func ParseNullStringDefault(nullString sql.NullString) string
func UniqueTableName ¶
func UpdateDatabase ¶
func Upsert ¶ added in v0.3.40
Upsert is an optimised SQL call for bulk upserts.
For efficiency, this function: 1. Creates an empty temporary SQL table. 2. Inserts all records into the temporary table using the postgres-specific COPY wire protocol. 3. Upserts all records from the temporary table into the target table (as specified by tableName).
The COPY protocol can be faster than repeated inserts for as little as 5 rows; see https://www.postgresql.org/docs/current/populate.html https://pkg.go.dev/github.com/jackc/pgx/v4#hdr-Copy_Protocol
The records to write should be structs with fields marked with "db" tags. Field names and values are extracted using the NamesValuesFromRecord function; see its definition for details. The first field is used as the primary key in SQL.
The temporary table is created with the provided schema, which should be of the form (
id UUID PRIMARY KEY, width int NOT NULL, height int NOT NULL
) I.e., it should omit everything before and after the "(" and ")", respectively.
func ValuesFromRecord ¶ added in v0.3.40
func ValuesFromRecord(x interface{}) []interface{}
ValuesFromRecord returns a slice composed of the values of the fields in a struct marked with "db" tags.
For example, if x is an instance of a struct with definition
type Rectangle struct { Name string, Width int `db:"width"` Height int `db:"height"` },
where Width = 5 and Height = 10, it returns [5, 10].
func WithTestDb ¶
WithTestDb spins up a Postgres database for testing migrations: perform the list of migrations before entering the action callback action: callback for client code
func WithTestDbCustom ¶ added in v0.3.40
func WithTestDbCustom(migrations []Migration, config configuration.PostgresConfig, action func(db *pgxpool.Pool) error) error
WithTestDbCustom connects to specified database for testing migrations: perform the list of migrations before entering the action callback config: PostgresConfig to specify connection details to database action: callback for client code
Types ¶
type Migration ¶ added in v0.3.40
type Migration struct {
// contains filtered or unexported fields
}
Migration represents a single, versioned database migration script
func ReadMigrations ¶ added in v0.3.43
func ReadMigrationsFromStatik ¶ added in v0.3.43
TODO: remove this when we've migrated over to iofs