diff --git a/README.md b/README.md index 8a889b6..aad5e04 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,38 @@ select * from dune.dune__tmp_.dbt_template_view_model All templates are in `models/templates/`. +## Table Visibility + +By default, all tables are **private** — only your team can query them. To make a table publicly accessible (visible and queryable by anyone on Dune), set `meta.dune.public: true` in the model config: + +```sql +{{ config( + alias = 'my_model' + , materialized = 'table' + , meta = { + "dune": { + "public": true + } + } + , properties = { + "partitioned_by": "ARRAY['block_date']" + } +) }} +``` + +To make all models in a folder public, set it in `dbt_project.yml`: + +```yaml +models: + my_project: + public_models: + +meta: + dune: + public: true +``` + +Visibility is only applied in the `prod` target and has no effect in development. Setting visibility for views is not supported at this time. + ## GitHub Actions ### CI Workflow (Pull Requests) diff --git a/dbt_project.yml b/dbt_project.yml index da479fa..a6c5e84 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -34,6 +34,8 @@ models: +materialized: view # fallback default, materialized should be overriden in model specific configs +view_security: invoker # required security setting for views +post-hook: + - sql: "{{ set_table_visibility(this, model.config.materialized) }}" + transaction: true - sql: "{{ optimize_table(this, model.config.materialized) }}" transaction: true - sql: "{{ vacuum_table(this, model.config.materialized) }}" diff --git a/macros/dune_dbt_overrides/set_table_visibility.sql b/macros/dune_dbt_overrides/set_table_visibility.sql new file mode 100644 index 0000000..b6fc8d2 --- /dev/null +++ b/macros/dune_dbt_overrides/set_table_visibility.sql @@ -0,0 +1,19 @@ +{%- macro dune_properties(properties) -%} + map_from_entries(ARRAY[ + {%- for key, value in properties.items() %} + ROW('{{ key }}', '{{ value }}') + {%- if not loop.last -%},{%- endif -%} + {%- endfor %} + ]) +{%- endmacro -%} + +{# post-hook that sets dune.public via ALTER TABLE on every table/incremental run (prod only). Setting visibility for views is not supported at this time. #} +{% macro set_table_visibility(this, materialization) %} +{%- if target.name == 'prod' + and materialization in ('table', 'incremental') -%} + {%- set dune_public = config.get('meta', {}).get('dune', {}).get('public', false) -%} + {%- set properties = {'dune.public': 'true' if dune_public else 'false'} -%} + ALTER TABLE {{ this }} + SET PROPERTIES extra_properties = {{ dune_properties(properties) }} +{%- endif -%} +{%- endmacro -%} diff --git a/models/templates/dbt_template_append_incremental_model.sql b/models/templates/dbt_template_append_incremental_model.sql index 1191758..7b865e9 100644 --- a/models/templates/dbt_template_append_incremental_model.sql +++ b/models/templates/dbt_template_append_incremental_model.sql @@ -9,6 +9,11 @@ alias = 'dbt_template_append_incremental_model' , materialized = 'incremental' , incremental_strategy = 'append' + , meta = { + "dune": { + "public": false + } + } , properties = { "partitioned_by": "ARRAY['block_date']" } diff --git a/models/templates/dbt_template_delete_insert_incremental_model.sql b/models/templates/dbt_template_delete_insert_incremental_model.sql index 8b7c0cb..7b727d9 100644 --- a/models/templates/dbt_template_delete_insert_incremental_model.sql +++ b/models/templates/dbt_template_delete_insert_incremental_model.sql @@ -4,6 +4,11 @@ , incremental_strategy = 'delete+insert' , unique_key = ['block_number', 'block_date'] , incremental_predicates = ["block_date >= now() - interval '1' day"] + , meta = { + "dune": { + "public": false + } + } , properties = { "partitioned_by": "ARRAY['block_date']" } diff --git a/models/templates/dbt_template_merge_incremental_model.sql b/models/templates/dbt_template_merge_incremental_model.sql index 38b15c2..daa48bc 100644 --- a/models/templates/dbt_template_merge_incremental_model.sql +++ b/models/templates/dbt_template_merge_incremental_model.sql @@ -4,6 +4,11 @@ , incremental_strategy = 'merge' , unique_key = ['block_number', 'block_date'] , incremental_predicates = ["DBT_INTERNAL_DEST.block_date >= now() - interval '1' day"] + , meta = { + "dune": { + "public": false + } + } , properties = { "partitioned_by": "ARRAY['block_date']" } diff --git a/models/templates/dbt_template_table_model.sql b/models/templates/dbt_template_table_model.sql index bf3952f..68561d2 100644 --- a/models/templates/dbt_template_table_model.sql +++ b/models/templates/dbt_template_table_model.sql @@ -1,6 +1,11 @@ {{ config( alias = 'dbt_template_table_model' , materialized = 'table' + , meta = { + "dune": { + "public": false + } + } , properties = { "partitioned_by": "ARRAY['block_date']" }