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

fix(targets): Quote column names in INSERT statement #2200

Merged
merged 5 commits into from
Jan 30, 2024
Merged
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
7 changes: 6 additions & 1 deletion singer_sdk/sinks/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import sqlalchemy as sa
from pendulum import now
from sqlalchemy.sql import quoted_name
from sqlalchemy.sql.expression import bindparam

from singer_sdk.connectors import SQLConnector
Expand Down Expand Up @@ -279,10 +280,14 @@ def generate_insert_statement(
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(property_names)})
({", ".join(column_identifiers)})
VALUES ({", ".join([f":{name}" for name in property_names])})
""", # noqa: S608
)
Expand Down
5 changes: 3 additions & 2 deletions tests/samples/test_target_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,15 @@ def test_record_with_missing_properties(
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"table": {"type": "string"},
},
},
[],
dedent(
"""\
INSERT INTO test_stream
(id, name)
VALUES (:id, :name)""",
(id, name, "table")
VALUES (:id, :name, :table)""",
),
),
],
Expand Down
Loading