I’m firing a macro using prehook in the pub_table model. The role of the macro is to truncate the pub_table but it doesn’t truncate the table. So i checked the firing of the truncate statement using 2 versions of the macro.
Version 1: (Truncates the pub_table)
{% macro delete_existing_records(table_name, ref_table_name, max_meta_inserted_at) %}
{% if execute %}
{% set table_exists_query %}
SELECT COUNT(*)
FROM `{{ table_name.project }}.{{ table_name.dataset }}.__TABLES__`
WHERE table_id = '{{ table_name.name }}'
{% endset %}
{% set table_exists = 1 | int %}
{{ log("Table exists value: " ~ table_exists, info=True) }}
{% if table_exists > 0 %}
{{ log("Truncating table: " ~ table_name, info=True) }}
TRUNCATE TABLE {{ table_name }}
{% else %}
{{ log("Table does not exist: " ~ table_name, info=True) }}
{% endif %}
{% endif %}
{% endmacro %}
Log message:
Table exists value: 1
Truncating table: `project`.`dataset`.`pub_table`
Version 2 (what i need):(Doesn’t truncate the pub_table)
{% macro delete_existing_records(table_name, ref_table_name, max_meta_inserted_at) %}
{% if execute %}
{% set table_exists_query %}
SELECT COUNT(*)
FROM `{{ table_name.project }}.{{ table_name.dataset }}.__TABLES__`
WHERE table_id = '{{ table_name.name }}'
{% endset %}
{% set table_exists = dbt_utils.get_single_value(table_exists_query) | int %}
{{ log("Table exists value: " ~ table_exists, info=True) }}
{% if table_exists > 0 %}
{{ log("Truncating table: " ~ table_name, info=True) }}
TRUNCATE TABLE {{ table_name }}
{% else %}
{{ log("Table does not exist: " ~ table_name, info=True) }}
{% endif %}
{% endif %}
{% endmacro %}
Log message:
Table exists value: 1
Truncating table: `project`.`dataset`.`pub_table`
The macros are pretty much the same except this line,
Version 1: {% set table_exists = 1 | int %}
Version 2: {% set table_exists = dbt_utils.get_single_value(table_exists_query) | int %}
and also the log messages reveal the execution gets into the if block in both the cases but the truncate statement doesn’t truncate the table in the second version. I don’t know where I’m going wrong.