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: Added SQLConnector.prepare_primary_key for target to implement for custom table primary key adaptation #2730

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
22 changes: 22 additions & 0 deletions singer_sdk/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,28 @@ def prepare_table(
self.to_sql_type(property_def),
)

self.prepare_primary_key(
full_table_name=full_table_name,
primary_keys=primary_keys,
)

def prepare_primary_key(
self,
*,
full_table_name: str | FullyQualifiedName, # noqa: ARG002
primary_keys: t.Sequence[str], # noqa: ARG002
) -> None:
"""Adapt target table primary key to provided schema if possible.

Implement this method in a subclass to adapt the primary key of the target table
to the provided one if possible.

Args:
full_table_name: the target table name.
primary_keys: list of key properties.
"""
self.logger.debug("Primary key adaptation is not implemented")

def prepare_column(
self,
full_table_name: str | FullyQualifiedName,
Expand Down
2 changes: 2 additions & 0 deletions singer_sdk/testing/suites.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
TargetInvalidSchemaTest,
TargetNoPrimaryKeys,
TargetOptionalAttributes,
TargetPrimaryKeyUpdates,
TargetRecordBeforeSchemaTest,
TargetRecordMissingKeyProperty,
TargetRecordMissingOptionalFields,
Expand Down Expand Up @@ -91,7 +92,7 @@


# Target Test Suites
target_tests = TestSuite(

Check warning on line 95 in singer_sdk/testing/suites.py

View workflow job for this annotation

GitHub Actions / Check API Changes

target_tests

Attribute value was changed: `TestSuite(kind='target', tests=[TargetArrayData, TargetCamelcaseComplexSchema, TargetCamelcaseTest, TargetCliPrintsTest, TargetDuplicateRecords, TargetEncodedStringData, TargetInvalidSchemaTest, TargetNoPrimaryKeys, TargetOptionalAttributes, TargetRecordBeforeSchemaTest, TargetRecordMissingKeyProperty, TargetRecordMissingOptionalFields, TargetSchemaNoProperties, TargetSchemaUpdates, TargetSpecialCharsInAttributes])` -> `TestSuite(kind='target', tests=[TargetArrayData, TargetCamelcaseComplexSchema, TargetCamelcaseTest, TargetCliPrintsTest, TargetDuplicateRecords, TargetEncodedStringData, TargetInvalidSchemaTest, TargetNoPrimaryKeys, TargetOptionalAttributes, TargetPrimaryKeyUpdates, TargetRecordBeforeSchemaTest, TargetRecordMissingKeyProperty, TargetRecordMissingOptionalFields, TargetSchemaNoProperties, TargetSchemaUpdates, TargetSpecialCharsInAttributes])`
kind="target",
tests=[
TargetArrayData,
Expand All @@ -104,6 +105,7 @@
# TargetMultipleStateMessages,
TargetNoPrimaryKeys,
TargetOptionalAttributes,
TargetPrimaryKeyUpdates,
TargetRecordBeforeSchemaTest,
TargetRecordMissingKeyProperty,
TargetRecordMissingOptionalFields,
Expand Down
4 changes: 4 additions & 0 deletions singer_sdk/testing/target_test_streams/pk_updates.singer
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"type": "SCHEMA", "stream": "example_stream", "schema": {"properties": {"id": {"type": "integer"}, "name": {"type": "string"}, "email": {"type": "string"}}, "key_properties": ["id"]}}
{"type": "RECORD", "stream": "example_stream", "record": {"id": 1, "name": "Alice", "email": "[email protected]"}}
{"type": "SCHEMA", "stream": "example_stream", "schema": {"properties": {"id": {"type": "integer"}, "name": {"type": "string"}, "email": {"type": "string"}}, "key_properties": ["email"]}}
{"type": "RECORD", "stream": "example_stream", "record": {"id": 2, "name": "Bob", "email": "[email protected]"}}
6 changes: 6 additions & 0 deletions singer_sdk/testing/target_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class TargetSchemaUpdates(TargetFileTestTemplate):
name = "schema_updates"


class TargetPrimaryKeyUpdates(TargetFileTestTemplate):
"""Test Target handles Primary Key updates."""

name = "pk_updates"


class TargetSpecialCharsInAttributes(TargetFileTestTemplate):
"""Test Target handles special chars in attributes."""

Expand Down
8 changes: 2 additions & 6 deletions tests/core/test_connector_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ def test_column_rename(self, connector: DuckDBConnector):
connector.rename_column("test_table", "old_name", "new_name")

with engine.connect() as conn:
result = conn.execute(
sa.text("SELECT * FROM test_table"),
)
result = conn.execute(sa.text("SELECT * FROM test_table"))
assert result.keys() == ["id", "new_name"]

def test_adapt_column_type(self, connector: DuckDBConnector):
Expand All @@ -341,9 +339,7 @@ def test_adapt_column_type(self, connector: DuckDBConnector):
connector._adapt_column_type("test_table", "name", sa.types.String())

with engine.connect() as conn:
result = conn.execute(
sa.text("SELECT * FROM test_table"),
)
result = conn.execute(sa.text("SELECT * FROM test_table"))
assert result.keys() == ["id", "name"]
assert result.cursor.description[1][1] == "STRING"

Expand Down
Loading