Sorry about the thread necro but wanted to add to this body of knowledge for folks that are wanting to do blue/green deploys but with schema swaps instead in a single dbt Cloud job (should work with dbt-core too) that has the following steps:
-
dbt run
on theblue_
schema. -
dbt test
on theblue_
schema. -
dbt run-operation swap_schema
to swapblue_
schema with thegreen_
schema. -
dbt docs generate
on thegreen_
schema.
Note: the swap_schema
macro is left as an exercise to the reader.
With a custom generate_schema_name
macro like so:
{% macro generate_schema_name(custom_schema_name, node) -%}
{%- set default_schema = target.schema -%}
{%- if var('is_pre_swap') is not none -%}
blue_{{ default_schema }}
{%- else -%}
green_{{ default_schema }}
{% endif -%}
{%- endmacro %}
And a sequence of run steps like so:
$ dbt run --vars 'is_pre_swap: 1'
$ dbt test --vars 'is_pre_swap: 1'
$ dbt run-operation swap_schema
$ dbt docs generate
This allows a single dbt Cloud job to do what we want since at the final step (docs generate
), the is_pre_swap
variable will be evaluated to none
so dbt will look to generate docs on the green_
schemas correctly.