Skip to content

Commit 6e51674

Browse files
committed
Add unload macro example
1 parent 82b46c8 commit 6e51674

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

macros/snowflake/dont_do_this.sql

Lines changed: 0 additions & 18 deletions
This file was deleted.

macros/snowflake/unloading.sql

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{% macro create_unload_external_stage() %}
2+
{% if env_var('DBT_ENVIRONMENT', 'dev') == 'prod' %}
3+
{% set sql %}
4+
CREATE OR REPLACE STAGE dbt_ext_unload_stage
5+
url='s3://your-company-artifacts/snowflake-unload/dbt/'
6+
credentials=(aws_key_id='{{ env_var('DBT_SECRET_AWS_ACCESS_KEY_ID') }}', aws_secret_key='{{ env_var('DBT_SECRET_AWS_SECRET_ACCESS_KEY') }}')
7+
file_format = (type = csv, field_optionally_enclosed_by = '"', compression = gzip);
8+
{% endset %}
9+
{% set _ = run_query(sql) %}
10+
{% endif %}
11+
{% endmacro %}
12+
13+
{% macro unload_to_s3() %}
14+
-- Another example here: https://gist.github.com/jeremyyeo/f07dbe9a7687ffc4976e1488a8e35547
15+
16+
{% if env_var('DBT_ENVIRONMENT', 'dev') == 'prod' %}
17+
18+
{% set sql %}
19+
20+
-- Unload (possibly multiple) data files into timestamped directory.
21+
COPY INTO @dbt_ext_unload_stage/{{ this.name }}/{{ run_started_at.date().isoformat() }}/{{ run_started_at.isoformat() }}/data
22+
FROM {{ this }}
23+
header = true;
24+
25+
-- Add an indicator that the unload has completed.
26+
COPY INTO @dbt_ext_unload_stage/{{ this.name }}/{{ run_started_at.date().isoformat() }}/{{ run_started_at.isoformat() }}/done.csv
27+
FROM (SELECT 1 AS done)
28+
single = true;
29+
30+
{% endset %}
31+
32+
{% if execute %}
33+
34+
{% do run_query(sql) %}
35+
36+
{% endif %}
37+
38+
{% endif %}
39+
{% endmacro %}

0 commit comments

Comments
 (0)