-
Notifications
You must be signed in to change notification settings - Fork 73
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
fix: Use a SQLAlchemy to generate an insert statement #2843
Merged
edgarrmondragon
merged 1 commit into
main
from
edgarrmondragon/fix/sqlalchemy-insert-statement
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,12 @@ | |
|
||
import re | ||
import typing as t | ||
import warnings | ||
from collections import defaultdict | ||
from copy import copy | ||
from textwrap import dedent | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.sql import quoted_name | ||
from sqlalchemy.sql import insert | ||
from sqlalchemy.sql.expression import bindparam | ||
|
||
from singer_sdk.connectors import SQLConnector | ||
|
@@ -282,19 +282,26 @@ def generate_insert_statement( | |
Returns: | ||
An insert statement. | ||
""" | ||
property_names = list(self.conform_schema(schema)["properties"].keys()) | ||
column_identifiers = [ | ||
self.connector.quote(quoted_name(name, quote=True)) | ||
for name in property_names | ||
] | ||
statement = dedent( | ||
f"""\ | ||
INSERT INTO {full_table_name} | ||
({", ".join(column_identifiers)}) | ||
VALUES ({", ".join([f":{name}" for name in property_names])}) | ||
""", | ||
conformed_schema = self.conform_schema(schema) | ||
property_names = list(conformed_schema["properties"]) | ||
|
||
_, schema_name, table_name = self.connector.parse_full_table_name( | ||
full_table_name | ||
) | ||
return statement.rstrip() | ||
|
||
table = sa.Table( | ||
table_name, | ||
sa.MetaData(), | ||
*[ | ||
sa.Column( | ||
name, sa.String | ||
) # Assuming all columns are of type String for simplicity # noqa: E501 | ||
Comment on lines
+296
to
+298
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (performance): Using String type for all columns could cause data type mismatches and performance issues. Consider mapping the schema types to appropriate SQLAlchemy types based on the conformed_schema['properties'] type definitions. |
||
for name in property_names | ||
], | ||
schema=schema_name, | ||
) | ||
|
||
return insert(table) | ||
|
||
def bulk_insert_records( | ||
self, | ||
|
@@ -321,7 +328,13 @@ def bulk_insert_records( | |
full_table_name, | ||
schema, | ||
) | ||
if isinstance(insert_sql, str): | ||
if isinstance(insert_sql, str): # pragma: no cover | ||
warnings.warn( | ||
"Generating a SQL insert statement as a string is deprecated. " | ||
"Please return an SQLAlchemy Executable object instead.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
insert_sql = sa.text(insert_sql) | ||
|
||
conformed_records = [self.conform_record(record) for record in records] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Need to handle case where schema_name is None.
The Table constructor will fail if schema_name is None. Consider adding a conditional to handle this case.