Skip to content

Commit

Permalink
normalize exceptions paths; remove always importing the mp context
Browse files Browse the repository at this point in the history
  • Loading branch information
VersusFacit committed Dec 14, 2023
1 parent e032b13 commit 4487ddf
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 16 deletions.
13 changes: 6 additions & 7 deletions dbt/adapters/redshift/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from dbt.common.contracts.util import Replaceable
from dbt.common.dataclass_schema import dbtClassMixin, StrEnum, ValidationError
from dbt.common.helper_types import Port
from dbt.exceptions import DbtRuntimeError, CompilationError
from dbt.common.exceptions import DbtRuntimeError, CompilationError
import dbt.flags
import dbt.mp_context

Expand All @@ -34,9 +34,6 @@ def get_message(self) -> str:
logger = AdapterLogger("Redshift")


drop_lock: Lock = dbt.mp_context._MP_CONTEXT.Lock() # type: ignore


class RedshiftConnectionMethod(StrEnum):
DATABASE = "database"
IAM = "iam"
Expand Down Expand Up @@ -279,16 +276,16 @@ def exception_handler(self, sql):
err_msg = str(e).strip()
logger.debug(f"Redshift error: {err_msg}")
self.rollback_if_open()
raise dbt.exceptions.DbtDatabaseError(err_msg) from e
raise dbt.common.exceptions.DbtDatabaseError(err_msg) from e

except Exception as e:
logger.debug("Error running SQL: {}", sql)
logger.debug("Rolling back transaction.")
self.rollback_if_open()
# Raise DBT native exceptions as is.
if isinstance(e, dbt.exceptions.DbtRuntimeError):
if isinstance(e, DbtRuntimeError):
raise
raise dbt.exceptions.DbtRuntimeError(str(e)) from e
raise DbtRuntimeError(str(e)) from e

@contextmanager
def fresh_transaction(self):
Expand All @@ -298,6 +295,8 @@ def fresh_transaction(self):
See drop_relation in RedshiftAdapter for more information.
"""
drop_lock: Lock = dbt.mp_context._MP_CONTEXT.Lock() # type: ignore

with drop_lock:
connection = self.get_thread_connection()

Expand Down
6 changes: 3 additions & 3 deletions dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def verify_database(self, database):
ra3_node = self.config.credentials.ra3_node

if database.lower() != expected.lower() and not ra3_node:
raise dbt.exceptions.NotImplementedError(
raise dbt.common.exceptions.NotImplementedError(
"Cross-db references allowed only in RA3.* node. ({} vs {})".format(
database, expected
)
Expand All @@ -109,9 +109,9 @@ def _get_catalog_schemas(self, manifest):
schemas = super(SQLAdapter, self)._get_catalog_schemas(manifest)
try:
return schemas.flatten(allow_multiple_databases=self.config.credentials.ra3_node)
except dbt.exceptions.DbtRuntimeError as exc:
except dbt.common.exceptions.DbtRuntimeError as exc:
msg = f"Cross-db references allowed only in {self.type()} RA3.* node. Got {exc.msg}"
raise dbt.exceptions.CompilationError(msg)
raise dbt.common.exceptions.CompilationError(msg)

def valid_incremental_strategies(self):
"""The set of standard builtin strategies which this adapter supports out-of-the-box.
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/redshift/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dbt.context.providers import RuntimeConfigObject
from dbt.contracts.graph.nodes import ModelNode
from dbt.adapters.base import RelationType
from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation_configs import (
RedshiftMaterializedViewConfig,
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/redshift/relation_configs/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from dbt.contracts.graph.nodes import ModelNode
from dbt.common.dataclass_schema import StrEnum
from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation_configs.base import RedshiftRelationConfigBase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from dbt.contracts.graph.nodes import ModelNode
from dbt.adapters.contracts.relation import ComponentName
from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation_configs.base import RedshiftRelationConfigBase
from dbt.adapters.redshift.relation_configs.dist import (
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/redshift/relation_configs/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from dbt.contracts.graph.nodes import ModelNode
from dbt.common.dataclass_schema import StrEnum
from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation_configs.base import RedshiftRelationConfigBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from dbt.tests.util import run_dbt, get_manifest
from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError
from dbt.context.providers import generate_runtime_model_context


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_redshift_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def test_add_query_with_no_cursor(self):
) as mock_get_thread_connection:
mock_get_thread_connection.return_value = None
with self.assertRaisesRegex(
dbt.exceptions.DbtRuntimeError, "Tried to run invalid SQL: on <None>"
dbt.common.exceptions.DbtRuntimeError, "Tried to run invalid SQL: on <None>"
):
self.adapter.connections.add_query(sql="")
mock_get_thread_connection.assert_called_once()
Expand Down

0 comments on commit 4487ddf

Please sign in to comment.