1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dbtのcustom schemasを使用する

Last updated at Posted at 2026-01-05

はじめに

この投稿はアイスタイル Advent Calendar 2025 の14日目の記事です。
こんにちは。 アイスタイルで、データ分析基盤を担当しているosadamです。
アイスタイルではELTのT(Transform)としてdbtを採用しています。

dbtを使用して開発を行っている中で困りごとが発生したので、その解決方法をまとめました。

問題

開発の際にチームメンバー各々のローカル環境からdbt buildを実行してGoogle CloudのBigQuery上で動作確認しています。
dbt_project.ymlでschemaを指定しているのですが、同じモデルを実行している時に、他の人の開発に依存し、開発体験が悪くなるという課題がありました。

models:
 models1:
    +schema: schema__models1
 models2:
   +schema: schema__models2

解決方法

custom schemasを使用しました。以下に説明が載っていますので詳しくはそちらをご覧ください。
https://docs.getdbt.com/docs/build/custom-schemas

dbt_project.ymlに変更は加えず、以下macroを作成します。

{% macro generate_schema_name(custom_schema_name, node) -%}
    {%- set default_schema = target.schema -%}
    {%- set override = none -%}
     -- 開発環境のdevのみに絞る
    {% if target.name == 'dev' %}
    -- 環境変数DBT_SCHEMA_OVERRIDEを使用して動的に変更
        {%- set override = 'test_' ~ env_var('DBT_SCHEMA_OVERRIDE') -%}
    {% endif %}

    {% if override is not none and override | trim | length > 0 %}
        {{ return(override | trim) }}
    {% endif %}

    {% if custom_schema_name is not none and custom_schema_name | trim | length > 0 %}
        {{ return(custom_schema_name | trim) }}
    {% endif %}

    {{ return(default_schema) }}

{% endmacro -%}

DBT_SCHEMA_OVERRIDEを各メンバーごとに設定し、開発環境でメンバー固有のschemaを設定しました。

最後に

custom schemasを使用することで他の人が作成したモデルに依存せず、開発体験が向上しました!
アイスタイルでは、データエンジニア絶賛募集中です!
【istyle】プロジェクトマネージャー/大規模データ分析基盤領域
【istyle】テクニカルリーダー候補/データ基盤インフラ領域
【istyle】データモデリングリード候補/データ基盤モデリング領域

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?