dbt using macro

In dbt we can easily create macro to automate common use tasks. One of that would be a audit log operation. 

In  your folder called macro, please create a file called "audit_operation.sql".


{% macro audit_operation(activity_type) %}
  insert into raw.audit (run_id, TYPE_ACITIVITY) values ('{{ 1 }}', '{{ activity_type }}')
{% endmacro %}

And then we can use it in our dbt project like so. Instead of placing the SQL code in the commented code, we use macro. 


name: jaffle_shop

profile: jaffle_shop

seed-paths: ["seeds"]
model-paths: ["models"]
macro-paths: ["macros"]
test-paths: ["tests"]           # ← Make sure this exists
clean-targets:
  - "target"
  - "dbt_packages"

seeds:
  jaffle_shop:
    +schema: raw

# on-run-start:  "insert into raw.audit (run_id, TYPE_ACITIVITY) values ('1', 'pre-hook')"
on-run-start: "{{ audit_operation('macro-pre-hook') }}"




Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20