diff --git a/lib/www/server/lib/db/event/patch.js b/lib/www/server/lib/db/event/patch.js index 3eefae6..f4fd51c 100644 --- a/lib/www/server/lib/db/event/patch.js +++ b/lib/www/server/lib/db/event/patch.js @@ -5,6 +5,8 @@ async function patch (projectId, eventId, payload, opts = {}) { const p = payload; // Shorter const client = await setSurvey(projectId); try { + await transaction.begin(client); + // The order of attributes in an object is not defined, so // in theory we could get a different order if we made separate // calls to Object.keys() and Object.values(), unlikely as that @@ -22,10 +24,32 @@ async function patch (projectId, eventId, payload, opts = {}) { const values = [ eventId, ...v ]; await client.query(text, values); + + // NOTE Horrible hack warning. + // If we try to do the UPDATE on event_log, as you normally would, + // we get a constraint violation on `event_log_full_validity_check`. + // We would normally set that constraint as DEFERRABLE, except that + // as of version 14, PostgreSQL does not support deferrable CHECK + // constraints: + // https://www.postgresql.org/docs/current/sql-createtable.html + // So the options that I can think of are either not to use a transaction + // or to apply this second update directly on the `event_log_full` table. + + const text1 = ` + UPDATE event_log_full + SET remarks = replace_placeholders(remarks, tstamp, sequence, point) + WHERE id = $1 AND validity @> current_timestamp; + `; + const values1 = [ eventId ]; + + await client.query(text1, values1); + + await transaction.commit(client); } catch (err) { + err.origin = __filename; throw err; } finally { - client.release(); + client.release(); // implies ROLLBACK; } return; diff --git a/lib/www/server/lib/db/event/post.js b/lib/www/server/lib/db/event/post.js index 0adaf55..d213a22 100644 --- a/lib/www/server/lib/db/event/post.js +++ b/lib/www/server/lib/db/event/post.js @@ -10,7 +10,7 @@ async function post (projectId, payload, opts = {}) { const text = ` INSERT INTO event_log (tstamp, sequence, point, remarks, labels) - VALUES ($1, $2, $3, $4, $5); + VALUES ($1, $2, $3, replace_placeholders($4, $1, $2, $3), $5); `; const values = [ p.tstamp, p.sequence, p.point, p.remarks, p.labels ]; diff --git a/lib/www/server/lib/db/event/put.js b/lib/www/server/lib/db/event/put.js index 744555f..699288b 100644 --- a/lib/www/server/lib/db/event/put.js +++ b/lib/www/server/lib/db/event/put.js @@ -12,7 +12,7 @@ async function put (projectId, eventId, payload, opts = {}) { tstamp = $1, sequence = $2, point = $3, - remarks = $4, + remarks = replace_placeholders($4, $1, $2, $3), labels = $5 WHERE id = $6; `;