forked from gooddata/gooddata-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_gooddata.sql
More file actions
52 lines (48 loc) · 1.71 KB
/
Copy pathimport_gooddata.sql
File metadata and controls
52 lines (48 loc) · 1.71 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
-- (C) 2021 GoodData Corporation
CREATE OR REPLACE PROCEDURE execute_sql(
sqlStatement VARCHAR,
debug BOOLEAN DEFAULT FALSE
) LANGUAGE plpgsql AS $$
BEGIN
IF debug THEN
RAISE NOTICE '%', sqlStatement;
END IF;
EXECUTE sqlStatement;
END; $$;
CREATE OR REPLACE PROCEDURE import_gooddata(
workspace VARCHAR,
object_type VARCHAR,
foreign_schema VARCHAR = NULL,
numeric_max_size INT = 18,
foreign_server VARCHAR = 'multicorn_gooddata',
debug BOOLEAN = FALSE
) LANGUAGE plpgsql AS $$
DECLARE
sql_statement VARCHAR;
foreign_schema VARCHAR := coalesce(foreign_schema, workspace);
foreign_table RECORD;
view_name VARCHAR;
BEGIN
-- Recreate schema, where foreign tables will be imported
sql_statement := format('DROP SCHEMA IF EXISTS "%s" CASCADE', foreign_schema);
CALL execute_sql(sql_statement, debug);
sql_statement := format('CREATE SCHEMA "%s"', foreign_schema);
CALL execute_sql(sql_statement, debug);
-- Import GoodData objects as foreign tables into the schema created above
sql_statement := format(
'IMPORT FOREIGN SCHEMA "%s" FROM SERVER "%s" INTO "%s" OPTIONS (object_type ''%s'', numeric_max_size ''%s'')',
workspace, foreign_server, foreign_schema, object_type, numeric_max_size
);
CALL execute_sql(sql_statement, debug);
FOR foreign_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = foreign_schema AND table_type = 'FOREIGN' LOOP
view_name := foreign_table.table_name || '_view';
sql_statement := format(
'CREATE OR REPLACE VIEW "%s"."%s" AS SELECT * FROM "%s"."%s"',
foreign_schema, view_name, foreign_schema, foreign_table.table_name
);
CALL execute_sql(sql_statement, debug);
END LOOP;
END; $$;