begin;
-- data to clean up
create temporary table temp_to_cleanup_release(
id sha1_git not null
);
create temporary table temp_to_cleanup_snapshot_branches(
snapshot_id bigint not null, branch_id bigint not null
);
create or replace function backup_wrong_data_to_cleanup()
returns void
language plpgsql
as $$
declare
release_id release.id%type;
snapshot_id snapshot.object_id%type;
branch_id snapshot_branches.branch_id%type;
begin
for release_id, snapshot_id, branch_id in (
select distinct r.id, s.object_id, sbs.branch_id
from origin o
inner join origin_visit ov on (o.id=ov.origin and o.type='hg')
inner join snapshot s on ov.snapshot_id=s.object_id
inner join snapshot_branches sbs on s.object_id=sbs.snapshot_id
inner join snapshot_branch sb on sbs.branch_id=sb.object_id
inner join release r on (sb.target_type='release' and r.id=sb.target)
)
loop
-- reference the release to clean up later
insert into temp_to_cleanup_release(id)
values (release_id);
-- then cleaning up the snapshot
insert into temp_to_cleanup_snapshot_branches(snapshot_id, branch_id)
values (snapshot_id, branch_id);
-- Note: if a snapshot is mixed with target of type release and
-- other stuff, it will be identified here and cleaned up. As themarked for clean
-- up (but in current state that will fail).
-- As the future ingestion run will end up computing a new hash,
-- future ingestion run will end up computing a new hash-- thus resulting in a new identifier, thusthis needs to be cleaned
-- resulting in a new identifier-- up.
-- for-- For any other kind of snapshots (without mixed release), there
-- is no need to clean those up as the snapshot identifier will
-- remain the same with future runs (hash identifier computation
-- is idempotent)
end loop;
return;
end;
$$;
create or replace function cleanup_wrong_origin_visits()
returns void
language plpgsql
as $$
declare
origin_id origin.id%type;
begin
-- clean up those origins' metadata
for origin_id in (
select id from origin where type='hg'
)
loop
delete from fetch_history where origin=origin_id;
delete from origin_visit where origin=origin_id;
delete from occurrence_history where origin=origin_id;
delete from occurrence where origin=origin_id;
end loop;
return;
end;
$$;
create or replace function cleanup_wrong_snapshots_and_releases()
returns void
language plpgsql
as $$
declarebegin
sid snapshot.id%type;-- data to clean up
bid snapshot_branches.branch_id%type;create temporary table temp_to_cleanup_release(
begin id sha1_git not null
perform backup_wrong_data_to_cleanup(););
create temporary table temp_to_cleanup_snapshot_branches(
snapshot_id bigint not null, branch_id bigint not null
);
perform backup_wrong_data_to_cleanup();
delete from release where id in (
select distinct id
from temp_to_cleanup_release
);
for sid, bid in (delete from snapshot_branches sbs
select distinct snapshot_id, branch_id
fromusing temp_to_cleanup_snapshot_branches t
)where sbs.branch_id=t.branch_id and sbs.snapshot_id=t.snapshot_id;
-- this could fail if snapshot is also referencing other releases
loopdelete from snapshot_branch where object_id in (
delete from snapshot_select distinct branches_id
where branch_id=bid::bigint and from temp_to_cleanup_snapshot_id=sid::bigint;branches
end loop;
);
delete from snapshot_branchorigin_visit where objecsnapshot_id in (
select distinct branch_idsnapshot_id
from temp_to_cleanup_snapshot_branches
);
delete from snapshot where object_id in (
select distinct snapshot_id
from temp_to_cleanup_snapshot_branches
);
drop table temp_to_cleanup_release;
drop table temp_to_cleanup_snapshot_branches;
return;
end;
$$;
create or replace function cleanup_wrong_data()
returns void
language plpgsql
as $$
begin
perform cleanup_wrong_snapshots_and_releases();
perform cleanup_wrong_origin_visits();
return;
end;
$$;
select * from cleanup_wrong_data();
rollback;