From e8351a8926c469af6607a4fe162c2bf3b4d1793b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Wed, 30 Oct 2024 14:13:31 -0600 Subject: [PATCH] Support custom fallbacks --- singer_sdk/connectors/sql.py | 22 +++++++++++++++++++++- tests/core/test_connector_sql.py | 7 +++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/singer_sdk/connectors/sql.py b/singer_sdk/connectors/sql.py index 0aa09e770..0cd27364a 100644 --- a/singer_sdk/connectors/sql.py +++ b/singer_sdk/connectors/sql.py @@ -238,6 +238,8 @@ def __init__(self) -> None: "ipv6": lambda _: sa.types.VARCHAR(45), } + self._fallback_type = sa.types.VARCHAR + def _invoke_handler( # noqa: PLR6301 self, handler: JSONtoSQLHandler, @@ -256,6 +258,24 @@ def _invoke_handler( # noqa: PLR6301 return handler() # type: ignore[no-any-return] return handler(schema) + @property + def fallback_type(self) -> type[sa.types.TypeEngine]: + """Return the fallback type. + + Returns: + The fallback type. + """ + return self._fallback_type + + @fallback_type.setter + def fallback_type(self, value: type[sa.types.TypeEngine]) -> None: + """Set the fallback type. + + Args: + value: The new fallback type. + """ + self._fallback_type = value + def register_type_handler(self, json_type: str, handler: JSONtoSQLHandler) -> None: """Register a custom type handler. @@ -385,7 +405,7 @@ def to_sql_type(self, schema: dict) -> sa.types.TypeEngine: return sql_type # Fallback - return sa.types.VARCHAR() + return self.fallback_type() class SQLConnector: # noqa: PLR0904 diff --git a/tests/core/test_connector_sql.py b/tests/core/test_connector_sql.py index c15a0c2ed..dc987a39b 100644 --- a/tests/core/test_connector_sql.py +++ b/tests/core/test_connector_sql.py @@ -597,6 +597,13 @@ def test_anyof_unknown(self, json_schema_to_sql: JSONSchemaToSQL): result = json_schema_to_sql.to_sql_type(jsonschema_type) assert isinstance(result, sa.types.VARCHAR) + def test_custom_fallback(self): + json_schema_to_sql = JSONSchemaToSQL() + json_schema_to_sql.fallback_type = sa.types.CHAR + jsonschema_type = {"cannot": "compute"} + result = json_schema_to_sql.to_sql_type(jsonschema_type) + assert isinstance(result, sa.types.CHAR) + @pytest.mark.parametrize( "jsonschema_type,expected_type", [