Skip to content

v0.20.1 — batch upsert from structs, bind-parameter limit

Latest

Choose a tag to compare

@kolkov kolkov released this 27 Oct 08:36
· 2 commits to main since this release

A patch release on top of v0.20.0: the struct form of batch upsert asked for in #47, an explicit error for batches over the database's bind-parameter limit, and fixes found since v0.20.0.

go get github.com/coregx/relica@v0.20.1

Added

  • BatchInsertQuery.ValuesStructs(rows) — batch upsert from structs (#47): db.BatchInsert(table, nil).ValuesStructs(nodes).OnConflict(...).DoUpdateAll() builds one multi-row INSERT … ON CONFLICT (ON DUPLICATE KEY UPDATE on MySQL) from a slice of structs or pointers of one type, read like BatchInsertStruct (db tags, Naming, embedded structs flattened). With nil columns the columns come from the struct (a single primary key that is zero in every row is left out) and ValuesStructs is the only source of rows; with listed columns exactly those are read, a listed primary key must not be zero, and it mixes with Values/ValuesMap (after a derived ValuesStructs they are an error: derived columns are sorted, a positional row would land in the wrong columns). Reachable through Executor.BatchInsert. BatchInsertStruct is built on it. The multi-row upsert with explicit values has existed since v0.16.5

Fixed

  • A batch over the database's bind-parameter limit reached the database — BatchInsert and BatchInsertStruct send one statement, and PostgreSQL and MySQL take at most 65535 parameters (rows × columns), SQLite 32766 (its compiled default), so a large batch failed in the driver or the server. It is now an error returned before anything is sent, naming the limit and the rows per statement that fit; Relica does not split the batch or open a transaction on its own — send the rows in slices, inside Transactional when they must be atomic (AGENTS.md › Batch Operations). In a transaction on MySQL and PostgreSQL the statement goes to the driver unprepared, where interpolateParams or the pgx simple protocol may accept it, so the check is left to the driver there; SQLite is checked everywhere
  • BatchInsertStruct with rows of two struct types wrote NULL for the columns the later type lacks; rows must now have one type
  • BatchInsertStruct wrote an empty autoid field as "" — Model().Insert() generates the ID, a batch does not; an empty autoid field (a string or a named string type) is now an error naming it
  • AGENTS.md › Batch Operations showed code that does not compile: BatchInsertStruct/InsertStruct without .Execute(), and a BatchUpdate(table, columns, key).Values(...) form that does not exist (it is BatchUpdate(table, keyColumn).Set(key, map))
  • The audit missed Update/Delete without Where when the table is not a string literal — skills/relica/scripts/audit.sh (V4) matched only db.Delete("t"), so db.Delete(table).Execute() in a loop over table names (a schema reset) or db.Update(tableName(x))…Execute() went unreported, and the v0.20.0 error surfaced only at run time. V4 now takes any single table argument: a literal, a variable or one call

Full Changelog: v0.20.0...v0.20.1