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] convert postgres delete implementation to parameterized query #434

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 19 additions & 15 deletions datastore/providers/postgres_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,25 @@ async def delete_by_filters(self, table: str, filter: DocumentMetadataFilter):
Deletes rows in the table that match the filter.
"""

filters = "WHERE"
if filter.document_id:
filters += f" document_id = '{filter.document_id}' AND"
if filter.source:
filters += f" source = '{filter.source}' AND"
if filter.source_id:
filters += f" source_id = '{filter.source_id}' AND"
if filter.author:
filters += f" author = '{filter.author}' AND"
if filter.start_date:
filters += f" created_at >= '{filter.start_date}' AND"
if filter.end_date:
filters += f" created_at <= '{filter.end_date}' AND"
filters = filters[:-4]
query = f"""
DELETE FROM {table} WHERE (%s is null or document_id = '%s' ) AND
(%s is NULL or source = %s) AND
(%s is NULL or source_id = %s) AND
(%s is NULL or author = %s) AND
(%s is NULL or created_at >= %s) AND
(%s is NULL or created_at <= %s)
"""

with self.client.cursor() as cur:
cur.execute(f"DELETE FROM {table} {filters}")
cur.execute(
query,
(
filter.document_id,
filter.source,
filter.source_id,
filter.author,
filter.start_date,
filter.end_date
)
)
self.client.commit()