-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_odc_product.sql
More file actions
102 lines (89 loc) · 3 KB
/
delete_odc_product.sql
File metadata and controls
102 lines (89 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
--------------------------------------
-- SQL to Delete a Data Cube Product
--------------------------------------
-- by Damien Ayers
-- https://gist.github.com/omad
-- Use with psql from the command line:
--
-- psql -v product_name=<product-to-delete> -f delete_odc_product.sql
--
--
-- COUNT NUMBER OF DATASETS OF EACH TYPE (including archived)
--
-- select
-- count(*),
-- t.name
-- from dataset
-- left join dataset_type t on dataset.dataset_type_ref = t.id
-- group by t.name;
--
-- CHECK FOR LINEAGE RECORDS
--
-- Are there any datasets that are descendents of this product?
-- If so, they will need to be removed first!
set search_path = agdc;
select count(*)
from dataset_source
left join dataset d on dataset_source.source_dataset_ref = d.id
where d.dataset_type_ref = (select id
from dataset_type
where dataset_type.name = :'product_name');
-- Are there any lineage records which need deleting?
-- These are the lineage history of the product we're deleting.
select count(*)
from dataset_source
left join dataset d on dataset_source.dataset_ref = d.id
where d.dataset_type_ref = (select id
from dataset_type
where dataset_type.name = :'product_name');
--
-- DELETE LINEAGE RECORDS
--
WITH datasets as (SELECT id
FROM dataset
where dataset.dataset_type_ref = (select id
FROM dataset_type
WHERE name = :'product_name'))
DELETE FROM dataset_source
USING datasets
where dataset_source.dataset_ref = datasets.id;
--
-- CHECK FOR LOCATION RECORDS
--
select count(*)
from dataset_location
left join dataset d on dataset_location.dataset_ref = d.id
where d.dataset_type_ref = (select id
from dataset_type
where dataset_type.name = :'product_name');
WITH datasets as (SELECT id
FROM dataset
where dataset.dataset_type_ref = (select id
FROM dataset_type
WHERE name = :'product_name'))
select count(*)
from dataset_location, datasets
where dataset_location.dataset_ref = datasets.id;
--
-- DELETE LOCATION RECORDS
--
WITH datasets as (SELECT id
FROM dataset
where dataset.dataset_type_ref = (select id
FROM dataset_type
WHERE name = :'product_name'))
DELETE FROM dataset_location
USING datasets
where dataset_location.dataset_ref = datasets.id;
--
-- DELETE DATASET RECORDS
--
DELETE FROM dataset
where dataset.dataset_type_ref = (select id
from dataset_type
where dataset_type.name = :'product_name');
--
-- FINALLY, DELETE THE PRODUCT
--
DELETE FROM dataset_type
where dataset_type.name = :'product_name';