In the world of data transformation, dbt (data build tool) has emerged as a powerful tool for analytics engineers. While dbt's core functionality revolves around defining and executing data transformations, its flexibility extends further through the use of "flags," also known as "global configs." These flags act as fine-tuning mechanisms, allowing you to adjust dbt's behavior to suit specific project needs and environments.
This article delves into the world of dbt flags, exploring their purpose, how to set them, and the available options to optimize your dbt projects.
Unlike resource-specific configurations that dictate what dbt should run (e.g., which models to build), flags govern how dbt executes those tasks. Think of them as global settings that influence the overall behavior of your dbt runs.
Key characteristics of flags:
dbt offers multiple avenues for setting flags, each with its own precedence:
dbt_project.yml
: This file holds project-level defaults, ensuring consistent behavior for everyone working on the project.dbt_project.yml
or environment variables for a particular dbt invocation.Precedence: CLI options > Environment variables > dbt_project.yml
> dbt defaults.
Example:
# dbt_project.yml
flags:
fail_fast: true # Stop after the first error
# Environment variable (bash)
export DBT_FAIL_FAST=1
# CLI option
dbt run --no-fail-fast # Overrides both project config and env var
In this example, even though fail_fast
is set to true
in dbt_project.yml
and the equivalent environment variable is set, the --no-fail-fast
CLI option will take precedence, causing dbt to continue running even after encountering an error.
dbt flags encompass a wide range of functionalities. Here's a breakdown of some key flag categories and examples:
--debug
: Enables verbose logging for debugging purposes.--fail-fast
: Halts execution immediately upon encountering the first error.--full-refresh
: Forces a full refresh of incremental models, rebuilding them from scratch.--log-level
: Adjusts the verbosity of log messages (e.g., debug
, info
, warn
, error
).--log-path
: Specifies the directory where log files are stored.--use-colors
: Enables or disables colored output in the console.--partial-parse
: Enables partial parsing, speeding up dbt runs by only parsing changed files.--cache-selected-only
: Reduces metadata caching to only selected resources--state
: Specifies a directory containing dbt's state artifacts (e.g., compiled SQL, manifest files), enabling features like incremental model building and freshness checks.--defer
: Defers to the state of another environment, this is commonly used for CI/CD pipelines.Accessing Flags in Jinja:
You can access flag values within Jinja code using the flags
context variable. This allows you to dynamically adjust your dbt project's behavior based on the configured flags.
{% if flags.FAIL_FAST %}
{{ log("Fail fast is enabled!", info=True) }}
{% endif %}
Important Note: Avoid using flags as inputs to configurations or dependencies (e.g., ref
, source
) that dbt resolves during parsing. Flag values can vary across invocations, leading to unexpected behavior.
Flag Name | Type | Default | Project | Env Var | CLI Option | Cloud CLI |
---|---|---|---|---|---|---|
cache_selected_only |
boolean | False |
✅ | DBT_CACHE_SELECTED_ONLY |
--cache-selected-only , --no-cache-selected-only |
✅ |
debug |
boolean | False |
✅ | DBT_DEBUG |
--debug , --no-debug |
✅ |
defer |
boolean | False |
❌ | DBT_DEFER |
--defer , --no-defer |
✅ |
defer_state |
path | None |
❌ | DBT_DEFER_STATE |
--defer-state |
❌ |
fail_fast |
boolean | False |
✅ | DBT_FAIL_FAST |
--fail-fast , -x , --no-fail-fast |
✅ |
full_refresh |
boolean | False |
✅ | DBT_FULL_REFRESH |
--full-refresh , --no-full-refresh |
✅ |
indirect_selection |
enum | eager |
✅ | DBT_INDIRECT_SELECTION |
--indirect-selection |
❌ |
introspect |
boolean | True |
❌ | DBT_INTROSPECT |
--introspect , --no-introspect |
❌ |
log_cache_events |
boolean | False |
❌ | DBT_LOG_CACHE_EVENTS |
--log-cache-events , --no-log-cache-events |
❌ |
log_format_file |
enum | default (text) |
✅ | DBT_LOG_FORMAT_FILE |
--log-format-file |
❌ |
log_format |
enum | default (text) |
✅ | DBT_LOG_FORMAT |
--log-format |
❌ |
log_level_file |
enum | debug |
✅ | DBT_LOG_LEVEL_FILE |
--log-level-file |
❌ |
log_level |
enum | info |
✅ | DBT_LOG_LEVEL |
--log-level |
❌ |
log_path |
path | None |
❌ | DBT_LOG_PATH |
--log-path |
❌ |
partial_parse |
boolean | True |
✅ | DBT_PARTIAL_PARSE |
--partial-parse , --no-partial-parse |
✅ |
populate_cache |
boolean | True |
✅ | DBT_POPULATE_CACHE |
--populate-cache , --no-populate-cache |
✅ |
print |
boolean | True |
❌ | DBT_PRINT |
--print |
❌ |
printer_width |
int | 80 |
✅ | DBT_PRINTER_WIDTH |
--printer-width |
❌ |
profile |
string | None |
✅ | DBT_PROFILE |
--profile |
❌ |
profiles_dir |
path | None |
❌ | DBT_PROFILES_DIR |
--profiles-dir |
❌ |
project_dir |
path | ❌ | DBT_PROJECT_DIR |
--project-dir |
❌ | |
quiet |
boolean | False |
❌ | DBT_QUIET |
--quiet |
✅ |
resource-type |
string | None |
❌ | DBT_RESOURCE_TYPES |
--resource-type , --exclude-resource-type |
✅ |
send_anonymous_usage_stats |
boolean | True |
✅ | DBT_SEND_ANONYMOUS_USAGE_STATS |
--send-anonymous-usage-stats , --no-send-anonymous-usage-stats |
❌ |
source_freshness_run_project_hooks |
boolean | False |
✅ | ❌ | ||
state |
path | none |
❌ | DBT_STATE , DBT_DEFER_STATE |
--state , --defer-state |
❌ |
static_parser |
boolean | True |
✅ | DBT_STATIC_PARSER |
--static-parser , --no-static-parser |
❌ |
store_failures |
boolean | False |
✅ | DBT_STORE_FAILURES |
--store-failures , --no-store-failures |
✅ |
target_path |
path | None |
❌ | DBT_TARGET_PATH |
--target-path |
❌ |
target |
string | None |
❌ | DBT_TARGET |
--target |
❌ |
use_colors_file |
boolean | True |
✅ | DBT_USE_COLORS_FILE |
--use-colors-file , --no-use-colors-file |
❌ |
use_colors |
boolean | True |
✅ | DBT_USE_COLORS |
--use-colors , --no-use-colors |
❌ |
use_experimental_parser |
boolean | False |
✅ | DBT_USE_EXPERIMENTAL_PARSER |
--use-experimental-parser , --no-use-experimental-parser |
❌ |
version_check |
boolean | varies |
✅ | DBT_VERSION_CHECK |
--version-check , --no-version-check |
❌ |
warn_error_options |
dict | ✅ | DBT_WARN_ERROR_OPTIONS |
--warn-error-options |
✅ | |
warn_error |
boolean | False |
✅ | DBT_WARN_ERROR |
--warn-error |
✅ |
write_json |
boolean | True |
✅ | DBT_WRITE_JSON |
--write-json , --no-write-json |
✅ |
dbt flags provide a powerful mechanism for customizing and optimizing your data transformation workflows. By understanding how to set flags and the available options, you can tailor dbt's behavior to meet the specific needs of your projects, environments, and teams. Embrace the flexibility of dbt flags to unlock the full potential of your data transformation pipelines.
For further exploration, refer to the official dbt documentation. You can also explore dbt's best practices documentation to learn more about setting up your dbt project.