Skip to content

Commit

Permalink
Merge pull request #74 from spresse1/guard-missing-service_id
Browse files Browse the repository at this point in the history
Add a check to guard against missing service_ids causing an import error
  • Loading branch information
jarondl committed Jul 12, 2023
2 parents 5ba9313 + d6e7444 commit 5301eca
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pygtfs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ def append_feed(schedule, feed_filename, strip_fields=True,
print("Failure while writing {0}".format(record))
raise
schedule.session.add(instance)

if isinstance(instance, ServiceException):
service = schedule.session.execute(
select(Service).where(Service.service_id == instance.service_id).where(Service.feed_id == feed_id)
).one_or_none()
if service is None:
# ServiceException was added for a day that has no associated regular service
# Create a dummy service object for this service exception
dummy = Service(
feed_id=feed_id,
service_id=instance.service_id,
monday='0',
tuesday='0',
wednesday='0',
thursday='0',
friday='0',
saturday='0',
sunday='0',
start_date=instance.date.strftime('%Y%m%d'),
end_date=instance.date.strftime('%Y%m%d'))
schedule.session.add(dummy)


if i % chunk_size == 0 and i > 0:
schedule.session.flush()
sys.stdout.write('.')
Expand Down

0 comments on commit 5301eca

Please sign in to comment.