Skip to content

Commit

Permalink
Add a table to record listen history deletion (#3188)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
amCap1712 authored Feb 20, 2025
1 parent 494ec5e commit 1802dde
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions admin/timescale/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ CREATE TABLE listen_user_metadata (

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

CREATE TABLE deleted_user_listen_history (
id INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL,
user_id INTEGER NOT NULL,
max_created TIMESTAMP WITH TIME ZONE NOT NULL
);

-- Playlists

CREATE TABLE playlist.playlist (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN;

CREATE TABLE deleted_user_listen_history (
id INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL,
user_id INTEGER NOT NULL,
max_created TIMESTAMP WITH TIME ZONE NOT NULL
);

COMMIT;
1 change: 1 addition & 0 deletions listenbrainz/listenstore/dump_listenstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,5 +556,6 @@ def cleanup_listen_delete_metadata(self):
self.log.info("Cleaning up listen_delete_metadata")
with timescale.engine.connect() as connection:
connection.execute(text("DELETE FROM listen_delete_metadata WHERE status != 'pending'"))
connection.execute(text("DELETE FROM deleted_user_listen_history"))
connection.commit()
self.log.info("Cleaning up listen_delete_metadata done!")
2 changes: 2 additions & 0 deletions listenbrainz/listenstore/timescale_listenstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,11 @@ def delete(self, user_id, created=None):
WHERE user_id = :user_id
"""
query2 = """DELETE FROM listen WHERE user_id = :user_id AND created <= :created"""
query3 = """INSERT INTO deleted_user_listen_history (user_id, max_created) VALUES (:user_id, :created)"""
try:
ts_conn.execute(sqlalchemy.text(query1), {"user_id": user_id})
ts_conn.execute(sqlalchemy.text(query2), {"user_id": user_id, "created": created})
ts_conn.execute(sqlalchemy.text(query3), {"user_id": user_id, "created": created})
ts_conn.commit()
except psycopg2.OperationalError as e:
self.log.error("Cannot delete listens for user: %s" % str(e))
Expand Down

0 comments on commit 1802dde

Please sign in to comment.