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

Updating dbt-duckdb imports to fix CI pipeline #67

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions dbt/adapters/excel/connections.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass

from dbt.adapters.duckdb.connections import DuckDBConnectionManager
from dbt.adapters.duckdb.connections import DuckDBCredentials
from dbt.adapters.duckdb import DuckDBConnectionManager
from dbt.adapters.duckdb import DuckDBCredentials


@dataclass
Expand Down
57 changes: 25 additions & 32 deletions dbt/adapters/excel/relation.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from typing import Optional
from typing import Type

import pandas as pd

from .connections import ExcelConnectionManager
from dbt.adapters.base.relation import BaseRelation
from dbt.adapters.base.relation import Self
from dbt.adapters.duckdb.utils import SourceConfig
from dbt.contracts.graph.nodes import SourceDefinition


@dataclass(frozen=True, eq=False, repr=False)
class ExcelRelation(BaseRelation):
external_location: Optional[str] = None
external: Optional[str] = None

@classmethod
def create_from_source(cls: Type[Self], source: SourceDefinition, **kwargs: Any) -> Self:

if "external_location" in source.meta:
external_location = source.meta["external_location"]
elif "external_location" in source.source_meta:
external_location = source.source_meta["external_location"]
else:
external_location = None

if external_location is not None:
external_location = external_location.format(
schema=source.schema,
name=source.name,
identifier=source.identifier,
source_config = SourceConfig.create(source)
# First check to see if a 'plugin' is defined in the meta argument for
# the source or its parent configuration, and if it is, use the environment
# associated with this run to get the name of the source that we should
# reference in the compiled model
if "plugin" in source_config.meta:
plugin_name = source_config.meta["plugin"]
source_name = ExcelConnectionManager.env().load_source(plugin_name, source_config)
kwargs["external"] = source_name
elif "external_location" in source_config.meta:
# Call str.format with the schema, name and identifier for the source so that they
# can be injected into the string; this helps reduce boilerplate when all
# of the tables in the source have a similar location based on their name
# and/or identifier.
ext_location = source_config.meta["external_location"].format(
**source_config.as_dict()
)
if external_location.endswith(".xlsx"):
excel_location = Path(external_location.strip("'"))
csv_location = (
excel_location.parent / excel_location.stem / source.identifier
).with_suffix(".csv")
csv_location.parent.mkdir(exist_ok=True)
pd.read_excel(excel_location, sheet_name=source.identifier).to_csv(
csv_location, index=False
)
external_location = str(csv_location)
if "(" not in external_location and not external_location.startswith("'"):
external_location = f"'{external_location}'"
kwargs["external_location"] = external_location
# If it's a function call or already has single quotes, don't add them
if "(" not in ext_location and not ext_location.startswith("'"):
ext_location = f"'{ext_location}'"
kwargs["external"] = ext_location

return super().create_from_source(source, **kwargs) # type: ignore

def render(self) -> str:
if self.external_location:
return self.external_location
if self.external:
return self.external
else:
return super().render()
2 changes: 1 addition & 1 deletion tests/unit/test_connections.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import mock

from botocore.credentials import Credentials
from dbt.adapters.duckdb.connections import Attachment
from dbt.adapters.duckdb.credentials import Attachment

from dbt.adapters.excel.connections import ExcelCredentials

Expand Down