diff --git a/.changes/unreleased/Fixes-20240201-124701.yaml b/.changes/unreleased/Fixes-20240201-124701.yaml new file mode 100644 index 00000000000..0c0b7d837e8 --- /dev/null +++ b/.changes/unreleased/Fixes-20240201-124701.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: Update 'compiled_code' context member logic to route based on command ('clone' + or not). Reimplement 'sql' context member as wrapper of 'compiled_code'. +time: 2024-02-01T12:47:01.488085+01:00 +custom: + Author: jtcohen6 + Issue: "9502" diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index e8aba9355e2..51b7010e109 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -1457,27 +1457,10 @@ def post_hooks(self) -> List[Dict[str, Any]]: h.to_dict(omit_none=True) for h in self.model.config.post_hook # type: ignore[union-attr] # noqa ] - @contextproperty() - def sql(self) -> Optional[str]: - # only doing this in sql model for backward compatible - if self.model.language == ModelLanguage.sql: # type: ignore[union-attr] - # If the model is deferred and the adapter doesn't support zero-copy cloning, then select * from the prod - # relation - # TODO: avoid routing on args.which if possible - if getattr(self.model, "defer_relation", None) and self.config.args.which == "clone": - # TODO https://github.com/dbt-labs/dbt-core/issues/7976 - return f"select * from {self.model.defer_relation.relation_name or str(self.defer_relation)}" # type: ignore[union-attr] - elif getattr(self.model, "extra_ctes_injected", None): - # TODO CT-211 - return self.model.compiled_code # type: ignore[union-attr] - else: - return None - else: - return None - @contextproperty() def compiled_code(self) -> Optional[str]: - if getattr(self.model, "defer_relation", None): + # TODO: avoid routing on args.which if possible + if getattr(self.model, "defer_relation", None) and self.config.args.which == "clone": # TODO https://github.com/dbt-labs/dbt-core/issues/7976 return f"select * from {self.model.defer_relation.relation_name or str(self.defer_relation)}" # type: ignore[union-attr] elif getattr(self.model, "extra_ctes_injected", None): @@ -1486,6 +1469,14 @@ def compiled_code(self) -> Optional[str]: else: return None + @contextproperty() + def sql(self) -> Optional[str]: + # only set this for sql models, for backward compatibility + if self.model.language == ModelLanguage.sql: # type: ignore[union-attr] + return self.compiled_code + else: + return None + @contextproperty() def database(self) -> str: return getattr(self.model, "database", self.config.credentials.database)