Skip to content

Commit

Permalink
Fix for un-intentional report cache busting when copying a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Oct 29, 2024
1 parent 4148b3b commit 55ce468
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 308 deletions.
75 changes: 75 additions & 0 deletions packages/api/migrations/committed/000337.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
--! Previous: sha1:b8cbc01e7d5166e77a68f5e3072082b77ff40769
--! Hash: sha1:34bba5d9e6dcba7e4de57df697d33c04c760aa76

-- Enter migration here
CREATE OR REPLACE FUNCTION public.copy_sketch_toc_item_recursive(parent_id integer, type public.sketch_child_type, append_copy_to_name boolean) RETURNS integer
LANGUAGE plpgsql
security definer
AS $$
declare
copy_id int;
child_copy_id int;
is_collection boolean;
child record;
begin
if type = 'sketch' then
if it_me((select user_id from sketches where id = parent_id)) = false then
raise exception 'Permission denied';
end if;
else
if it_me((select user_id from sketch_folders where id = parent_id)) = false then
raise exception 'Permission denied';
end if;
end if;
SET session_replication_role = replica;
if type = 'sketch' then
-- copy it and get the copy id
select id, is_collection(sketch_class_id) from copy_sketch(parent_id) into copy_id, is_collection;
-- When copying a sketch from the forum, make sure its folder_id and collection_id are cleared
if ((select shared_in_forum from sketches where id = parent_id)) then
update sketches set collection_id = null, folder_id = null where id = copy_id;
end if;
if append_copy_to_name = true then
update sketches set name = name || ' (copy)' where id = copy_id;
end if;
if is_collection then
-- copy subfolders and sub-sketches
FOR child IN SELECT * FROM get_children_of_collection(parent_id)
LOOP
raise notice 'copying %', child.type;
select copy_sketch_toc_item_recursive(child.id, child.type, false) into child_copy_id;
raise notice 'assigning collection_id=%', copy_id;
if child.type = 'sketch_folder' then
update sketch_folders set collection_id = copy_id, folder_id = null where id = child_copy_id;
else
update sketches set collection_id = copy_id, folder_id = null where id = child_copy_id;
end if;
END LOOP;
end if;
elsif type = 'sketch_folder' then
-- copy it and get the copy id
select id from copy_sketch_folder(parent_id) into copy_id;
-- When copying a sketch from the forum, make sure its folder_id and collection_id are cleared
if ((select shared_in_forum from sketch_folders where id = parent_id)) then
update sketch_folders set collection_id = null, folder_id = null where id = copy_id;
end if;
if append_copy_to_name = true then
update sketch_folders set name = name || ' (copy)' where id = copy_id;
end if;
-- copy subfolders and sub-sketches
FOR child IN SELECT * FROM get_children_of_folder(parent_id)
LOOP
raise notice 'copying %', child.type;
select copy_sketch_toc_item_recursive(child.id, child.type, false) into child_copy_id;
raise notice 'assigning folder_id=%', copy_id;
if child.type = 'sketch_folder' then
update sketch_folders set folder_id = copy_id, collection_id = null where id = child_copy_id;
else
update sketches set folder_id = copy_id, collection_id = null where id = child_copy_id;
end if;
END LOOP;
end if;
SET session_replication_role = default;
return copy_id;
end;
$$;
2 changes: 1 addition & 1 deletion packages/api/migrations/current.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-- Enter migration here
-- Enter migration here
Loading

0 comments on commit 55ce468

Please sign in to comment.