Guest User

Untitled

a guest
May 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. ------------------------------------------
  2. -- Some SQL to Delete a Data Cube Product
  3. ------------------------------------------
  4.  
  5. --
  6. -- COUNT NUMBER OF DATASETS OF EACH TYPE (including archived)
  7. --
  8. select
  9. count(*),
  10. t.name
  11. from dataset
  12. left join dataset_type t on dataset.dataset_type_ref = t.id
  13. group by t.name;
  14.  
  15. --
  16. -- CHECK FOR LINEAGE RECORDS
  17. --
  18.  
  19. -- Are there any datasets that are descendents of this product?
  20. -- If so, they will need to be removed first!
  21. select count(*)
  22. from dataset_source
  23. left join dataset d on dataset_source.source_dataset_ref = d.id
  24. where d.dataset_type_ref = (select id
  25. from dataset_type
  26. where dataset_type.name = 'ls8_fc_albers_archived');
  27.  
  28. -- Are there any lineage records which need deleting?
  29. -- These are the lineage history of the product we're deleting.
  30. select count(*)
  31. from dataset_source
  32. left join dataset d on dataset_source.dataset_ref = d.id
  33. where d.dataset_type_ref = (select id
  34. from dataset_type
  35. where dataset_type.name = 'ls8_fc_albers_archived');
  36. --
  37. -- DELETE LINEAGE RECORDS
  38. --
  39. WITH datasets as (SELECT id
  40. FROM dataset
  41. where dataset.dataset_type_ref = (select id
  42. FROM dataset_type
  43. WHERE name = 'ls8_fc_albers_archived'))
  44. DELETE FROM dataset_source
  45. USING datasets
  46. where dataset_source.dataset_ref = datasets.id;
  47.  
  48.  
  49. --
  50. -- CHECK FOR LOCATION RECORDS
  51. --
  52. select count(*)
  53. from dataset_location
  54. left join dataset d on dataset_location.dataset_ref = d.id
  55. where d.dataset_type_ref = (select id
  56. from dataset_type
  57. where dataset_type.name = 'ls8_fc_albers_archived');
  58.  
  59.  
  60. WITH datasets as (SELECT id
  61. FROM dataset
  62. where dataset.dataset_type_ref = (select id
  63. FROM dataset_type
  64. WHERE name = 'ls8_fc_albers_archived'))
  65. select count(*)
  66. from dataset_location, datasets
  67. where dataset_location.dataset_ref = datasets.id;
  68.  
  69.  
  70. --
  71. -- DELETE LOCATION RECORDS
  72. --
  73. WITH datasets as (SELECT id
  74. FROM dataset
  75. where dataset.dataset_type_ref = (select id
  76. FROM dataset_type
  77. WHERE name = 'ls8_fc_albers_archived'))
  78. DELETE FROM dataset_location
  79. USING datasets
  80. where dataset_location.dataset_ref = datasets.id;
  81.  
  82. --
  83. -- DELETE DATASET RECORDS
  84. --
  85. DELETE FROM dataset
  86. where dataset.dataset_type_ref = (select id
  87. from dataset_type
  88. where dataset_type.name = 'ls8_fc_albers_archived');
  89.  
  90.  
  91. --
  92. -- FINALLY, DELETE THE PRODUCT
  93. --
  94. DELETE FROM dataset_type
  95. where dataset_type.name = 'ls8_fc_albers_archived';
Add Comment
Please, Sign In to add comment