Search code examples
dbt

Configure post-hook if your subdirectory is mart and staging


I want to do different things depending if my sql file was in staging subdirectory or in mart subdirectory.

More specifically i want to grant different groups permissions depending on if its mart or staging

Currently i have different models that all have staging and mart directories in them, i want to modify my post-hook so it will do different things if file was from staging dict or not.

For example right now, in dbt_project.yml file i only have 1 post-hook in my models part:

models:
  model_repo:
    +persist_docs: 
      relation: true
      columns: true
    +post-hook: 
      - "INSERT INTO {{ this }} values (NOW(),'post-hook')"

Is there a way i can have custom post-hook depending if it is from mart or staging?


Solution

  • You can specify different post-hooks for staging and mart based in the resource-path:

    models:
      model_repo:
        staging:
          +post-hook: 
            - "-- post-hook for models in models/staging/*"
        mart:
          +post-hook: 
            - "-- post-hook for models in models/mart/*"
    

    OR

    To achieve a more dynamic behavior based on the model path, you can create a macro as sugested by jtcohen6 in this comment:

    {% macro grant_some_permission_macro() %}
    
      {% set fqn = model.fqn %}
      /*{# e.g. ['my_project', 'some_parent_dir', 'some_child_dir', 'part_a', 'part_b', 'some_model'] #}*/
    
      {% set layer = fqn[2] %}
      /*{# adjust to your subfolder level that represents the layer #}*/
    
      {% if layer = 'staging' %}
      GRANT STAGING_PERMISSION TO ...
      {% elif layer = 'mart' %}
      GRANT MART_PERMISSION TO ...
      {% endif %}
    
    {% endmacro %}
    

    Then use it:

    models:
      model_repo:
        +post-hook: 
          - "{{ grant_some_permission_macro() }}"