This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource.sql
29 lines (27 loc) · 1.62 KB
/
resource.sql
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
DROP VIEW IF EXISTS gcp_resources;
do $$
declare
tbl text;
strSQL text = '';
begin
-- iterate over every table in our information_schema that has an `arn` column available
FOR tbl IN SELECT table_name from information_schema.columns where table_name like 'gcp_%s' and COLUMN_NAME = 'project_id'
intersect
select table_name from information_schema.columns where table_name like 'gcp_%s' and COLUMN_NAME = 'id'
LOOP
-- UNION each table query to create one view
IF NOT (strSQL = ''::text) THEN
strSQL = strSQL || ' UNION ALL ';
END IF;
-- create an SQL query to select from table and transform it into our resources view schema
strSQL = strSQL || format('select cq_id, cq_meta, %L as cq_table, project_id, %s as region, id, %s as name, %s as description,
COALESCE(%s, (cq_meta->>''last_updated'')::timestamp) as fetch_date
FROM %s', tbl,
CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE column_name='region' AND table_name=tbl) THEN 'region' ELSE E'\'unavailable\'' END,
CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE column_name='name' AND table_name=tbl) THEN 'name' ELSE 'NULL' END,
CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE column_name='description' AND table_name=tbl) THEN 'description' ELSE 'NULL' END,
CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE column_name='fetch_date' AND table_name=tbl) THEN 'fetch_date' ELSE 'NULL::timestamp' END,
tbl);
END LOOP;
execute format('CREATE VIEW gcp_resources AS (%s)', strSQL);
end $$;