Skip to content

Commit

Permalink
Added UNIQUE constraint protection
Browse files Browse the repository at this point in the history
Added try/catch against UNIQUE Constraint/Integrity error, it’ll still raise exception if it breaks again. Also fixed #15
  • Loading branch information
lkellar committed Jan 23, 2020
1 parent 66c86ce commit 5b30b17
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,13 @@ ignore-on-opaque-inference=yes
# List of class names for which member attributes should not be checked (useful
# for classes with dynamically set attributes). This supports the use of
# qualified names.
ignored-classes=optparse.Values,thread._local,_thread._local
ignored-classes=optparse.Values,thread._local,_thread._local,scoped_session

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=flask_sqlalchemy
ignored-modules=

# Show a hint with possible names when a member name was not found. The aspect
# of finding the hint is based on edit distance.
Expand Down
2 changes: 1 addition & 1 deletion maps/scraper/geocoder/jobmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def create(self, addresses):
data=input_data)
response_json = res.json()

if response_json['statusCode'] == '400':
if int(response_json['statusCode']) >= 400:
# if there's an error creating the job, raise an error! Yay
raise BingAPIError(response_json['errorDetails'])

Expand Down
11 changes: 10 additions & 1 deletion maps/scraper/springdale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import requests
import pytz

from sqlalchemy.exc import IntegrityError

from maps import db
from maps.models import Call, CallQuery
from maps.scraper.geocoder import geocode_lookup
Expand Down Expand Up @@ -69,7 +71,14 @@ def scrape_to_db():
new_calls = geocode_calls(new_calls)

for call in new_calls:
db.session.merge(call)
try:
db.session.merge(call)
except IntegrityError:
existing_call = CallQuery.get_existing_springdale(call)
if existing_call:
existing_call.notes = disposition
else:
raise Exception(f'Call {call} was given unique constraint error, but it can\'t be found in the db')

# Commit new calls
db.session.commit()
Expand Down

0 comments on commit 5b30b17

Please sign in to comment.