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

refactor: Make SQLSink a generic with a SQLConnector type parameter #2564

Merged
Merged
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
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
Loading