Skip to content

Commit

Permalink
fix: Prune old table version for incremental models (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
svdimchenko authored Dec 12, 2023
1 parent 86178a4 commit c1aa4ef
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 2 additions & 6 deletions dbt/adapters/athena/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,7 @@ def _get_glue_table_versions_to_expire(self, relation: AthenaRelation, to_keep:
return table_versions_ordered[int(to_keep) :]

@available
def expire_glue_table_versions(
self, relation: AthenaRelation, to_keep: int, delete_s3: bool
) -> List[TableVersionTypeDef]:
def expire_glue_table_versions(self, relation: AthenaRelation, to_keep: int, delete_s3: bool) -> List[str]:
conn = self.connections.get_thread_connection()
creds = conn.credentials
client = conn.handle
Expand Down Expand Up @@ -924,11 +922,9 @@ def expire_glue_table_versions(
LOGGER.debug(f"Deleted version {version} of table {relation.render()} ")
if delete_s3:
self.delete_from_s3(location)
LOGGER.debug(f"{location} was deleted")
except Exception as err:
LOGGER.debug(f"There was an error when expiring table version {version} with error: {err}")

LOGGER.debug(f"{location} was deleted")

return deleted_versions

@available
Expand Down
16 changes: 9 additions & 7 deletions dbt/include/athena/macros/adapters/persist_docs.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{% macro athena__persist_docs(relation, model, for_relation, for_columns) -%}
{% macro athena__persist_docs(relation, model, for_relation=true, for_columns=true) -%}
{% set persist_relation_docs = for_relation and config.persist_relation_docs() and model.description %}
{% set persist_column_docs = for_columns and config.persist_column_docs() and model.columns %}
{% set skip_archive_table_version = not is_incremental() %}
{% if persist_relation_docs or persist_column_docs %}
{% do adapter.persist_docs_to_glue(relation,
model,
persist_relation_docs,
persist_column_docs,
skip_archive_table_version=skip_archive_table_version) %}}
{% do adapter.persist_docs_to_glue(
relation=relation,
model=model,
persist_relation_docs=persist_relation_docs,
persist_column_docs=persist_column_docs,
skip_archive_table_version=true
)
%}}
{% endif %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% set table_type = config.get('table_type', default='hive') | lower %}
{% set strategy = validate_get_incremental_strategy(raw_strategy, table_type) %}
{% set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') %}

{% set versions_to_keep = config.get('versions_to_keep', 1) | as_number %}
{% set lf_tags_config = config.get('lf_tags_config') %}
{% set lf_grants = config.get('lf_grants') %}
{% set partitioned_by = config.get('partitioned_by') %}
Expand Down Expand Up @@ -125,6 +125,8 @@

{% do persist_docs(target_relation, model) %}

{% do adapter.expire_glue_table_versions(target_relation, versions_to_keep, False) %}

{{ return({'relations': [target_relation]}) }}

{%- endmaterialization %}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% set to_return = create_or_replace_view(run_outside_transaction_hooks=False) %}

{% do adapter.expire_glue_table_versions(target_relation, versions_to_keep, True) %}
{% do adapter.expire_glue_table_versions(target_relation, versions_to_keep, False) %}

{% set target_relation = this.incorporate(type='view') %}
{% do persist_docs(target_relation, model) %}
Expand Down

0 comments on commit c1aa4ef

Please sign in to comment.