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

Override check schema exists #452

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8f92274
add support for datashared objects and external tables for redshift l…
jiezhen-chen Apr 7, 2023
29e5a5c
changie
jiezhen-chen Apr 7, 2023
34fd237
Merge branch 'main' into fix_get_relation
jiezhen-chen Apr 7, 2023
8cf25a9
Merge branch 'main' into fix_get_relation
jiezhen-chen Apr 11, 2023
b86a826
Merge branch 'main' into fix_get_relation
dataders Apr 12, 2023
dc98d12
refactor inner joins to cte
jiezhen-chen Apr 13, 2023
7bf1978
shorten list_relations_without_caching query
jiezhen-chen Apr 13, 2023
0958f28
Merge branch 'fix_get_relation' of github.com:jiezhen-chen/dbt-redshi…
jiezhen-chen Apr 13, 2023
c69e651
add database filter to get_columns_in_relation to only query inbound …
jiezhen-chen Apr 13, 2023
bce3e88
leverage redshift_conn method for list_schemas
jiezhen-chen Apr 14, 2023
451c60d
add list_schemas method
jiezhen-chen Apr 14, 2023
4353a38
migrate from macros to redshift driver
jiezhen-chen Apr 28, 2023
b5494a6
Merge branch 'main' into redshift_list_schemas
jiezhen-chen Apr 28, 2023
4295477
merge with main
jiezhen-chen Apr 29, 2023
d408c06
add database_metadata_current_db_only to unit tests
jiezhen-chen Apr 29, 2023
98ed151
override check_schema_exists
jiezhen-chen May 1, 2023
bd4aa2f
Merge branch 'main' into override_get_schema_exists
jiezhen-chen May 12, 2023
2a35c33
override_get_schema_exists
jiezhen-chen May 13, 2023
a4848d1
Merge branch 'dbt-labs:main' into override_get_schema_exists
jiezhen-chen May 15, 2023
c4e030b
functional test check_schema_exists
jiezhen-chen May 16, 2023
eeaa4bc
Merge branch 'dbt-labs:main' into override_get_schema_exists
jiezhen-chen May 16, 2023
b43b24a
override check_schema_Exists
jiezhen-chen May 17, 2023
8e1e028
override check_schema_Exists
jiezhen-chen May 17, 2023
99ef2f0
revert changes in adapters.sql
jiezhen-chen May 17, 2023
5c16f0c
remove available decorator for list_schemas
jiezhen-chen May 17, 2023
259ec22
Merge branch 'main' into override_check_schema_exists
jiezhen-chen May 23, 2023
7746d0c
Merge branch 'main' into override_check_schema_exists
jiezhen-chen May 24, 2023
579c21a
merge with main manually
jiezhen-chen Jul 24, 2023
08f3aa2
merge with main manually
jiezhen-chen Jul 24, 2023
1385563
merge with main manually
jiezhen-chen Jul 24, 2023
b2de60a
adding functional tests for new check_schema_exists method
jiezhen-chen Jul 24, 2023
fd7aaf6
remove current_db_only param
jiezhen-chen Jul 25, 2023
de78fcc
Merge branch 'dbt-labs:main' into override_check_schema_exists
jiezhen-chen Jul 25, 2023
a988555
Update Features-20230407-104723.yaml
jiezhen-chen Jul 25, 2023
68f2c73
Update Features-20230407-104723.yaml
jiezhen-chen Jul 25, 2023
c3acee4
Merge branch 'main' into override_check_schema_exists
mikealfare Aug 11, 2023
f25c8b0
Merge branch 'main' into override_check_schema_exists
dataders Aug 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/Features-20230407-104723.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Features
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a description that identifies the change.

time: 2023-04-07T10:47:23.105369-07:00
custom:
Author: jiezhen-chen
Issue: 555
3 changes: 0 additions & 3 deletions dbt/adapters/redshift/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ def _connection_keys(self):
"schema",
"sslmode",
"region",
"sslmode",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to remove these? Is it related to this specific change?

"region",
"iam_profile",
"autocreate",
"db_groups",
"ra3_node",
Expand Down
17 changes: 16 additions & 1 deletion dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional, Set, Any, Dict, Type
from typing import Optional, Set, Any, Dict, Type, List
from collections import namedtuple
from dbt.adapters.base import PythonJobHelper
from dbt.adapters.base.impl import AdapterConfig, ConstraintSupport
Expand Down Expand Up @@ -115,6 +115,21 @@ def valid_incremental_strategies(self):
def timestamp_add_sql(self, add_to: str, number: int = 1, interval: str = "hour") -> str:
return f"{add_to} + interval '{number} {interval}'"

def _get_cursor(self):
return self.connections.get_thread_connection().handle.cursor()

def list_schemas(self, database: str, schema: Optional[str] = None) -> List[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would update the associated macro to call this, otherwise any other macro that is using the associated macro would still use the postgres macro, hence pg_ tables.

cursor = self._get_cursor()
results = []
for s in cursor.get_schemas(catalog=database, schema_pattern=schema):
results.append(s[0])
return results

@available
def check_schema_exists(self, database: str, schema: str) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as on list_schemas

results = self.list_schemas(database=database, schema=schema)
return len(results) > 0

def _link_cached_database_relations(self, schemas: Set[str]):
"""
:param schemas: The set of schemas that should have links added.
Expand Down
31 changes: 30 additions & 1 deletion tests/functional/adapter/test_adapter_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@

"""

models__get_invalid_schema = """

{% set upstream = ref('upstream') %}

{% set existing = adapter.check_schema_exists(upstream.database, "doesnotexist") %}
{% if existing == False %}
select 2 as id
{% else %}
select 1 as id
{% endif %}
"""

models__get_valid_schema = """

{% set upstream = ref('upstream') %}

{% set existing = adapter.check_schema_exists(upstream.database, upstream.schema) %}
{% if existing == True %}
select 2 as id
{% else %}
select 1 as id
{% endif %}
"""

models__upstream_sql = """
select 1 as id

Expand Down Expand Up @@ -84,6 +108,9 @@ def models(self):
"base_view.sql": "{{ config(bind=True) }} select * from {{ ref('model') }}",
"get_relation_type.sql": models__get_relation_type,
"expected_type.sql": "select 1 as valid_type",
"get_invalid_schema.sql": models__get_invalid_schema,
"get_valid_schema.sql": models__get_invalid_schema,
"get_schema_expected.sql": "select 2 as id",
}

def project_files(
Expand All @@ -104,11 +131,13 @@ def project_config_update(self):
def test_adapter_methods(self, project):
run_dbt(["compile"]) # trigger any compile-time issues
result = run_dbt()
assert len(result) == 7
assert len(result) == 10

run_dbt(["test"])
check_relations_equal(project.adapter, ["call_get_relation", "expected"])
check_relations_equal(project.adapter, ["get_relation_type", "expected_type"])
check_relations_equal(project.adapter, ["get_invalid_schema", "get_schema_expected"])
check_relations_equal(project.adapter, ["get_valid_schema", "get_schema_expected"])


class TestRedshiftAdapterMethod(RedshiftAdapterMethod):
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_redshift_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,18 @@ def mock_cursor(self, mock_get_thread_conn):
mock_handle.return_value = mock_cursor
return mock_cursor

@mock.patch("dbt.adapters.redshift.impl.RedshiftAdapter._get_cursor")
def test_list_schemas(self, mock_cursor):
mock_cursor.return_value.get_schemas.return_value = ["schema1"], ["schema2"]
results = self.adapter.list_schemas(database="somedb", schema="someschema")
self.assertTrue(results == ["schema1", "schema2"])

@mock.patch("dbt.adapters.redshift.impl.RedshiftAdapter.list_schemas")
def test_check_schema_exists(self, mock_list_schemas):
mock_list_schemas.return_value = ["schema1", "schema2"]
results = self.adapter.check_schema_exists(database="somedb", schema="someschema")
self.assertTrue(results is True)


class TestRedshiftAdapterConversions(TestAdapterConversions):
def test_convert_text_type(self):
Expand Down