Skip to content

Commit

Permalink
Merge pull request #7761 from ckan/7748-datastore-records-row-errors
Browse files Browse the repository at this point in the history
datastore errors with records_row
  • Loading branch information
smotornyuk committed Sep 7, 2023
2 parents cff1cba + 1bb21a6 commit 169e9df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changes/7748.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
datastore_create, datastore_upsert now include a 'records_row' number when an error occurs
while inserting, upserting or updating records
17 changes: 11 additions & 6 deletions ckanext/datastore/backend/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ def upsert_data(context: Context, data_dict: dict[str, Any]):
records = data_dict['records']
sql_columns = ", ".join(
identifier(name) for name in field_names)
num = -1

if method == _INSERT:
rows = []
Expand Down Expand Up @@ -1151,8 +1152,10 @@ def upsert_data(context: Context, data_dict: dict[str, Any]):
try:
context['connection'].execute(sql_string, rows)
except (DatabaseError, DataError) as err:
raise ValidationError(
{u'records': [_programming_error_summary(err)]})
raise ValidationError({
'records': [_programming_error_summary(err)],
'records_row': num,
})

elif method in [_UPDATE, _UPSERT]:
unique_keys = _get_unique_key(context, data_dict)
Expand Down Expand Up @@ -1228,8 +1231,9 @@ def upsert_data(context: Context, data_dict: dict[str, Any]):
sql_string, used_values + unique_values)
except sqlalchemy.exc.DatabaseError as err:
raise ValidationError({
u'records': [_programming_error_summary(err)],
u'_records_row': num})
'records': [_programming_error_summary(err)],
'records_row': num,
})

# validate that exactly one row has been updated
if results.rowcount != 1:
Expand Down Expand Up @@ -1263,8 +1267,9 @@ def upsert_data(context: Context, data_dict: dict[str, Any]):
(used_values + unique_values) * 2)
except sqlalchemy.exc.DatabaseError as err:
raise ValidationError({
u'records': [_programming_error_summary(err)],
u'_records_row': num})
'records': [_programming_error_summary(err)],
'records_row': num,
})


def validate(context: Context, data_dict: dict[str, Any]):
Expand Down
8 changes: 6 additions & 2 deletions ckanext/datastore/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,10 @@ def test_create_trigger_exception(self, app):
records=[{u"spam": u"spam"}, {u"spam": u"EGGS"}],
triggers=[{u"function": u"spamexception_trigger"}],
)
assert error.value.error_dict == {u"records": [u'"EGGS"? Yeeeeccch!']}
assert error.value.error_dict == {
"records": ['"EGGS"? Yeeeeccch!'],
"records_row": 1,
}

@pytest.mark.ckan_config("ckan.plugins", "datastore")
@pytest.mark.usefixtures("clean_datastore", "with_plugins")
Expand Down Expand Up @@ -1402,5 +1405,6 @@ def test_upsert_trigger_exception(self, app):
records=[{u"spam": u"spam"}, {u"spam": u"BEANS"}],
)
assert error.value.error_dict == {
u"records": [u'"BEANS"? Yeeeeccch!']
"records": ['"BEANS"? Yeeeeccch!'],
"records_row": 1,
}

0 comments on commit 169e9df

Please sign in to comment.