Skip to content

Commit

Permalink
Merge pull request #142 from BritishGeologicalSurvey/validate-copy-ta…
Browse files Browse the repository at this point in the history
…ble-rows

Validate table name in copy_table_rows
  • Loading branch information
volcan01010 authored Nov 4, 2022
2 parents bc8b6df + e678402 commit 887f084
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions etlhelper/etl.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ def copy_table_rows(table, source_conn, dest_conn, target=None,
e.g. WHERE, ORDER BY, LIMIT
:return processed, failed: (int, int) number of rows processed, failed
"""
validate_identifier(table)

select_query = f"SELECT * FROM {table}"
if not target:
target = table
Expand Down
8 changes: 8 additions & 0 deletions test/integration/etl/test_etl_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
iter_rows,
)
from etlhelper.row_factories import dict_row_factory
from etlhelper.exceptions import ETLHelperBadIdentifierError


def test_copy_rows_happy_path(pgtestdb_conn, pgtestdb_test_tables,
Expand Down Expand Up @@ -74,6 +75,13 @@ def test_copy_table_rows_on_error(pgtestdb_test_tables, pgtestdb_conn,
assert result[1:] == test_table_data[1:]


def test_copy_table_rows_bad_table(pgtestdb_conn, pgtestdb_test_tables,
pgtestdb_insert_sql, test_table_data):
# Arrange and act
with pytest.raises(ETLHelperBadIdentifierError):
copy_table_rows('bad; sql', pgtestdb_conn, pgtestdb_conn, target='dest')


def transform_return_list(rows):
# Simple transform function that changes size and number of rows
return [(row.id, row.value) for row in rows if row.id > 1]
Expand Down

0 comments on commit 887f084

Please sign in to comment.