dbt-macros

Build dbt Jinja macros for reusable SQL with adapter.dispatch and run_query guards.

14|1|Updated May 5, 2026
One-click install
npx skills add https://github.com/ivanshamaev/de-agent-skills --skill dbt-macros
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dbt-macros
Source: https://github.com/ivanshamaev/de-agent-skills/tree/main/skills/dbt_macros
Command: npx skills add https://github.com/ivanshamaev/de-agent-skills --skill dbt-macros

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you write reusable dbt Jinja macros that compile reliably and behave correctly across environments and warehouses, reducing fragile copy-paste SQL and hardcoded dialect logic.

Core Features & Use Cases

  • Jinja macro fundamentals for dbt: Use expressions, control flow, filters, whitespace control, and context variables to generate correct SQL.
  • Safe dynamic execution: Build macros that run warehouse queries only when dbt is executing (not during compile/docs), using run_query and execute guards.
  • Cross-database portability: Implement dialect-specific behavior with adapter.dispatch and override built-in dbt macros for naming/aliasing/schema conventions.
  • Production-grade macro patterns: Create common building blocks like dynamic pivots, unioning relations by column alignment, audit columns, grants, idempotent DDL, and column-existence checks.

Quick Start

Activate the skill when your task is to implement a cross-database dbt macro that safely uses run_query and adapter.dispatch to generate dynamic SQL for models.

Frequently Asked Questions about dbt-macros

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write cross-database dbt macros that work across different warehouses?

Cross-database dbt macros use adapter.dispatch patterns to route execution to dialect-specific implementations. This ensures your Jinja macros generate correct SQL across different warehouses by overriding built-in dbt macros for naming and aliasing conventions.

Why does my dbt macro run queries during compile or docs generation?

Your dbt macro lacks execute guards. You must wrap run_query and DDL execution in an execute variable check so warehouse queries only run when dbt is executing, preventing unintended database calls during compile or docs generation.

How do I use adapter.dispatch to override built-in dbt macros?

Adapter.dispatch overrides built-in dbt macros by routing Jinja macro calls to dialect-specific implementations. You create default and warehouse-specific macro versions, allowing dbt to select the correct SQL generation logic based on the active adapter.

What dbt context variables do I need to handle when writing dynamic SQL macros?

Writing dynamic SQL macros requires correct handling of dbt context variables including this, target, model, and execute. These variables provide environment-aware behavior, schema and alias overrides, and safe execution state checks for your Jinja logic.

Can I use run_query in dbt macros without causing execution errors?

You can use run_query safely by adding execute guards to your dbt macros. This prevents warehouse queries from running during compilation or docs generation, ensuring dynamic SQL execution only occurs when dbt is actively running models.

What are common production-grade dbt macro patterns for data transformation?

Common production-grade dbt macro patterns include dynamic pivots, unioning relations by column alignment, audit columns, grants, idempotent DDL, and column-existence checks. These reusable Jinja building blocks reduce fragile copy-paste SQL and hardcoded dialect logic.