Skip to content

Commit 992ee95

Browse files
test: Test with singer-sdk @ main
1 parent 4f4b9c7 commit 992ee95

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

poetry.lock

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ sqlalchemy = "~=2.0"
4040
sshtunnel = "0.4.0"
4141

4242
[tool.poetry.dependencies.singer-sdk]
43-
version = "~=0.42.0b1"
43+
git = "https://github.com/meltano/sdk.git"
44+
branch = "edgarrmondragon/refactor/jsonschematosql-fromconfig"
4445

4546
[tool.poetry.extras]
4647
faker = ["faker"]

target_postgres/connector.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,24 @@
4141
class JSONSchemaToPostgres(JSONSchemaToSQL):
4242
"""Convert JSON Schema types to Postgres types."""
4343

44-
def __init__(self, *, content_encoding: bool = True) -> None:
44+
def __init__(self, *, content_encoding: bool = True, **kwargs):
4545
"""Initialize the JSONSchemaToPostgres instance."""
46-
super().__init__()
46+
super().__init__(**kwargs)
4747
self.content_encoding = content_encoding
4848

49+
@classmethod
50+
def from_config(
51+
cls: type[JSONSchemaToPostgres],
52+
config: dict,
53+
*,
54+
max_varchar_length: int | None = None,
55+
) -> JSONSchemaToPostgres:
56+
"""Create a JSONSchemaToPostgres instance from a configuration."""
57+
return cls(
58+
content_encoding=config.get("interpret_content_encoding", False),
59+
max_varchar_length=max_varchar_length,
60+
)
61+
4962
def handle_raw_string(self, schema):
5063
"""Handle a raw string type."""
5164
if self.content_encoding and schema.get("contentEncoding") == "base16":
@@ -63,6 +76,8 @@ class PostgresConnector(SQLConnector):
6376
allow_merge_upsert: bool = True # Whether MERGE UPSERT is supported.
6477
allow_temp_tables: bool = True # Whether temp tables are supported.
6578

79+
jsonschema_to_sql_converter = JSONSchemaToPostgres
80+
6681
def __init__(self, config: dict) -> None:
6782
"""Initialize a connector to a Postgres database.
6883
@@ -100,18 +115,6 @@ def __init__(self, config: dict) -> None:
100115
sqlalchemy_url=url.render_as_string(hide_password=False),
101116
)
102117

103-
@cached_property
104-
def interpret_content_encoding(self) -> bool:
105-
"""Whether to interpret schema contentEncoding to set the column type.
106-
107-
It is an opt-in feature because it might result in data loss if the
108-
actual data does not match the schema's advertised encoding.
109-
110-
Returns:
111-
True if the feature is enabled, False otherwise.
112-
"""
113-
return self.config.get("interpret_content_encoding", False)
114-
115118
def prepare_table( # type: ignore[override] # noqa: PLR0913
116119
self,
117120
full_table_name: str | FullyQualifiedName,
@@ -258,7 +261,10 @@ def _handle_array_type(self, jsonschema: dict) -> ARRAY | JSONB:
258261
@cached_property
259262
def jsonschema_to_sql(self) -> JSONSchemaToSQL:
260263
"""Return a JSONSchemaToSQL instance with custom type handling."""
261-
to_sql = JSONSchemaToPostgres(content_encoding=self.interpret_content_encoding)
264+
to_sql = JSONSchemaToPostgres.from_config(
265+
self.config,
266+
max_varchar_length=self.max_varchar_length,
267+
)
262268
to_sql.fallback_type = TEXT
263269
to_sql.register_type_handler("integer", BIGINT)
264270
to_sql.register_type_handler("object", JSONB)

0 commit comments

Comments
 (0)