Skip to content

Commit 1802dde

Browse files
authored
Add a table to record listen history deletion (#3188)
* Add a table to record listen history deletion It is possible for users' to delete their entire listen history in one command, instead of individually deleting every listen. To handle this spark, record the user_id and the max created value until which listens have been deleted.
1 parent 494ec5e commit 1802dde

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

admin/timescale/create_tables.sql

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ CREATE TABLE listen_user_metadata (
2828

2929
SELECT create_hypertable('listen', 'listened_at', chunk_time_interval => INTERVAL '30 days');
3030

31+
CREATE TABLE deleted_user_listen_history (
32+
id INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL,
33+
user_id INTEGER NOT NULL,
34+
max_created TIMESTAMP WITH TIME ZONE NOT NULL
35+
);
36+
3137
-- Playlists
3238

3339
CREATE TABLE playlist.playlist (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BEGIN;
2+
3+
CREATE TABLE deleted_user_listen_history (
4+
id INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL,
5+
user_id INTEGER NOT NULL,
6+
max_created TIMESTAMP WITH TIME ZONE NOT NULL
7+
);
8+
9+
COMMIT;

listenbrainz/listenstore/dump_listenstore.py

+1
Original file line numberDiff line numberDiff line change
@@ -556,5 +556,6 @@ def cleanup_listen_delete_metadata(self):
556556
self.log.info("Cleaning up listen_delete_metadata")
557557
with timescale.engine.connect() as connection:
558558
connection.execute(text("DELETE FROM listen_delete_metadata WHERE status != 'pending'"))
559+
connection.execute(text("DELETE FROM deleted_user_listen_history"))
559560
connection.commit()
560561
self.log.info("Cleaning up listen_delete_metadata done!")

listenbrainz/listenstore/timescale_listenstore.py

+2
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,11 @@ def delete(self, user_id, created=None):
668668
WHERE user_id = :user_id
669669
"""
670670
query2 = """DELETE FROM listen WHERE user_id = :user_id AND created <= :created"""
671+
query3 = """INSERT INTO deleted_user_listen_history (user_id, max_created) VALUES (:user_id, :created)"""
671672
try:
672673
ts_conn.execute(sqlalchemy.text(query1), {"user_id": user_id})
673674
ts_conn.execute(sqlalchemy.text(query2), {"user_id": user_id, "created": created})
675+
ts_conn.execute(sqlalchemy.text(query3), {"user_id": user_id, "created": created})
674676
ts_conn.commit()
675677
except psycopg2.OperationalError as e:
676678
self.log.error("Cannot delete listens for user: %s" % str(e))

0 commit comments

Comments
 (0)