Skip to content

Commit

Permalink
Merge pull request #442 from internxt/fix/remove-deleted-children-fol…
Browse files Browse the repository at this point in the history
…ders-from-lookup-table

[PB-3189] fix: correctly remove children folders from lookup table on delete
  • Loading branch information
sg-gs authored Dec 16, 2024
2 parents 4869c87 + 399917d commit 4d408aa
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_folder_in_look_up_table()
RETURNS TRIGGER AS $$
DECLARE
user_record users%ROWTYPE;
BEGIN
SELECT * INTO user_record FROM users WHERE id = NEW.user_id;
-- If the name has been changed, update the lookup table
IF NEW.deleted = false AND NEW.removed = false AND OLD.plain_name != NEW.plain_name THEN
UPDATE look_up
SET
name = NEW.plain_name,
tokenized_name = to_tsvector(NEW.plain_name)
WHERE
item_type = 'folder'
AND item_id = NEW.uuid
AND user_id = user_record.uuid;
-- If the folder is restored
ELSIF NEW.deleted = false and NEW.removed = false AND (old.deleted = true or old.removed = true) THEN
INSERT INTO look_up (id, name, tokenized_name, item_id, item_type, user_id)
VALUES (uuid_generate_v4(), NEW.plain_name, to_tsvector(NEW.plain_name), NEW.uuid, 'folder', user_record.uuid);
-- If the file status changes to deleted / trashed
ELSIF (OLD.deleted = false OR OLD.removed = false) AND (NEW.deleted = true OR NEW.removed = true) THEN
-- Remove the lookup entry
DELETE FROM look_up
WHERE
item_type = 'folder'
AND item_id = NEW.uuid
AND user_id = user_record.uuid;
END IF;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
`);
},

async down(queryInterface, Sequelize) {},
};

0 comments on commit 4d408aa

Please sign in to comment.