From d6e7444e99a4c15ab7008efe163caf44d32b98a4 Mon Sep 17 00:00:00 2001 From: Steven Presser Date: Wed, 31 May 2023 14:55:49 -0400 Subject: [PATCH] Add a check to guard against missing service_ids causing an import error. This occurs when a feed has a service_id which only appears in the calendar_exceptions table and is not present in the calendar table. This code adds a dummy entry to the calendar table. --- pygtfs/loader.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pygtfs/loader.py b/pygtfs/loader.py index 9905ff2..5c1e346 100644 --- a/pygtfs/loader.py +++ b/pygtfs/loader.py @@ -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('.')