From 956184377276dd91e82e0103ffe24c9c8ddc0fd2 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 10 Sep 2020 12:28:36 +0100 Subject: [PATCH] stop the peek table growing by reusing rows correctly --- syncapi/storage/postgres/peeks_table.go | 4 +++- syncapi/storage/sqlite3/peeks_table.go | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/syncapi/storage/postgres/peeks_table.go b/syncapi/storage/postgres/peeks_table.go index f0c63fdb3..75eeac986 100644 --- a/syncapi/storage/postgres/peeks_table.go +++ b/syncapi/storage/postgres/peeks_table.go @@ -44,7 +44,9 @@ CREATE INDEX IF NOT EXISTS syncapi_peeks_user_id_device_id_idx ON syncapi_peeks( const insertPeekSQL = "" + "INSERT INTO syncapi_peeks" + " (room_id, user_id, device_id, creation_ts)" + - " VALUES ($1, $2, $3, $4) RETURNING id" + " VALUES ($1, $2, $3, $4)" + + " ON CONFLICT (room_id, user_id, device_id) DO UPDATE SET deleted=false, creation_ts=$4" + + " RETURNING id" const deletePeekSQL = "" + "UPDATE syncapi_peeks SET deleted=true, id=nextval('syncapi_stream_id') WHERE room_id = $1 AND user_id = $2 AND device_id = $3 RETURNING id" diff --git a/syncapi/storage/sqlite3/peeks_table.go b/syncapi/storage/sqlite3/peeks_table.go index 4dc336a12..d755e28c2 100644 --- a/syncapi/storage/sqlite3/peeks_table.go +++ b/syncapi/storage/sqlite3/peeks_table.go @@ -42,9 +42,9 @@ CREATE INDEX IF NOT EXISTS syncapi_peeks_user_id_device_id_idx ON syncapi_peeks( ` const insertPeekSQL = "" + - "INSERT INTO syncapi_peeks" + - " (id, room_id, user_id, device_id, creation_ts)" + - " VALUES ($1, $2, $3, $4, $5)" + "INSERT OR REPLACE INTO syncapi_peeks" + + " (id, room_id, user_id, device_id, creation_ts, deleted)" + + " VALUES ($1, $2, $3, $4, $5, false)" const deletePeekSQL = "" + "UPDATE syncapi_peeks SET deleted=true, id=$1 WHERE room_id = $2 AND user_id = $3 AND device_id = $4"