Skip to content

Commit

Permalink
refactor: Make SQLSink a generic with a SQLConnector type paramet…
Browse files Browse the repository at this point in the history
…er (#2564)
  • Loading branch information
edgarrmondragon authored Jul 25, 2024
1 parent e0197cd commit 078cffd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion samples/sample_target_sqlite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_sqlalchemy_url(self, config: dict[str, t.Any]) -> str: # noqa: PLR6301
return f"sqlite:///{config[DB_PATH_CONFIG]}"


class SQLiteSink(SQLSink):
class SQLiteSink(SQLSink[SQLiteConnector]):
"""The Sink class for SQLite.
This class allows developers to optionally override `get_records()` and other
Expand Down
12 changes: 7 additions & 5 deletions singer_sdk/sinks/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@

from singer_sdk.target_base import Target

_C = t.TypeVar("_C", bound=SQLConnector)

class SQLSink(BatchSink):

class SQLSink(BatchSink, t.Generic[_C]):
"""SQL-type sink type."""

connector_class: type[SQLConnector]
connector_class: type[_C]
soft_delete_column_name = "_sdc_deleted_at"
version_column_name = "_sdc_table_version"

Expand All @@ -37,7 +39,7 @@ def __init__(
stream_name: str,
schema: dict,
key_properties: t.Sequence[str] | None,
connector: SQLConnector | None = None,
connector: _C | None = None,
) -> None:
"""Initialize SQL Sink.
Expand All @@ -48,12 +50,12 @@ def __init__(
key_properties: The primary key columns.
connector: Optional connector to reuse.
"""
self._connector: SQLConnector
self._connector: _C
self._connector = connector or self.connector_class(dict(target.config))
super().__init__(target, stream_name, schema, key_properties)

@property
def connector(self) -> SQLConnector:
def connector(self) -> _C:
"""The connector object.
Returns:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class SQLConnectorMock(SQLConnector):
"""A Mock SQLConnector class."""


class SQLSinkMock(SQLSink):
class SQLSinkMock(SQLSink[SQLConnectorMock]):
"""A mock Sink class."""

name = "sql-sink-mock"
Expand Down

0 comments on commit 078cffd

Please sign in to comment.