Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[METASVC-111] SQL: Add StoredProc to replace a collection's UUID #32

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
CREATE OR REPLACE PROCEDURE replace_collection_uuid(original uuid, new_uuid uuid)
LANGUAGE plpgsql
AS $$
declare
urlaliases url_aliases[] := '{}';
urlalias url_aliases%rowtype;

ccs collection_collections[] := '{}';
cc collection_collections%rowtype;

cds collection_digitalobjects[] := '{}';
cd collection_digitalobjects%rowtype;
begin
if original is null or new_uuid is null then
raise exception 'Both parameters (original, new_uuid) must not be null!';
end if;

-- store & remove url aliases
for urlalias in
delete from url_aliases
where target_uuid = original
returning *
loop
-- change target uuid
urlalias.target_uuid := new_uuid;
urlaliases := urlaliases || urlalias;
end loop;

-- store & remove collection_collections entries
for cc in
delete from collection_collections
where child_collection_uuid = original
returning *
loop
-- change child collection's uuid
cc.child_collection_uuid := new_uuid;
ccs := ccs || cc;
end loop;

-- store & remove collection_digitalobjects entries
for cd in
delete from collection_digitalobjects
where collection_uuid = original
returning *
loop
cd.collection_uuid := new_uuid;
cds := cds || cd;
end loop;

update collections set uuid = new_uuid where uuid = original;
if not found then
raise info 'Table collections was not updated, % not found.', original;
end if;

-- reinsert formerly removed data sets
if cardinality(urlaliases) = 0 then
raise info 'There were no url aliases to update, target % not found.', original;
else
foreach urlalias in array urlaliases loop
insert into url_aliases values (urlalias.*);
end loop;
end if;

if cardinality(ccs) = 0 then
raise info 'Table collection_collections was not updated, child UUID % not found.', original;
else
foreach cc in array ccs loop
insert into collection_collections values (cc.*);
end loop;
end if;

if cardinality(cds) = 0 then
raise info 'Table collection_digitalobjects was not updated, collection UUID % not found.',
original;
else
foreach cd in array cds loop
insert into collection_digitalobjects values (cd.*);
end loop;
end if;
end;
$$;

Loading