Open4

安全にdbtコマンド打つ環境を整える

YuichiYuichi

https://docs.getdbt.com/reference/dbt-jinja-functions/flags

下記を検証する

dbt_project.ymlとマクロを利用する方法

これが最も効果的な方法です。dbt_project.ymlの**on-run-startフック**とカスタムマクロを組み合わせることで、実行時に--selectオプションがあるかチェックし、なければエラーを発生させることができます。

ステップ1:マクロの作成

macrosフォルダにcheck_select_arg.sqlのようなファイルを作成し、以下の内容を記述します。

{% macro check_select_arg() %}
  {# `dbt run`や`dbt build`などのコマンドで`--select`がない場合にエラーを発生させる #}
  {% if not invocation_args_dict.select and invocation_args_dict.which in ['run', 'build'] %}
    {{ exceptions.raise_compiler_error("Error: You must provide at least one --select argument.") }}
  {% endif %}
{% endmacro %}

このマクロは、dbt runまたはdbt buildコマンドが実行された際に、--select引数が存在しない場合にコンパイルエラーを発生させます。

ステップ2:dbt_project.ymlの設定

プロジェクトのルートにあるdbt_project.ymlファイルに以下の設定を追加します。

on-run-start:
  - "{{ check_select_arg() }}"

これにより、dbtの実行開始時に、作成したマクロcheck_select_argが呼び出されるようになります。

YuichiYuichi

{% do log(invocation_args_dict, info=True) %}

{
    "quiet": False,
    "require_nested_cumulative_type_params": False,
    "use_colors": True,
    "favor_state": False,
    "project_dir": "/workspaces/~/dbt_project",
    "include_saved_query": False,
    "use_colors_file": True,
    "version_check": True,
    "log_level_file": "debug",
    "printer_width": 80,
    "select": ("user_prefecture_history_incremental",),
    "exclude_resource_types": (),
    "vars": {},
    "log_format": "default",
    "log_level": "info",
    "partial_parse": True,
    "invocation_command": "dbt ",
    "skip_nodes_if_on_run_start_fails": False,
    "empty": False,
    "require_yaml_configuration_for_mf_time_spines": False,
    "profiles_dir": "/workspaces/~/dbt_project",
    "defer": False,
    "populate_cache": True,
    "send_anonymous_usage_stats": True,
    "introspect": True,
    "show": False,
    "state_modified_compare_vars": False,
    "resource_types": (),
    "strict_mode": False,
    "show_resource_report": False,
    "state_modified_compare_more_unrendered_values": False,
    "indirect_selection": "eager",
    "log_file_max_bytes": 10485760,
    "write_json": True,
    "print": True,
    "require_explicit_package_overrides_for_builtin_materializations": True,
    "macro_debugging": False,
    "warn_error_options": {"include": [], "exclude": []},
    "static_parser": True,
    "log_path": "/workspaces/~/dbt_project/logs",
    "exclude": (),
    "log_format_file": "debug",
    "partial_parse_file_diff": True,
    "cache_selected_only": False,
    "require_resource_names_without_spaces": False,
    "which": "build",
    "target": "dev",
    "source_freshness_run_project_hooks": False,
    "require_batched_execution_for_custom_microbatch_strategy": False,
    "export_saved_queries": False,
}

{% do log(invocation_args_dict.select, info=True) %}

('user_prefecture_history_incremental',)

{% do log(invocation_args_dict.which, info=True) %}

build
YuichiYuichi

dbt ls
{% do log(invocation_args_dict.select, info=True) %}

()

{% do log(invocation_args_dict.which, info=True) %}

ls