|
create or replace function |
|
remove_deployment_metadata(sid varchar) |
|
returns integer as |
|
$$ |
|
declare |
|
metadata_count integer; |
|
begin |
|
-- Generated by store/postgres/examples/layout.rs in graph-node |
|
with dds as ( |
|
select id |
|
from subgraphs.dynamic_ethereum_contract_data_source |
|
where deployment = sid), |
|
md0 as (delete from subgraphs.dynamic_ethereum_contract_data_source e |
|
where e.deployment = sid |
|
returning e.id), |
|
md1 as (delete from subgraphs.ethereum_block_handler_entity e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md2 as (delete from subgraphs.ethereum_block_handler_filter_entity e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md3 as (delete from subgraphs.ethereum_call_handler_entity e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md4 as (delete from subgraphs.ethereum_contract_abi e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md5 as (delete from subgraphs.ethereum_contract_data_source e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md6 as (delete from subgraphs.ethereum_contract_data_source_template e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md7 as (delete from subgraphs.ethereum_contract_data_source_template_source e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md8 as (delete from subgraphs.ethereum_contract_event_handler e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md9 as (delete from subgraphs.ethereum_contract_mapping e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md10 as (delete from subgraphs.ethereum_contract_source e |
|
where left(e.id, 46) = sid or left(e.id, 40) in (select id from dds) |
|
returning e.id), |
|
md11 as (delete from subgraphs.subgraph_deployment e |
|
where left(e.id, 46) = sid |
|
returning e.id), |
|
md12 as (delete from subgraphs.subgraph_deployment_assignment e |
|
where left(e.id, 46) = sid |
|
returning e.id), |
|
md13 as (delete from subgraphs.subgraph_manifest e |
|
where left(e.id, 46) = sid |
|
returning e.id) |
|
select sum(count) into metadata_count from ( |
|
select count(*) from md0 |
|
union all |
|
select count(*) from md1 |
|
union all |
|
select count(*) from md2 |
|
union all |
|
select count(*) from md3 |
|
union all |
|
select count(*) from md4 |
|
union all |
|
select count(*) from md5 |
|
union all |
|
select count(*) from md6 |
|
union all |
|
select count(*) from md7 |
|
union all |
|
select count(*) from md8 |
|
union all |
|
select count(*) from md9 |
|
union all |
|
select count(*) from md10 |
|
union all |
|
select count(*) from md11 |
|
union all |
|
select count(*) from md12 |
|
union all |
|
select count(*) from md13 |
|
) a; |
|
return metadata_count; |
|
end; |
|
$$ language plpgsql; |
|
|
|
create or replace function |
|
log_msg(msg varchar, start timestamptz) |
|
returns void as |
|
$$ |
|
declare |
|
now timestamptz; |
|
begin |
|
now := clock_timestamp(); |
|
raise notice '% %', to_char(now - start, 'SSSS.MS'), msg; |
|
end; |
|
$$ language plpgsql; |
|
|
|
-- Remove a deployment. This removes all the data and metadata for a |
|
-- deployment. In particular, it will |
|
-- * delete entries in event_meta_data belonging to the deployment |
|
-- * delete the deployment metadata in subgraphs.entities |
|
-- * drop the schema for the deployment, deleting all its data |
|
-- * remove the row in deployment_schemas for the deployment |
|
create or replace function |
|
remove_deployment(sid varchar) |
|
returns bigint as |
|
$$ |
|
declare |
|
sgd varchar; |
|
sgd_version varchar; |
|
num_entities bigint; |
|
metadata_count int; |
|
start timestamptz; |
|
begin |
|
start := clock_timestamp(); |
|
|
|
if exists (select 1 |
|
from subgraphs.subgraph_deployment_assignment |
|
where id = sid) then |
|
raise 'Can not remove subgraph % because it is currently assigned', |
|
sid; |
|
end if; |
|
|
|
if exists (select 1 |
|
from subgraphs.subgraph s, |
|
subgraphs.subgraph_version v |
|
where (v.id = s.pending_version |
|
or v.id = s.current_version) |
|
and v.deployment = sid) then |
|
raise 'Can not remove subgraph % because it is a current or pending version of a subgraph', |
|
sid; |
|
end if; |
|
|
|
if sid not like 'Qm%' then |
|
raise 'Can not remove subgraph %, it is not a normal subgraph', |
|
sid; |
|
end if; |
|
|
|
select name, version |
|
into sgd, sgd_version |
|
from deployment_schemas |
|
where subgraph = sid; |
|
|
|
select entity_count into num_entities |
|
from subgraphs.subgraph_deployment |
|
where id = sid; |
|
|
|
raise notice 'Removing subgraph % (%)', sgd, sgd_version; |
|
raise notice ' subgraph_id: %', sid; |
|
raise notice ' entities: %', num_entities; |
|
|
|
if sgd_version = 'split' then |
|
-- We do not remove those unneeded events from event_meta_data since |
|
-- that takes an insanely long time; we just remember them so we can |
|
-- clean them up later, if needed |
|
perform log_msg(format('Saving unneeded event_ids'), start); |
|
|
|
execute format('insert into unneeded_event_ids |
|
(select distinct event_id from %I.entity_history)', sgd); |
|
end if; |
|
|
|
perform log_msg(format('Removing metadata for %s', sgd), start); |
|
metadata_count := remove_deployment_metadata(sid); |
|
|
|
perform log_msg(format('Removing schema %s', sgd), start); |
|
execute format('drop schema %I cascade', sgd); |
|
|
|
delete from deployment_schemas |
|
where subgraph = sid; |
|
|
|
delete from subgraphs.subgraph_version |
|
where deployment = sid; |
|
|
|
perform log_msg(format('Done removing %s(%s metadata entries)', |
|
sgd, metadata_count), start); |
|
return num_entities; |
|
end; |
|
$$ language plpgsql; |