how to test macro with relation to parameter

The problem I’m having

I have a macro which takes a parameter of relation.

{% macro apply_abcd (relation) %}

I want to test this macro by running
dbt modelling run-operation apply_abcd

how i do pass the relation parameter (this is actually a table built within the project)

I do not want to add the macro to dbt_project.yml without testing the macro on its own.

The context of why I’m trying to do this

some other non-dbt-process removes some of the policy tags on some tables based on some condition.

The macro i’m creating is to apply the policy tags where the policy tags have been removed by the above process

What I’ve already tried

Some example code or error messages

Put code inside backticks
  to preserve indentation
    which is especially important 
      for Python and YAML! 

Hey @dbt_user_suga, assuming you’re using the {{ var('...') }} function in your macro, as per the docs here.

You should then be able to execute the following command (also seen in the docs):
dbt run --vars 'relation: <value>'

Where is the literal string value that you want to pass in.

Hi @dbt_user_suga
dbt run-operation {macro} --args '{args}' Specify the macro to invoke. dbt will call this macro with the supplied arguments and then exit .--args supply arguments to the macro. This dictionary will be mapped to the keyword arguments defined in the selected macro.

dbt run-operation macro_anme --args '{"arg1": "value", "arg2": 20180101}'
dbt run-operation macro_two --args '{arg1: value, date: 20180101}'
dbt run-operation macro_anme --args 'arg1: value'

for more information regarding macros you can go through this blog post

I will give a simpler example. My macro code is something like below

{%- macro drop_table(relation) -%}
{%- if execute -%}
    {%- set temp_table_identifier -%}
       {{ relation.database + '.' + relation.schema + '.' + relation.identifier}}
    {%- endset -%}
    {%- set query -%}
       DROP TABLE {{ temp_table_identifier }};
    {%- endset -%}
{%- endif -%}
{%- endmacro -%}

I have 2 tables created in my dbt project TableA, TableB.

If i test my macro like below:
dbt run-operation drop_table --args '{relation: TableA}'

throws an error like ‘str object’ has no attribute 'database’. This means the value being passed (TableA) is considered as a string whereas it is a table which is of type relation.

please let me know how to test my macro? Thanks

@dbt_user_suga I believe you’ll need to pass in all of the attributes so that the database/schema/identifier can be assigned. Should look something like:

dbt run-operation drop_table --args '{"relation": {"database": "my_database", "schema": "my_schema", "identifier": "TableA"}}'

Replacing my_database and my_schema with your relevant values.

1 Like

if u already have a model in dbt for the table u want to drop, you can use {{ref()}} inside your macro

{%- macro drop_table(relation) -%}
    {%- set query -%}
       DROP TABLE {{ ref(relation) }};
    {%- endset -%}
    {{ return(query) }}
{%- endmacro -%}

you need not to pass database, schema to macro as arguments

@Surya How do I pass the arguments when i run your updated macro
dbt run-operation drop_table --args ???

dbt run-operation drop_table --args '{relation: TableA}'

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.