|
| 1 | +{% materialization bridge_incremental, default -%} |
| 2 | + |
| 3 | + {% set full_refresh_mode = flags.FULL_REFRESH %} |
| 4 | + |
| 5 | + {% set target_relation = this %} |
| 6 | + {% set existing_relation = load_relation(this) %} |
| 7 | + {% set tmp_relation = make_temp_relation(this) %} |
| 8 | + |
| 9 | + {{ run_hooks(pre_hooks, inside_transaction=False) }} |
| 10 | + |
| 11 | + -- `BEGIN` happens here: |
| 12 | + {{ run_hooks(pre_hooks, inside_transaction=True) }} |
| 13 | + |
| 14 | + {% set to_drop = [] %} |
| 15 | + {% if existing_relation is none %} |
| 16 | + {% set build_sql = create_table_as(False, target_relation, sql) %} |
| 17 | + {% elif existing_relation.is_view or full_refresh_mode %} |
| 18 | + {#-- Make sure the backup doesn't exist so we don't encounter issues with the rename below #} |
| 19 | + {% set backup_identifier = existing_relation.identifier ~ "__dbt_backup" %} |
| 20 | + {% set backup_relation = existing_relation.incorporate(path={"identifier": backup_identifier}) %} |
| 21 | + {% do adapter.drop_relation(backup_relation) %} |
| 22 | + |
| 23 | + {% do adapter.rename_relation(target_relation, backup_relation) %} |
| 24 | + {% set build_sql = create_table_as(False, target_relation, sql) %} |
| 25 | + {% do to_drop.append(backup_relation) %} |
| 26 | + {% else %} |
| 27 | + |
| 28 | + {% set tmp_relation = make_temp_relation(target_relation) %} |
| 29 | + {% do run_query(create_table_as(True, tmp_relation, sql)) %} |
| 30 | + {% do adapter.expand_target_column_types( |
| 31 | + from_relation=tmp_relation, |
| 32 | + to_relation=target_relation) %} |
| 33 | + {% set build_sql = dbtvault.incremental_bridge_replace(tmp_relation, target_relation) %} |
| 34 | +{% endif %} |
| 35 | + |
| 36 | + {% call statement("main") %} |
| 37 | + {{ build_sql }} |
| 38 | + {% endcall %} |
| 39 | + |
| 40 | + {{ run_hooks(post_hooks, inside_transaction=True) }} |
| 41 | + |
| 42 | + -- `COMMIT` happens here |
| 43 | + {% do adapter.commit() %} |
| 44 | + |
| 45 | + {% for rel in to_drop %} |
| 46 | + {% do adapter.drop_relation(rel) %} |
| 47 | + {% endfor %} |
| 48 | + |
| 49 | + {{ run_hooks(post_hooks, inside_transaction=False) }} |
| 50 | + |
| 51 | + {{ return({'relations': [target_relation]}) }} |
| 52 | + |
| 53 | +{%- endmaterialization %} |
0 commit comments