From a2a2c89e29752ac29ef5c2ae99e0895e5267ff72 Mon Sep 17 00:00:00 2001 From: Greg Clarke Date: Wed, 31 Jul 2024 14:13:29 -0400 Subject: [PATCH] adding config option for table and incremental materializaions --- .../macros/materializations/incremental.sql | 2 ++ .../macros/materializations/table.sql | 2 ++ .../macros/utils/run_script_headers.sql | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 dbt/include/bigquery/macros/utils/run_script_headers.sql diff --git a/dbt/include/bigquery/macros/materializations/incremental.sql b/dbt/include/bigquery/macros/materializations/incremental.sql index 3908bedc2..4be116859 100644 --- a/dbt/include/bigquery/macros/materializations/incremental.sql +++ b/dbt/include/bigquery/macros/materializations/incremental.sql @@ -86,6 +86,8 @@ {{ run_hooks(pre_hooks) }} + {{ bigquery__run_script_headers(config.get('script_headers')) }} + {% if partition_by.copy_partitions is true and strategy != 'insert_overwrite' %} {#-- We can't copy partitions with merge strategy --#} {% set wrong_strategy_msg -%} The 'copy_partitions' option requires the 'incremental_strategy' option to be set to 'insert_overwrite'. diff --git a/dbt/include/bigquery/macros/materializations/table.sql b/dbt/include/bigquery/macros/materializations/table.sql index e3c5b3598..9546753f5 100644 --- a/dbt/include/bigquery/macros/materializations/table.sql +++ b/dbt/include/bigquery/macros/materializations/table.sql @@ -11,6 +11,8 @@ {{ run_hooks(pre_hooks) }} + {{ bigquery__run_script_headers(config.get('script_headers')) }} + {# We only need to drop this thing if it is not a table. If it _is_ already a table, then we can overwrite it without downtime diff --git a/dbt/include/bigquery/macros/utils/run_script_headers.sql b/dbt/include/bigquery/macros/utils/run_script_headers.sql new file mode 100644 index 000000000..68079536f --- /dev/null +++ b/dbt/include/bigquery/macros/utils/run_script_headers.sql @@ -0,0 +1,21 @@ +{% macro bigquery__run_script_headers(script_headers) %} + {%- if script_headers is not none -%} + {%- if script_headers is string -%} + {%- set script_headers = [script_headers] -%} + {%- endif -%} + + {%- set formatted_script_headers = [] -%} + + {%- for script_header in script_headers -%} + {%- set trimmed_script_header = script_header.strip() -%} + {%- if not trimmed_script_header.endswith(';') -%} + {%- set formatted_script_header = trimmed_script_header ~ ';' -%} + {%- else -%} + {%- set formatted_script_header = trimmed_script_header -%} + {%- endif -%} + {%- do formatted_script_headers.append(formatted_script_header) -%} + {%- endfor -%} + + {{ formatted_script_headers | join('\n') }} + {%- endif -%} +{%- endmacro -%}