pipework 0.208.0 → 0.208.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.208.1
|
|
4
|
+
|
|
5
|
+
- **Fixed: an audited `pipe.upsert` keyed on a column pipework fills in recorded every write as `create` with `before` null** (#790). The `before` snapshot was looked up by the conflict-column values the caller passed, so on a tenant-scoped table with `.onConflict(['tenantId'])` (where the scoped insert fills `tenantId` in) it matched nothing. It is now looked up by the conflict-column values of the rows the upsert wrote, read from the table as it was before the statement, so an upsert that replaced a row records `update` with that row as `before`. Conflict columns filled by a default count too.
|
|
6
|
+
|
|
3
7
|
## 0.208.0
|
|
4
8
|
|
|
5
9
|
- **Fixed: a write was audited once per `@touches` target on its entity, whatever the verb** (#788). A step declaring `X:create` and `X:update` wrote a `create` and an `update` row for each insert, and again for each update. Each row a write affects is now recorded under the step's touches whose verb is the row's verb: an insert is `create`, an update `update`, a delete `delete`, and an upsert row `create` when it was new and `update` when it existed. With no touch of that verb, the row is recorded under the step's custom-verb touches (any verb but `create`, `update`, `delete`, `read`), else under its touches other than `read` (so a step declaring only `X:update` still records an upsert's new row as `update`), else under its `read` touches. A write to an entity counts for every touch on that entity, so none of them also gets a read row at step end, and a nested step's write counts for each enclosing step that declares the same touch. **Behavior change:** a step whose touches include the write's verb no longer records the write under its other touches on that entity.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verb-upsert.d.ts","sourceRoot":"","sources":["../../../../src/data/write/audit/verb-upsert.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"verb-upsert.d.ts","sourceRoot":"","sources":["../../../../src/data/write/audit/verb-upsert.ts"],"names":[],"mappings":"AAaA,OAAO,EAAwC,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAGjG,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAI7C,KAAK,KAAK,GAAG,GAAG,CAAA;AAEhB,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,kBAAkB,GAAG,GAAG,CAgF7F"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// UPSERT under audit: a single INSERT ... ON CONFLICT DO UPDATE / DO NOTHING
|
|
2
2
|
// that either inserts a new row or updates the conflicting one. Audit rows
|
|
3
3
|
// distinguish create vs update by left-joining the upserted row with the
|
|
4
|
-
// `before` snapshot
|
|
4
|
+
// `before` snapshot (pre-write rows sharing the upserted rows' conflict-target
|
|
5
|
+
// values, whoever supplied them): a NULL `before` row
|
|
5
6
|
// means the upserted row was freshly inserted; non-NULL means it updated
|
|
6
7
|
// an existing row. The audit's `before` jsonb is wrapped in a CASE that
|
|
7
8
|
// returns NULL::jsonb for the insert branch.
|
|
@@ -41,17 +42,13 @@ export function compileUpsertAudit(db, state, ctx) {
|
|
|
41
42
|
const upsertInner = upsertChain.returning().getSQL();
|
|
42
43
|
const tableName = quoteIdentifier(state.table.$name, 'target table');
|
|
43
44
|
const conflictColsSql = conflictCols.map(k => quoteIdentifier(camelToSnake(k), 'conflict column')).join(', ');
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// No `FOR UPDATE` — the statement's snapshot keeps `before` and `upserted`
|
|
49
|
-
// consistent; locking inside a CTE conflicts with the sibling write on the
|
|
50
|
-
// same row and silently yields zero rows from `before`.
|
|
51
|
-
// `valuesRows` is never empty: `db.insert(...).values([])` above throws first, so this row-value IN never renders `IN ()`.
|
|
45
|
+
// `before` is keyed on the rows `upserted` wrote, not the caller's values, so
|
|
46
|
+
// conflict columns pipework fills in (tenant) or defaults match too. All CTEs
|
|
47
|
+
// see the pre-statement snapshot: `before` reads the replaced row. No `FOR
|
|
48
|
+
// UPDATE`: locking inside a CTE conflicts with the sibling write on the row.
|
|
52
49
|
const beforeSelect = sql `
|
|
53
50
|
SELECT * FROM ${sql.raw(tableName)}
|
|
54
|
-
WHERE (${sql.raw(conflictColsSql)}) IN (${sql.
|
|
51
|
+
WHERE (${sql.raw(conflictColsSql)}) IN (SELECT ${sql.raw(conflictColsSql)} FROM upserted)
|
|
55
52
|
`;
|
|
56
53
|
const joinOn = conflictCols
|
|
57
54
|
.map(k => {
|
|
@@ -79,8 +76,8 @@ export function compileUpsertAudit(db, state, ctx) {
|
|
|
79
76
|
const auditSelect = buildAuditInserts(ctx, fromClause, buildEntityIdExpr(state.table, 'upserted'), beforeExpr, after, rowFilter);
|
|
80
77
|
const finalSelect = buildFinalSelect(state, 'upserted');
|
|
81
78
|
return sql `
|
|
82
|
-
WITH
|
|
83
|
-
|
|
79
|
+
WITH upserted AS (${upsertInner}),
|
|
80
|
+
before AS (${beforeSelect}),
|
|
84
81
|
audit_inserts AS (
|
|
85
82
|
INSERT INTO "audit_record" (${sql.raw(AUDIT_COLUMNS_SQL)})
|
|
86
83
|
${auditSelect}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verb-upsert.js","sourceRoot":"","sources":["../../../../src/data/write/audit/verb-upsert.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,2EAA2E;AAC3E,yEAAyE;AACzE,
|
|
1
|
+
{"version":3,"file":"verb-upsert.js","sourceRoot":"","sources":["../../../../src/data/write/audit/verb-upsert.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,2EAA2E;AAC3E,yEAAyE;AACzE,+EAA+E;AAC/E,sDAAsD;AACtD,yEAAyE;AACzE,wEAAwE;AACxE,6CAA6C;AAE7C,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAE,YAAY,EAAE,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