create a function to batch delete items in trash

This commit is contained in:
Hongbo Wu 2024-06-11 18:51:25 +08:00
parent 54dabe449d
commit 755f483ba9
4 changed files with 65 additions and 0 deletions

View file

@ -1392,6 +1392,30 @@ export const batchDelete = async (criteria: FindOptionsWhere<LibraryItem>) => {
return authTrx(async (t) => t.query(sql))
}
export const batchDeleteAllTrash = async () => {
const sql = `
DO $$
DECLARE
user_record RECORD;
user_cursor CURSOR FOR SELECT id FROM omnivore.user WHERE status = 'ACTIVE'; -- Adjust the condition as needed
BEGIN
OPEN user_cursor;
LOOP
FETCH NEXT FROM user_cursor INTO user_record;
EXIT WHEN NOT FOUND;
DELETE FROM omnivore.library_item WHERE user_id = user_record.id AND state = 'DELETED' AND deleted_at < '2023-01-01';
RETURN NEXT;
END LOOP;
CLOSE user_cursor;
END $$;`
return authTrx(async (t) => t.query(sql))
}
export const findLibraryItemIdsByLabelId = async (
labelId: string,
userId: string

View file

@ -0,0 +1,32 @@
-- Type: DO
-- Name: batch_delete_trash_items
-- Description: Create a function to batch delete library items in trash
BEGIN;
CREATE OR REPLACE FUNCTION batch_delete_trash_items()
RETURNS VOID AS $$
DECLARE
user_record RECORD;
user_cursor CURSOR FOR
SELECT
id
FROM
omnivore.user
WHERE
status = 'ACTIVE';
BEGIN
FOR user_record IN user_cursor LOOP
-- For Row Level Security
PERFORM omnivore.set_claims(user_record.id, 'omnivore_user');
DELETE FROM omnivore.library_item
WHERE
user_id = user_record.id
AND state = 'DELETED'
AND deleted_at < NOW() - INTERVAL '14 days';
END LOOP;
END;
$$ LANGUAGE plpgsql;
COMMIT;

View file

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: batch_delete_trash_items
-- Description: Create a function to batch delete library items in trash
BEGIN;
DROP FUNCTION IF EXISTS batch_delete_trash_items();
COMMIT;