Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to dbt-adapters and dbt-common #706

Merged
merged 12 commits into from
Jan 29, 2024
6 changes: 6 additions & 0 deletions .changes/unreleased/Dependencies-20240124-111727.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Dependencies
body: Migrate to dbt-common and dbt-adapters
time: 2024-01-24T11:17:27.580348-08:00
custom:
Author: colin-rogers-dbt
PR: "706"
10 changes: 5 additions & 5 deletions dbt/adapters/redshift/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import sqlparse
import redshift_connector
from dbt.adapters.exceptions import FailedToConnectError
from dbt.common.clients import agate_helper
from dbt_common.clients import agate_helper
from redshift_connector.utils.oids import get_datatype_name

from dbt.adapters.sql import SQLConnectionManager
from dbt.adapters.contracts.connection import AdapterResponse, Connection, Credentials
from dbt.adapters.events.logging import AdapterLogger
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.common.exceptions import DbtRuntimeError, CompilationError, DbtDatabaseError
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_common.exceptions import DbtRuntimeError, CompilationError, DbtDatabaseError


class SSLConfigError(CompilationError):
Expand Down
10 changes: 5 additions & 5 deletions dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from dataclasses import dataclass
from dbt.common.contracts.constraints import ConstraintType
from dbt_common.contracts.constraints import ConstraintType
from typing import Optional, Set, Any, Dict, Type
from collections import namedtuple
from dbt.adapters.base import PythonJobHelper
Expand All @@ -11,7 +11,7 @@
from dbt.adapters.events.logging import AdapterLogger


import dbt.adapters.exceptions
import dbt_common.exceptions

from dbt.adapters.redshift import RedshiftConnectionManager, RedshiftRelation

Expand Down 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.common.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.common.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.common.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 @@ -9,7 +9,7 @@
RelationResults,
)
from dbt.adapters.base import RelationType
from dbt.common.exceptions import DbtRuntimeError
from dbt_common.exceptions import DbtRuntimeError

from dbt.adapters.redshift.relation_configs import (
RedshiftMaterializedViewConfig,
Expand Down
4 changes: 2 additions & 2 deletions dbt/adapters/redshift/relation_configs/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
RelationConfigValidationMixin,
RelationConfigValidationRule,
)
from dbt.common.dataclass_schema import StrEnum
from dbt.common.exceptions import DbtRuntimeError
from dbt_common.dataclass_schema import StrEnum
from dbt_common.exceptions import DbtRuntimeError
from typing_extensions import Self

from dbt.adapters.redshift.relation_configs.base import RedshiftRelationConfigBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
RelationConfigValidationRule,
)
from dbt.adapters.contracts.relation import ComponentName, RelationConfig
from dbt.common.exceptions import DbtRuntimeError
from dbt_common.exceptions import DbtRuntimeError
from typing_extensions import Self

from dbt.adapters.redshift.relation_configs.base import RedshiftRelationConfigBase
Expand Down
4 changes: 2 additions & 2 deletions dbt/adapters/redshift/relation_configs/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
RelationConfigValidationMixin,
RelationConfigValidationRule,
)
from dbt.common.dataclass_schema import StrEnum
from dbt.common.exceptions import DbtRuntimeError
from dbt_common.dataclass_schema import StrEnum
from dbt_common.exceptions import DbtRuntimeError
from typing_extensions import Self

from dbt.adapters.redshift.relation_configs.base import RedshiftRelationConfigBase
Expand Down
3 changes: 1 addition & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# install latest changes in dbt-core + dbt-postgres
# TODO: how to switch from HEAD to x.y.latest branches after minor releases?
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter
git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-postgres&subdirectory=plugins/postgres

# if version 1.x or greater -> pin to major version
Expand Down
41 changes: 4 additions & 37 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,6 @@ def _plugin_version() -> str:
return attributes["version"]


def _core_patch(plugin_patch: str):
"""
Determines the compatible dbt-core patch given this plugin's patch

Args:
plugin_patch: the version patch of this plugin
"""
pre_release_phase = "".join([i for i in plugin_patch if not i.isdigit()])
if pre_release_phase:
if pre_release_phase not in ["a", "b", "rc"]:
raise ValueError(f"Invalid prerelease patch: {plugin_patch}")
return f"0{pre_release_phase}1"
return "0"


# require a compatible minor version (~=) and prerelease if this is a prerelease
def _core_version(plugin_version: str = _plugin_version()) -> str:
"""
Determine the compatible dbt-core version give this plugin's version.

We assume that the plugin must agree with `dbt-core` down to the minor version.

Args:
plugin_version: the version of this plugin, this is an argument in case we ever want to unit test this
"""
try:
# *_ may indicate a dev release which won't affect the core version needed
major, minor, plugin_patch, *_ = plugin_version.split(".", maxsplit=3)
except ValueError:
raise ValueError(f"Invalid version: {plugin_version}")

return f"{major}.{minor}.{_core_patch(plugin_patch)}"


setup(
name="dbt-redshift",
version=_plugin_version(),
Expand All @@ -82,11 +48,12 @@ def _core_version(plugin_version: str = _plugin_version()) -> str:
packages=find_namespace_packages(include=["dbt", "dbt.*"]),
include_package_data=True,
install_requires=[
f"dbt-core~={_core_version()}",
f"dbt-postgres~={_core_version()}",
"dbt-common<1.0",
"dbt-adapters~=0.1.0a1",
"dbt-postgres~=1.8.0",
colin-rogers-dbt marked this conversation as resolved.
Show resolved Hide resolved
# dbt-redshift depends deeply on this package. it does not follow SemVer, therefore there have been breaking changes in previous patch releases
# Pin to the patch or minor version, and bump in each new minor version of dbt-redshift.
"redshift-connector==2.0.915",
"redshift-connector<=2.0.918, >=2.0.913",
# installed via dbt-core but referenced directly; don't pin to avoid version conflicts with dbt-core
"agate",
],
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.common.exceptions import DbtRuntimeError
from dbt_common.exceptions import DbtRuntimeError
from dbt.context.providers import generate_runtime_model_context


Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_redshift_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from multiprocessing import get_context
from unittest import mock

from dbt_common.exceptions import DbtRuntimeError
from unittest.mock import Mock, call

import agate
Expand All @@ -12,7 +14,7 @@
RedshiftAdapter,
Plugin as RedshiftPlugin,
)
from dbt.common.clients import agate_helper
from dbt_common.clients import agate_helper
from dbt.adapters.exceptions import FailedToConnectError
from dbt.adapters.redshift.connections import RedshiftConnectMethodFactory, RedshiftSSLConfig
from .utils import (
Expand Down Expand Up @@ -514,7 +516,7 @@ def test_dbname_verification_is_case_insensitive(self):

def test_execute_with_fetch(self):
cursor = mock.Mock()
table = dbt.common.clients.agate_helper.empty_table()
table = agate_helper.empty_table()
with mock.patch.object(self.adapter.connections, "add_query") as mock_add_query:
mock_add_query.return_value = (
None,
Expand Down Expand Up @@ -553,9 +555,7 @@ def test_add_query_with_no_cursor(self):
self.adapter.connections, "get_thread_connection"
) as mock_get_thread_connection:
mock_get_thread_connection.return_value = None
with self.assertRaisesRegex(
dbt.common.exceptions.DbtRuntimeError, "Tried to run invalid SQL: on <None>"
):
with self.assertRaisesRegex(DbtRuntimeError, "Tried to run invalid SQL: on <None>"):
self.adapter.connections.add_query(sql="")
mock_get_thread_connection.assert_called_once()

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import agate
import pytest
from dbt.common.dataclass_schema import ValidationError
from dbt_common.dataclass_schema import ValidationError
from dbt.config.project import PartialProject


Expand Down Expand Up @@ -233,7 +233,7 @@ def assert_fails_validation(dct, cls):
class TestAdapterConversions(TestCase):
@staticmethod
def _get_tester_for(column_type):
from dbt.common.clients import agate_helper
from dbt_common.clients import agate_helper

if column_type is agate.TimeDelta: # dbt never makes this!
return agate.TimeDelta()
Expand Down
Loading