|
1. Put the above code in |
|
|
|
|
|
{% materialization transient_if_dev_table, adapter='snowflake' %} |
|
{%- set identifier = model['alias'] -%} |
|
|
|
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%} |
|
{%- set target_relation = api.Relation.create(identifier=identifier, |
|
schema=schema, |
|
database=database, type='table') -%} |
|
|
|
{{ run_hooks(pre_hooks, inside_transaction=False) }} |
|
|
|
-- `BEGIN` happens here: |
|
{{ run_hooks(pre_hooks, inside_transaction=True) }} |
|
|
|
{#-- Drop the relation if it was a view to "convert" it in a table. This may lead to |
|
-- downtime, but it should be a relatively infrequent occurrence #} |
|
{% if old_relation is not none and not old_relation.is_table %} |
|
{{ log("Dropping relation " ~ old_relation ~ " because it is of type " ~ old_relation.type) }} |
|
{{ drop_relation_if_exists(old_relation) }} |
|
{% endif %} |
|
|
|
--build model |
|
{% call statement('main') -%} |
|
{{ create_transient_table_in_dev_as(false, target_relation, sql) }} |
|
{%- endcall %} |
|
|
|
{{ run_hooks(post_hooks, inside_transaction=True) }} |
|
|
|
-- `COMMIT` happens here |
|
{{ adapter.commit() }} |
|
|
|
{{ run_hooks(post_hooks, inside_transaction=False) }} |
|
|
|
{{ return({'relations': [target_relation]}) }} |
|
|
|
{% endmaterialization %} |
|
|
|
|
|
{% macro create_transient_table_in_dev_as(temporary, relation, sql) -%} |
|
{%- set transient = (target.name=='dev') -%} |
|
|
|
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%} |
|
{%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%} |
|
{%- set copy_grants = config.get('copy_grants', default=false) -%} |
|
{%- if cluster_by_keys is not none and cluster_by_keys is string -%} |
|
{%- set cluster_by_keys = [cluster_by_keys] -%} |
|
{%- endif -%} |
|
{%- if cluster_by_keys is not none -%} |
|
{%- set cluster_by_string = cluster_by_keys|join(", ")-%} |
|
{% else %} |
|
{%- set cluster_by_string = none -%} |
|
{%- endif -%} |
|
{%- set sql_header = config.get('sql_header', none) -%} |
|
|
|
{{ sql_header if sql_header is not none }} |
|
|
|
create or replace {% if temporary -%} |
|
temporary |
|
{%- elif transient -%} |
|
transient |
|
{%- endif %} table {{ relation }} {% if copy_grants and not temporary -%} copy grants {%- endif %} as |
|
( |
|
{%- if cluster_by_string is not none -%} |
|
select * from( |
|
{{ sql }} |
|
) order by ({{ cluster_by_string }}) |
|
{%- else -%} |
|
{{ sql }} |
|
{%- endif %} |
|
); |
|
{% if cluster_by_string is not none and not temporary -%} |
|
alter table {{relation}} cluster by ({{cluster_by_string}}); |
|
{%- endif -%} |
|
{% if enable_automatic_clustering and cluster_by_string is not none and not temporary -%} |
|
alter table {{relation}} resume recluster; |
|
{%- endif -%} |
|
|
|
{% endmacro %} |