From 118fbbdc26376dd13cf09b43b31e5eba4235bbe8 Mon Sep 17 00:00:00 2001 From: Doug Beatty <44704949+dbeatty10@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:45:17 -0600 Subject: [PATCH] [CT-3267] [Feature] Debug log when `type_code` fails to convert to a `data_type` (#39) Co-authored-by: Mike Alfare <13974384+mikealfare@users.noreply.github.com> Co-authored-by: colin-rogers-dbt <111200756+colin-rogers-dbt@users.noreply.github.com> Co-authored-by: Mike Alfare --- .changes/unreleased/Features-20240323-160222.yaml | 6 ++++++ dbt/adapters/postgres/connections.py | 3 +++ .../contracts/test_nonstandard_data_type.py | 15 ++++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 .changes/unreleased/Features-20240323-160222.yaml diff --git a/.changes/unreleased/Features-20240323-160222.yaml b/.changes/unreleased/Features-20240323-160222.yaml new file mode 100644 index 00000000..c5af1aca --- /dev/null +++ b/.changes/unreleased/Features-20240323-160222.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Debug log when `type_code` fails to convert to a `data_type` +time: 2024-03-23T16:02:22.153674-06:00 +custom: + Author: dbeatty10 + Issue: "8912" diff --git a/dbt/adapters/postgres/connections.py b/dbt/adapters/postgres/connections.py index 741f5701..83f26957 100644 --- a/dbt/adapters/postgres/connections.py +++ b/dbt/adapters/postgres/connections.py @@ -4,8 +4,10 @@ from dbt.adapters.contracts.connection import AdapterResponse, Credentials from dbt.adapters.events.logging import AdapterLogger +from dbt.adapters.events.types import TypeCodeNotFound from dbt.adapters.sql import SQLConnectionManager from dbt_common.exceptions import DbtDatabaseError, DbtRuntimeError +from dbt_common.events.functions import warn_or_error from dbt_common.helper_types import Port from mashumaro.jsonschema.annotations import Maximum, Minimum import psycopg2 @@ -203,4 +205,5 @@ def data_type_code_to_name(cls, type_code: Union[int, str]) -> str: if type_code in psycopg2.extensions.string_types: return psycopg2.extensions.string_types[type_code].name else: + warn_or_error(TypeCodeNotFound(type_code=type_code)) return f"unknown type_code {type_code}" diff --git a/tests/functional/contracts/test_nonstandard_data_type.py b/tests/functional/contracts/test_nonstandard_data_type.py index ee48bb3c..e563f4d1 100644 --- a/tests/functional/contracts/test_nonstandard_data_type.py +++ b/tests/functional/contracts/test_nonstandard_data_type.py @@ -1,6 +1,6 @@ import pytest -from tests.functional.utils import run_dbt, run_dbt_and_capture +from tests.functional.utils import run_dbt_and_capture my_numeric_model_sql = """ @@ -45,7 +45,9 @@ def models(self): } def test_nonstandard_data_type(self, project): - run_dbt(["run"], expect_pass=True) + expected_debug_msg = "The `type_code` 790 was not recognized" + _, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True) + assert expected_debug_msg in logs class TestModelContractUnrecognizedTypeCodeActualMismatch: @@ -58,8 +60,10 @@ def models(self): def test_nonstandard_data_type(self, project): expected_msg = "unknown type_code 790 | DECIMAL | data type mismatch" - _, logs = run_dbt_and_capture(["run"], expect_pass=False) + expected_debug_msg = "The `type_code` 790 was not recognized" + _, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=False) assert expected_msg in logs + assert expected_debug_msg in logs class TestModelContractUnrecognizedTypeCodeExpectedMismatch: @@ -72,6 +76,7 @@ def models(self): def test_nonstandard_data_type(self, project): expected_msg = "DECIMAL | unknown type_code 790 | data type mismatch" - _, logs = run_dbt_and_capture(["run"], expect_pass=False) - print(logs) + expected_debug_msg = "The `type_code` 790 was not recognized" + _, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=False) assert expected_msg in logs + assert expected_debug_msg in logs