Skip to content

Commit

Permalink
[#3428] catch errors on upsert + update as well
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Apr 18, 2017
1 parent 5e5f337 commit 6bb499f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions ckanext/datastore/db.py
Expand Up @@ -719,7 +719,7 @@ def upsert_data(context, data_dict):
toolkit._("The data was invalid (for example: a numeric value "
"is out of range or was inserted into a text field)."
))
except sqlalchemy.exc.InternalError as err:
except sqlalchemy.exc.DatabaseError as err:
raise ValidationError(
{u'records': [_programming_error_summary(err)]})

Expand Down Expand Up @@ -782,8 +782,13 @@ def upsert_data(context, data_dict):
[u'"{0}"'.format(part) for part in unique_keys]),
primary_value=u','.join(["%s"] * len(unique_keys))
)
results = context['connection'].execute(
sql_string, used_values + [full_text] + unique_values)
try:
results = context['connection'].execute(
sql_string, used_values + [full_text] + unique_values)
except sqlalchemy.exc.DatabaseError as err:
raise ValidationError({
u'records': [_programming_error_summary(err)],
u'_records_row': num})

# validate that exactly one row has been updated
if results.rowcount != 1:
Expand Down Expand Up @@ -811,9 +816,14 @@ def upsert_data(context, data_dict):
for part in unique_keys]),
primary_value=u','.join(["%s"] * len(unique_keys))
)
context['connection'].execute(
sql_string,
(used_values + [full_text] + unique_values) * 2)
try:
context['connection'].execute(
sql_string,
(used_values + [full_text] + unique_values) * 2)
except sqlalchemy.exc.DatabaseError as err:
raise ValidationError({
u'records': [_programming_error_summary(err)],
u'_records_row': num})


def _get_unique_key(context, data_dict):
Expand Down Expand Up @@ -1483,8 +1493,8 @@ def _write_engine_execute(sql):

def _programming_error_summary(pe):
u'''
return the text description of e sqlalchemy ProgrammingError or
InternalError without the actual SQL included, for raising as a
return the text description of a sqlalchemy DatabaseError
without the actual SQL included, for raising as a
ValidationError to send back to API users
'''
# first line only, after the '(ProgrammingError)' text
Expand Down

0 comments on commit 6bb499f

Please sign in to comment.