From ef15701aa8c39c1641269d46874dca8d2631d86c Mon Sep 17 00:00:00 2001 From: Mila Page <67295367+VersusFacit@users.noreply.github.com> Date: Thu, 18 Aug 2022 00:52:43 -0700 Subject: [PATCH] Change postgres name truncation logic to be overridable. (#5656) * Change postgres name truncation logic to be overridable. Add exception with debugging instructions. * Add changelog. Co-authored-by: Mila Page --- .../unreleased/Fixes-20220815-230409.yaml | 7 +++++++ core/dbt/adapters/cache.py | 20 +++++++++++++++++-- .../dbt/include/postgres/macros/adapters.sql | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 .changes/unreleased/Fixes-20220815-230409.yaml diff --git a/.changes/unreleased/Fixes-20220815-230409.yaml b/.changes/unreleased/Fixes-20220815-230409.yaml new file mode 100644 index 00000000000..3a3c0ba9a90 --- /dev/null +++ b/.changes/unreleased/Fixes-20220815-230409.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: Finishing logic upgrade to Redshift for name truncation collisions. +time: 2022-08-15T23:04:09.173645-07:00 +custom: + Author: versusfacit + Issue: "5586" + PR: "5656" diff --git a/core/dbt/adapters/cache.py b/core/dbt/adapters/cache.py index 593bd16851b..e58e6f9849c 100644 --- a/core/dbt/adapters/cache.py +++ b/core/dbt/adapters/cache.py @@ -1,3 +1,4 @@ +import re import threading from copy import deepcopy from typing import Any, Dict, Iterable, List, Optional, Set, Tuple @@ -413,9 +414,24 @@ def _check_rename_constraints(self, old_key, new_key): :raises InternalError: If the new key is already present. """ if new_key in self.relations: + # Tell user when collision caused by model names truncated during + # materialization. + match = re.search("__dbt_backup|__dbt_tmp$", new_key.identifier) + if match: + truncated_model_name_prefix = new_key.identifier[: match.start()] + message_addendum = ( + "\n\nName collisions can occur when the length of two " + "models' names approach your database's builtin limit. " + "Try restructuring your project such that no two models " + "share the prefix '{}'.".format(truncated_model_name_prefix) + + " Then, clean your warehouse of any removed models." + ) + else: + message_addendum = "" + dbt.exceptions.raise_cache_inconsistent( - "in rename, new key {} already in cache: {}".format( - new_key, list(self.relations.keys()) + "in rename, new key {} already in cache: {}{}".format( + new_key, list(self.relations.keys()), message_addendum ) ) diff --git a/plugins/postgres/dbt/include/postgres/macros/adapters.sql b/plugins/postgres/dbt/include/postgres/macros/adapters.sql index 11e79078e1d..6b0ee3a7d0f 100644 --- a/plugins/postgres/dbt/include/postgres/macros/adapters.sql +++ b/plugins/postgres/dbt/include/postgres/macros/adapters.sql @@ -146,7 +146,7 @@ {% set suffix = suffix ~ dtstring %} {% endif %} {% set suffix_length = suffix|length %} - {% set relation_max_name_length = 63 %} + {% set relation_max_name_length = base_relation.relation_max_name_length() %} {% if suffix_length > relation_max_name_length %} {% do exceptions.raise_compiler_error('Relation suffix is too long (' ~ suffix_length ~ ' characters). Maximum length is ' ~ relation_max_name_length ~ ' characters.') %} {% endif %}