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

refactor(targets): Added a class method to instantiate JSONSchemaToSQL from the target configuration #2782

Merged

Conversation

edgarrmondragon
Copy link
Collaborator

@edgarrmondragon edgarrmondragon commented Nov 28, 2024


📚 Documentation preview 📚: https://meltano-sdk--2782.org.readthedocs.build/en/2782/

Summary by Sourcery

This pull request introduces a from_config class method to the JSONSchemaToSQL class, allowing instantiation of the class from the target configuration. It also adds a jsonschema_to_sql_converter property to SQLConnector to allow overriding the default JSONSchemaToSQL class. This change allows users to customize the JSONSchemaToSQL class using the target configuration.

Enhancements:

  • Expose a class method from_config on JSONSchemaToSQL to allow instantiation of the class from the target configuration.
  • Add jsonschema_to_sql_converter property to SQLConnector to allow overriding the default JSONSchemaToSQL class.

Documentation:

  • Update documentation to reflect the changes in the JSONSchemaToSQL class.

Copy link

codspeed-hq bot commented Nov 28, 2024

CodSpeed Performance Report

Merging #2782 will not alter performance

Comparing edgarrmondragon/refactor/jsonschematosql-fromconfig (d410295) with main (de71fed)

Summary

✅ 7 untouched benchmarks

@edgarrmondragon edgarrmondragon force-pushed the edgarrmondragon/refactor/jsonschematosql-fromconfig branch from bc026c7 to 934ef21 Compare November 28, 2024 23:23
Copy link

codecov bot commented Nov 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.33%. Comparing base (de71fed) to head (d410295).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2782      +/-   ##
==========================================
- Coverage   91.37%   91.33%   -0.04%     
==========================================
  Files          62       62              
  Lines        5203     5207       +4     
  Branches      675      675              
==========================================
+ Hits         4754     4756       +2     
- Misses        317      319       +2     
  Partials      132      132              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@edgarrmondragon edgarrmondragon self-assigned this Nov 28, 2024
@edgarrmondragon edgarrmondragon force-pushed the edgarrmondragon/refactor/jsonschematosql-fromconfig branch 3 times, most recently from 55b3d2c to 4be6949 Compare November 28, 2024 23:48
@edgarrmondragon edgarrmondragon changed the title refactor: Added a class method to instantiate JSONSchemaToSQL from the tap configuration refactor(targets): Added a class method to instantiate JSONSchemaToSQL from the target configuration Nov 30, 2024
@edgarrmondragon edgarrmondragon added the Type/Target Singer targets label Nov 30, 2024
@edgarrmondragon edgarrmondragon force-pushed the edgarrmondragon/refactor/jsonschematosql-fromconfig branch from 4be6949 to de53f85 Compare November 30, 2024 04:04
@edgarrmondragon edgarrmondragon force-pushed the edgarrmondragon/refactor/jsonschematosql-fromconfig branch from de53f85 to 42a702d Compare November 30, 2024 20:27
edgarrmondragon

This comment was marked as off-topic.

@edgarrmondragon edgarrmondragon added the SQL Support for SQL taps and targets label Jan 30, 2025
@edgarrmondragon edgarrmondragon added this to the v0.45 milestone Jan 30, 2025
@edgarrmondragon edgarrmondragon marked this pull request as ready for review February 24, 2025 16:29
@edgarrmondragon edgarrmondragon requested review from a team as code owners February 24, 2025 16:29
Copy link
Contributor

sourcery-ai bot commented Feb 24, 2025

Reviewer's Guide by Sourcery

This pull request introduces a from_config class method to the JSONSchemaToSQL class, enabling instantiation with target configuration values. It also updates the SQLConnector to utilize this new method and provides documentation updates to reflect the changes.

Updated class diagram for JSONSchemaToSQL

classDiagram
    class JSONSchemaToSQL {
        - _fallback_type: type[sa.types.TypeEngine]
        + __init__(max_varchar_length: int | None = None) : None
        + from_config(config: dict, max_varchar_length: int | None) : JSONSchemaToSQL
        + _invoke_handler(handler: JSONtoSQLHandler, json_type: str, jsonschema: dict, column_name: str) : sa.types.TypeEngine
        + register_column_name_handler(json_type: str, handler: ColumnNameHandler) : None
        + register_type_handler(json_type: str, handler: JSONtoSQLHandler) : None
        + register_format_handler(json_format: str, handler: JSONtoSQLHandler) : None
        + column_name_mapping(column_name: str) : str
        + convert_column(column_name: str, jsonschema: dict) : sa.Column
    }
Loading

Updated class diagram for SQLConnector

classDiagram
    class SQLConnector {
        - config: dict | None
        - authenticator: AuthenticatorBase | None
        - state: dict
        - _metadata_cache: dict
        + __init__(config: dict | None = None, parse_env_config: bool = False, state: dict | None = None) 
        + discover() : dict
        + _discover_catalog() : dict
        + get_catalog() : dict
        + discover_streams() : list[dict]
        + sync(*args, **kwargs) : None
        + _sync_stream(stream: SingerStream, catalog: dict) : None
        + get_records(context: dict) : t.Iterable[dict]
        + get_schema(context: dict) : dict
        + get_singer_stream(stream_name: str, catalog: dict) : SingerStream
        + _get_row_count(table_name: str) : int
        + open_connection() : sa.engine.Connection
        + bulk_insert_records(table_name: str, records: t.Iterable[dict], stream: SingerStream) : None
        + create_empty_table(stream: SingerStream, schema: dict) : None
        + jsonschema_to_sql() : JSONSchemaToSQL
        + _connect() : t.Iterator[sa.engine.Connection]
        + to_sql_type(jsonschema_type: str, jsonschema_format: str | None = None) : sa.types.TypeEngine
        + get_table(table_name: str) : sa.Table
        + prepare_schema(schema: dict) : dict
        + get_starting_replication_key_value(context: dict) : t.Any
        + get_replication_key_value(record: dict, context: dict) : t.Any
        + finalize_state_message(state_message: dict) : dict
        + write_bookmark(state: dict, stream_name: str, state_value: t.Any) : None
        + write_state_message(state: dict) : None
        + close() : None
        + ping_connection() : bool
        + validate_config(raise_errors: bool = True) : bool
        + apply_catalog(catalog: dict) : None
        + get_fully_qualified_name(table_name: str) : str
        + jsonschema_to_sql_converter: type[JSONSchemaToSQL]
        + sql_to_jsonschema_converter: type[SQLToJSONSchema]
    }
Loading

File-Level Changes

Change Details Files
Introduced a from_config class method to the JSONSchemaToSQL class, allowing instantiation with target configuration values.
  • Added a from_config class method to JSONSchemaToSQL to enable instantiation using target configuration.
  • Implemented the default behavior of from_config to pass max_varchar_length to the constructor.
  • Updated SQLConnector.jsonschema_to_sql to use JSONSchemaToSQL.from_config for instantiation.
  • Added jsonschema_to_sql_converter class attribute to SQLConnector to allow overriding the default JSONSchemaToSQL class.
singer_sdk/connectors/sql.py
Updated documentation to reflect the new from_config method for instantiating JSONSchemaToSQL.
  • Modified the documentation to use JSONSchemaToSQL.from_config when creating a custom jsonschema_to_sql property.
  • Updated the documentation to reflect the new from_config method for instantiating JSONSchemaToSQL.
docs/guides/sql-target.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @edgarrmondragon - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a test case that demonstrates the usage of from_config with a custom configuration.
  • It might be helpful to include a note about the expected structure of the config dictionary in the from_config docstring.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -574,7 +608,10 @@ def jsonschema_to_sql(self) -> JSONSchemaToSQL:

.. versionadded:: 0.42.0
"""
return JSONSchemaToSQL(max_varchar_length=self.max_varchar_length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider simplifying the code by directly instantiating the converter in the cached property and passing the config to the constructor, instead of using a from_config method.

The extra layer of abstraction may not be worth the complexity unless you plan to extend the configuration logic in the near future. If you want to simplify while keeping future customization in mind, consider directly instantiating the converter in the cached property and use the class attribute for customization when needed.

For example, instead of:

@functools.cached_property
def jsonschema_to_sql(self) -> JSONSchemaToSQL:
    return self.jsonschema_to_sql_converter.from_config(
        self.config,
        max_varchar_length=self.max_varchar_length,
    )

you might simplify it to:

@functools.cached_property
def jsonschema_to_sql(self) -> JSONSchemaToSQL:
    return self.jsonschema_to_sql_converter(
        max_varchar_length=self.max_varchar_length,
        config=self.config
    )

Additionally, update your converter's constructor to accept a config parameter if needed. This streamlines the instantiation while retaining the ability to extend behavior via subclassing.

@edgarrmondragon edgarrmondragon merged commit 4373898 into main Feb 24, 2025
36 checks passed
@edgarrmondragon edgarrmondragon deleted the edgarrmondragon/refactor/jsonschematosql-fromconfig branch February 24, 2025 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SQL Support for SQL taps and targets Type/Target Singer targets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant