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

test: Test with singer-sdk @ main #198

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 17 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ sqlalchemy = "~=2.0"
sshtunnel = "0.4.0"

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

[tool.poetry.extras]
faker = ["faker"]
Expand Down
36 changes: 21 additions & 15 deletions target_postgres/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,24 @@
class JSONSchemaToPostgres(JSONSchemaToSQL):
"""Convert JSON Schema types to Postgres types."""

def __init__(self, *, content_encoding: bool = True) -> None:
def __init__(self, *, content_encoding: bool = True, **kwargs):
"""Initialize the JSONSchemaToPostgres instance."""
super().__init__()
super().__init__(**kwargs)
self.content_encoding = content_encoding

@classmethod
def from_config(
cls: type[JSONSchemaToPostgres],
config: dict,
*,
max_varchar_length: int | None = None,
) -> JSONSchemaToPostgres:
"""Create a JSONSchemaToPostgres instance from a configuration."""
return cls(
content_encoding=config.get("interpret_content_encoding", False),
max_varchar_length=max_varchar_length,
)

def handle_raw_string(self, schema):
"""Handle a raw string type."""
if self.content_encoding and schema.get("contentEncoding") == "base16":
Expand All @@ -63,6 +76,8 @@ class PostgresConnector(SQLConnector):
allow_merge_upsert: bool = True # Whether MERGE UPSERT is supported.
allow_temp_tables: bool = True # Whether temp tables are supported.

jsonschema_to_sql_converter = JSONSchemaToPostgres

def __init__(self, config: dict) -> None:
"""Initialize a connector to a Postgres database.

Expand Down Expand Up @@ -100,18 +115,6 @@ def __init__(self, config: dict) -> None:
sqlalchemy_url=url.render_as_string(hide_password=False),
)

@cached_property
def interpret_content_encoding(self) -> bool:
"""Whether to interpret schema contentEncoding to set the column type.

It is an opt-in feature because it might result in data loss if the
actual data does not match the schema's advertised encoding.

Returns:
True if the feature is enabled, False otherwise.
"""
return self.config.get("interpret_content_encoding", False)

def prepare_table( # type: ignore[override] # noqa: PLR0913
self,
full_table_name: str | FullyQualifiedName,
Expand Down Expand Up @@ -258,7 +261,10 @@ def _handle_array_type(self, jsonschema: dict) -> ARRAY | JSONB:
@cached_property
def jsonschema_to_sql(self) -> JSONSchemaToSQL:
"""Return a JSONSchemaToSQL instance with custom type handling."""
to_sql = JSONSchemaToPostgres(content_encoding=self.interpret_content_encoding)
to_sql = JSONSchemaToPostgres.from_config(
self.config,
max_varchar_length=self.max_varchar_length,
)
to_sql.fallback_type = TEXT
to_sql.register_type_handler("integer", BIGINT)
to_sql.register_type_handler("object", JSONB)
Expand Down