Skip to content

Commit 211e82a

Browse files
committed
set extra_properties at create time via properties() override, alter on incremental runs
1 parent 1bb6a60 commit 211e82a

File tree

6 files changed

+94
-7
lines changed

6 files changed

+94
-7
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{#
2+
Override of the dbt-trino adapter's properties() macro to inject dune.public
3+
into extra_properties at CREATE TABLE time.
4+
5+
Configure per model via config():
6+
, dune_public = true -- make table publicly visible on Dune
7+
, dune_public = false -- (default) keep table private
8+
9+
Or set for an entire folder in dbt_project.yml:
10+
models:
11+
my_project:
12+
public_models:
13+
+dune_public: true
14+
15+
Only runs in prod. On incremental (non-full-refresh) runs, set_table_visibility
16+
handles it via ALTER TABLE instead.
17+
#}
18+
{% macro properties(temporary=False) %}
19+
{%- set _properties = config.get('properties') -%}
20+
{%- set table_format = config.get('table_format') -%}
21+
{%- set file_format = config.get('file_format') -%}
22+
23+
{%- if file_format -%}
24+
{%- if _properties -%}
25+
{%- if _properties.format -%}
26+
{% set msg %}
27+
You can specify either 'file_format' or 'properties.format' configurations, but not both.
28+
{% endset %}
29+
{% do exceptions.raise_compiler_error(msg) %}
30+
{%- else -%}
31+
{%- do _properties.update({'format': "'" ~ file_format ~ "'"}) -%}
32+
{%- endif -%}
33+
{%- else -%}
34+
{%- set _properties = {'format': "'" ~ file_format ~ "'"} -%}
35+
{%- endif -%}
36+
{%- endif -%}
37+
38+
{%- if table_format -%}
39+
{%- if _properties -%}
40+
{%- if _properties.type -%}
41+
{% set msg %}
42+
You can specify either 'table_format' or 'properties.type' configurations, but not both.
43+
{% endset %}
44+
{% do exceptions.raise_compiler_error(msg) %}
45+
{%- else -%}
46+
{%- do _properties.update({'type': "'" ~ table_format ~ "'"}) -%}
47+
{%- endif -%}
48+
{%- else -%}
49+
{%- set _properties = {'type': "'" ~ table_format ~ "'"} -%}
50+
{%- endif -%}
51+
{%- endif -%}
52+
53+
{%- if temporary -%}
54+
{%- if _properties -%}
55+
{%- if _properties.location -%}
56+
{%- do _properties.update({'location': _properties.location[:-1] ~ "__dbt_tmp'"}) -%}
57+
{%- endif -%}
58+
{%- endif -%}
59+
{%- endif -%}
60+
61+
{#-- Inject dune.public into extra_properties at CREATE time (prod only) --#}
62+
{%- set dune_public = config.get('dune_public') -%}
63+
{%- if dune_public is not none and target.name == 'prod' -%}
64+
{%- set visibility_value = 'true' if dune_public else 'false' -%}
65+
{%- set extra_props_sql = "map_from_entries(ARRAY[ROW('dune.public', '" ~ visibility_value ~ "')])" -%}
66+
{%- if _properties is none -%}
67+
{%- set _properties = {'extra_properties': extra_props_sql} -%}
68+
{%- else -%}
69+
{%- do _properties.update({'extra_properties': extra_props_sql}) -%}
70+
{%- endif -%}
71+
{%- endif -%}
72+
73+
{%- if _properties is not none -%}
74+
WITH (
75+
{%- for key, value in _properties.items() -%}
76+
{{ key }} = {{ value }}
77+
{%- if not loop.last -%}{{ ',\n ' }}{%- endif -%}
78+
{%- endfor -%}
79+
)
80+
{%- endif -%}
81+
{%- endmacro -%}

macros/dune_dbt_overrides/set_table_visibility.sql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
{%- endmacro -%}
99

1010
{#
11-
set_table_visibility: post-hook macro that sets the dune.public extra property on tables/incrementals.
11+
set_table_visibility: post-hook that sets dune.public on incremental (non-full-refresh) runs.
12+
13+
For table materializations and incremental full-refreshes, extra_properties is set
14+
at CREATE TABLE time via the overridden properties() macro. This hook handles the
15+
incremental case where no CREATE is issued (INSERT/MERGE only).
1216

1317
Configure per model via config():
1418
, dune_public = true -- make table publicly visible on Dune
@@ -20,10 +24,12 @@
2024
public_models:
2125
+dune_public: true
2226

23-
Only runs in prod. Views are not supported yet.
27+
Only runs in prod. Views are not supported.
2428
#}
2529
{% macro set_table_visibility(this, materialization) %}
26-
{%- if target.name == 'prod' and materialization in ('table', 'incremental') -%}
30+
{%- if target.name == 'prod'
31+
and materialization == 'incremental'
32+
and not flags.FULL_REFRESH -%}
2733
{%- set dune_public = config.get('dune_public', false) -%}
2834
{%- set properties = {'dune.public': 'true' if dune_public else 'false'} -%}
2935
ALTER TABLE {{ this }}

models/templates/dbt_template_append_incremental_model.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
alias = 'dbt_template_append_incremental_model'
1010
, materialized = 'incremental'
1111
, incremental_strategy = 'append'
12-
-- , dune_public = true -- uncomment to make this table publicly visible on Dune
12+
, dune_public = false
1313
, properties = {
1414
"partitioned_by": "ARRAY['block_date']"
1515
}

models/templates/dbt_template_delete_insert_incremental_model.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
, incremental_strategy = 'delete+insert'
55
, unique_key = ['block_number', 'block_date']
66
, incremental_predicates = ["block_date >= now() - interval '1' day"]
7-
-- , dune_public = true -- uncomment to make this table publicly visible on Dune
7+
, dune_public = false
88
, properties = {
99
"partitioned_by": "ARRAY['block_date']"
1010
}

models/templates/dbt_template_merge_incremental_model.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
, incremental_strategy = 'merge'
55
, unique_key = ['block_number', 'block_date']
66
, incremental_predicates = ["DBT_INTERNAL_DEST.block_date >= now() - interval '1' day"]
7-
-- , dune_public = true -- uncomment to make this table publicly visible on Dune
7+
, dune_public = false
88
, properties = {
99
"partitioned_by": "ARRAY['block_date']"
1010
}

models/templates/dbt_template_table_model.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{ config(
22
alias = 'dbt_template_table_model'
33
, materialized = 'table'
4-
-- , dune_public = true -- uncomment to make this table publicly visible on Dune
4+
, dune_public = false
55
, properties = {
66
"partitioned_by": "ARRAY['block_date']"
77
}

0 commit comments

Comments
 (0)