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

feat: Implement drop_glue_database macro and method #766

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Implement drop_glue_database macro and method
time: 2024-12-17T00:12:38.943084+01:00
custom:
Author: svdimchenko
Issue: "768"
23 changes: 22 additions & 1 deletion dbt-athena/src/dbt/adapters/athena/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from functools import lru_cache
from textwrap import dedent
from threading import Lock
from typing import Any, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple, Type
from typing import TYPE_CHECKING, Any, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple, Type
from urllib.parse import urlparse
from uuid import uuid4

Expand Down Expand Up @@ -69,6 +69,10 @@
from dbt.adapters.contracts.relation import RelationConfig
from dbt.adapters.sql import SQLAdapter


if TYPE_CHECKING:
from mypy_boto3_glue.client import GlueClient

boto3_client_lock = Lock()


Expand Down Expand Up @@ -1173,6 +1177,23 @@ def delete_from_glue_catalog(self, relation: AthenaRelation) -> None:
LOGGER.error(e)
raise e

@available
def drop_glue_database(self, database_name: str, catalog_name: str = "awsdatacatalog") -> None:
conn = self.connections.get_thread_connection()
creds = conn.credentials
client = conn.handle
catalog = self._get_data_catalog(catalog_name)
catalog_id = get_catalog_id(catalog)

svdimchenko marked this conversation as resolved.
Show resolved Hide resolved
with boto3_client_lock:
glue_client: GlueClient = client.session.client(
"glue",
region_name=client.region_name,
config=get_boto3_config(num_retries=creds.effective_num_retries),
)
glue_client.delete_database(Name=database_name, CatalogId=catalog_id)
LOGGER.debug(f"Glue database successfully deleted: {catalog_name}.{database_name}")

@available.parse_none
def valid_snapshot_target(self, relation: BaseRelation) -> None:
"""Log an error to help developers migrate to the new snapshot logic"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
drop schema if exists {{ relation.without_identifier().render_hive() }} cascade
{% endcall %}
{% endmacro %}


{% macro drop_glue_database(database_name, catalog_name='awsdatacatalog') -%}
{{ adapter.drop_glue_database(database_name, catalog_name) }}
{% endmacro %}
12 changes: 12 additions & 0 deletions dbt-athena/tests/unit/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from dbt_common.exceptions import ConnectionError, DbtRuntimeError
from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID
from mypy_boto3_glue.client import GlueClient

from dbt.adapters.athena import AthenaAdapter
from dbt.adapters.athena import Plugin as AthenaPlugin
Expand Down Expand Up @@ -1252,6 +1253,17 @@ def test_format_unsupported_type(self):
with pytest.raises(ValueError):
self.adapter.format_value_for_partition("test", "unsupported_type")

@mock_aws
def test_drop_glue_database(self):
glue_client: GlueClient = boto3.client("glue", region_name=AWS_REGION)
test_input = {"Name": "test"}
glue_client.create_database(DatabaseInput=test_input)
database_list = glue_client.get_databases()["DatabaseList"]
assert [test_input["Name"]] == [db["Name"] for db in database_list]
self.adapter.acquire_connection("dummy")
self.adapter.drop_glue_database(database_name=test_input["Name"])
assert glue_client.get_databases()["DatabaseList"] == []


class TestAthenaFilterCatalog:
def test__catalog_filter_table(self):
Expand Down
Loading