mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
create a function to batch delete items in trash
This commit is contained in:
parent
54dabe449d
commit
755f483ba9
4 changed files with 65 additions and 0 deletions
0
packages/api/src/jobs/prune_library_item.ts
Normal file
0
packages/api/src/jobs/prune_library_item.ts
Normal 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
|
||||
|
|
|
|||
32
packages/db/migrations/0181.do.batch_delete_trash_items.sql
Executable file
32
packages/db/migrations/0181.do.batch_delete_trash_items.sql
Executable 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;
|
||||
9
packages/db/migrations/0181.undo.batch_delete_trash_items.sql
Executable file
9
packages/db/migrations/0181.undo.batch_delete_trash_items.sql
Executable 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;
|
||||
Loading…
Reference in a new issue