-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get folder ancestors excluding root children direct root childr…
…en, useful for workspaces
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
migrations/20241004142347-get-folder-ancestor-omit-root-children.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface) { | ||
await queryInterface.sequelize.query(` | ||
CREATE OR REPLACE FUNCTION get_folder_ancestors_excluding_root_children(folder_id UUID, u_id INT) | ||
RETURNS setof folders AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
WITH RECURSIVE hier AS ( | ||
SELECT c.* | ||
FROM folders c | ||
WHERE c.removed = FALSE | ||
AND c.uuid = folder_id | ||
UNION | ||
SELECT f.* | ||
FROM folders f | ||
INNER JOIN hier fh ON fh.parent_id = f.id | ||
WHERE f.removed = FALSE | ||
AND f.user_id = u_id | ||
AND f.parent_id IS NOT NULL | ||
AND NOT EXISTS ( | ||
SELECT 1 | ||
FROM folders root | ||
WHERE root.id = f.parent_id | ||
AND root.parent_id IS NULL | ||
) | ||
) | ||
SELECT * FROM hier | ||
WHERE parent_id IS NOT NULL; -- Exclude the root folder itself | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
`); | ||
}, | ||
|
||
async down(queryInterface) { | ||
await queryInterface.sequelize.query( | ||
'drop function get_folder_ancestors_excluding_root_children;', | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters