|
| 1 | +{% macro default__reset_csv_table(model, full_refresh, old_relation, agate_table) %} |
| 2 | + {% set sql = "" %} |
| 3 | + {% if full_refresh %} |
| 4 | + {{ adapter.drop_relation(old_relation) }} |
| 5 | + {% set sql = create_csv_table(model, agate_table) %} |
| 6 | + {% else %} |
| 7 | + {{ adapter.truncate_relation(old_relation) }} |
| 8 | + {% set sql = "delete from " ~ old_relation %} |
| 9 | + {% endif %} |
| 10 | + |
| 11 | + {{ return(sql) }} |
| 12 | +{% endmacro %} |
| 13 | + |
| 14 | +{% macro generate_schema_name(custom_schema_name, node) -%} |
| 15 | + |
| 16 | + {%- set default_schema = target.schema -%} |
| 17 | + {%- if custom_schema_name is none -%} |
| 18 | + |
| 19 | + {{ default_schema }} |
| 20 | + |
| 21 | + {%- else -%} |
| 22 | + |
| 23 | + {{ custom_schema_name | trim }} |
| 24 | + |
| 25 | + {%- endif -%} |
| 26 | + |
| 27 | +{%- endmacro %} |
| 28 | + |
| 29 | +{% macro postgres__create_schema(relation) -%} |
| 30 | + {%- call statement('create_schema') -%} |
| 31 | + /* schemas are not created in CrateDB */ |
| 32 | + DROP TABLE IF EXISTS thisschemadefinitelydoesnotexits.thiswouldnotexist |
| 33 | + /* but we need to run something to not have just EOF */ |
| 34 | + {% endcall %} |
| 35 | +{% endmacro %} |
| 36 | + |
| 37 | +{% macro postgres__create_table_as(temporary, relation, sql) -%} |
| 38 | + {%- set unlogged = config.get('unlogged', default=false) -%} |
| 39 | + {%- set sql_header = config.get('sql_header', none) -%} |
| 40 | + |
| 41 | + {{ sql_header if sql_header is not none }} |
| 42 | + |
| 43 | + create table {{ relation }} |
| 44 | + as ( |
| 45 | + {{ sql|replace('"crate".', "") }} |
| 46 | + ); |
| 47 | +{%- endmacro %} |
| 48 | + |
| 49 | +{% macro postgres__drop_schema(relation) -%} |
| 50 | + {% if relation.database -%} |
| 51 | + {{ adapter.verify_database(relation.database) }} |
| 52 | + {%- endif -%} |
| 53 | + {%- call statement('drop_schema') -%} |
| 54 | + /* schemas are not dropped in CrateDB */ |
| 55 | + {%- endcall -%} |
| 56 | +{% endmacro %} |
| 57 | + |
| 58 | +{% macro default__drop_relation(relation) -%} |
| 59 | + {% call statement('drop_relation', auto_begin=False) -%} |
| 60 | + drop {{ relation.type }} if exists "{{ relation.schema }}"."{{ relation.identifier }}" |
| 61 | + {%- endcall %} |
| 62 | +{% endmacro %} |
| 63 | + |
| 64 | +{% macro default__drop_schema(relation) -%} |
| 65 | + {%- call statement('drop_schema') -%} |
| 66 | + /* schemas are not dropped in CrateDB */ |
| 67 | + {% endcall %} |
| 68 | +{% endmacro %} |
| 69 | + |
| 70 | +{% macro default__create_view_as(relation, sql) -%} |
| 71 | + {%- set sql_header = config.get('sql_header', none) -%} |
| 72 | + |
| 73 | + {{ sql_header if sql_header is not none }} |
| 74 | + create view "{{ relation.schema }}"."{{ relation.identifier }}" as |
| 75 | + {{ sql|replace('"crate".', "") }} |
| 76 | + ; |
| 77 | +{%- endmacro %} |
| 78 | + |
| 79 | +{% macro postgres__rename_relation(from_relation, to_relation) -%} |
| 80 | + {% do drop_relation(to_relation) %} |
| 81 | + {% set schema_query = "SELECT table_type FROM information_schema.tables WHERE table_schema = '{}' AND table_name = '{}'".format(from_relation.schema, from_relation.identifier) %} |
| 82 | + {% set results = run_query(schema_query) %} |
| 83 | + {% if execute %} |
| 84 | + {% set results_list = results.columns[0].values() %} |
| 85 | + {% else %} |
| 86 | + {% set results_list = [] %} |
| 87 | + {% endif %} |
| 88 | + {% for relation_type in results_list %} |
| 89 | + {% if relation_type == 'VIEW' %} |
| 90 | + {% set view_query = "SELECT view_definition FROM information_schema.views WHERE table_schema = '{}' AND table_name = '{}'".format(from_relation.schema, from_relation.identifier) %} |
| 91 | + {% set view_definitions = run_query(view_query) %} |
| 92 | + {% if execute %} |
| 93 | + {% set view_definitions_list = view_definitions.columns[0].values() %} |
| 94 | + {% else %} |
| 95 | + {% set view_definitions_list = [] %} |
| 96 | + {% endif %} |
| 97 | + {% for view_definition in view_definitions_list %} |
| 98 | + {% call statement('drop_view') -%} |
| 99 | + DROP VIEW IF EXISTS {{ to_relation.schema }}.{{ to_relation.identifier }}; |
| 100 | + {%- endcall %} |
| 101 | + {% call statement('create_view') -%} |
| 102 | + CREATE VIEW {{ to_relation.schema }}.{{ to_relation.identifier }} AS {{ view_definition }} |
| 103 | + {%- endcall %} |
| 104 | + {% call statement('drop_view') -%} |
| 105 | + DROP VIEW IF EXISTS {{ from_relation.schema }}.{{ from_relation.identifier }}; |
| 106 | + {%- endcall %} |
| 107 | + {% endfor %} |
| 108 | + {% else %} |
| 109 | + {% call statement('rename_table') -%} |
| 110 | + ALTER TABLE {{ from_relation.schema }}.{{ from_relation.identifier }} |
| 111 | + RENAME TO {{ to_relation.identifier }} |
| 112 | + {%- endcall %} |
| 113 | + {% endif %} |
| 114 | + {% endfor %} |
| 115 | +{% endmacro %} |
| 116 | + |
0 commit comments