From a8fcd81ae264e2d3d29f4969aa3d25910cb75764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Tue, 13 Aug 2024 13:12:18 -0600 Subject: [PATCH] refactor: Backwards-compatible identifier quoting in fully qualified names --- singer_sdk/connectors/sql.py | 7 ++----- tests/core/test_connector_sql.py | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/singer_sdk/connectors/sql.py b/singer_sdk/connectors/sql.py index 24f7be756e..2d488ae150 100644 --- a/singer_sdk/connectors/sql.py +++ b/singer_sdk/connectors/sql.py @@ -52,7 +52,6 @@ def __init__( schema: str | None = None, database: str | None = None, delimiter: str = ".", - dialect: sa.engine.Dialect, ) -> None: """Initialize the fully qualified table name. @@ -61,7 +60,6 @@ def __init__( schema: The name of the schema. Defaults to None. database: The name of the database. Defaults to None. delimiter: The delimiter to use between parts. Defaults to '.'. - dialect: The SQLAlchemy dialect to use for quoting. Raises: ValueError: If the fully qualified name could not be generated. @@ -70,7 +68,6 @@ def __init__( self.schema = schema self.database = database self.delimiter = delimiter - self.dialect = dialect parts = [] if self.database: @@ -94,7 +91,7 @@ def __init__( super().__init__(self.delimiter.join(parts)) - def prepare_part(self, part: str) -> str: + def prepare_part(self, part: str) -> str: # noqa: PLR6301 """Prepare a part of the fully qualified name. Args: @@ -103,7 +100,7 @@ def prepare_part(self, part: str) -> str: Returns: The prepared part. """ - return self.dialect.identifier_preparer.quote(part) + return part class SQLConnector: # noqa: PLR0904 diff --git a/tests/core/test_connector_sql.py b/tests/core/test_connector_sql.py index 6a9a5e1893..c8390f33d7 100644 --- a/tests/core/test_connector_sql.py +++ b/tests/core/test_connector_sql.py @@ -360,32 +360,35 @@ def create_engine(self) -> Engine: def test_fully_qualified_name(): - dialect = DefaultDialect() - - fqn = FullyQualifiedName(table="my_table", dialect=dialect) + fqn = FullyQualifiedName(table="my_table") assert fqn == "my_table" - fqn = FullyQualifiedName(schema="my_schema", table="my_table", dialect=dialect) + fqn = FullyQualifiedName(schema="my_schema", table="my_table") assert fqn == "my_schema.my_table" fqn = FullyQualifiedName( database="my_catalog", schema="my_schema", table="my_table", - dialect=dialect, ) assert fqn == "my_catalog.my_schema.my_table" def test_fully_qualified_name_with_quoting(): + class QuotedFullyQualifiedName(FullyQualifiedName): + def __init__(self, *, dialect: sa.engine.Dialect, **kwargs: t.Any): + self.dialect = dialect + super().__init__(**kwargs) + + def prepare_part(self, part: str) -> str: + return self.dialect.identifier_preparer.quote(part) + dialect = DefaultDialect() - fqn = FullyQualifiedName(table="order", schema="public", dialect=dialect) + fqn = QuotedFullyQualifiedName(table="order", schema="public", dialect=dialect) assert fqn == 'public."order"' def test_fully_qualified_name_empty_error(): - dialect = DefaultDialect() - with pytest.raises(ValueError, match="Could not generate fully qualified name"): - FullyQualifiedName(dialect=dialect) + FullyQualifiedName()