Skip to content

feat: odpt integration - #1797

Open
ianktc wants to merge 5 commits into
mainfrom
feat/odpt-integration
Open

feat: odpt integration#1797
ianktc wants to merge 5 commits into
mainfrom
feat/odpt-integration

Conversation

@ianktc

@ianktc ianktc commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary:

odpt_script as provided directly from ODPT

This PR adds a new odpt_import task to the tasks_executor Cloud Function that imports Japanese transit feeds from the Open Data Platform for Transportation (ODPT). It queries ODPT's members-portal metadata API for openly-licensed (CC BY 4.0 and CC0) GTFS datasets, creates/updates the corresponding Gtfsfeed and Gtfsrealtimefeed (trip updates, vehicle positions, service alerts) records, tags each with an odpt external ID and a Japan-level Location, and links related GTFS-RT feeds back to their parent schedule feed. Closes MobilityData/mobility-database-catalogs#1391. A new monthly Cloud Scheduler job (odpt_import_schedule, 5th of the month) is added, alongside the existing JBDA/TDG import schedules.

Expected behavior:

  • Running the task with dry_run: true (default) fetches and diffs against the DB but performs no writes — all staged session changes are explicitly rolled back at the end.
  • Running with dry_run: false creates new GTFS/GTFS-RT feeds for previously-unseen ODPT org/dataset pairs, updates existing ones only when a tracked field (name, provider, producer URL, license URL, or an RT endpoint URL) actually changed, and leaves unchanged feeds untouched.
  • New/changed feeds trigger a dataset-download Pub/Sub message; changed feeds also enqueue a website cache-revalidation task.
  • If a schedule feed's producer URL changes in place, a feed.url_updated notification event is emitted.
  • Changes are committed in batches (COMMIT_BATCH_SIZE, default 5) rather than one giant transaction, with per-batch IntegrityError handling that rolls back just that batch.

Testing tips:

  • Run scripts/api-tests.sh --folder functions-python/tasks_executor — includes the new test_odpt_import.py covering: full create path (schedule + 3 RT feeds + location + external ID), the dry-run-must-not-write regression test, and graceful handling of an upstream HTTP failure.
  • To exercise locally end-to-end, invoke the tasks_executor function with {"task": "odpt_import", "payload": {"dry_run": true}} first and inspect the returned summary (created_gtfs, updated_gtfs, created_gtfs_rt, updated_gtfs_rt, linked_refs, total_processed_items) before flipping to dry_run: false against a local DB.

Please make sure these boxes are checked before submitting your pull request - thanks!

  • Run the unit tests with ./scripts/api-tests.sh to make sure you didn't break anything
  • Add or update any needed documentation to the repo
  • Format the title like "feat: [new feature short description]". Title must follow the Conventional Commit Specification(https://www.conventionalcommits.org/en/v1.0.0/).
  • Linked all relevant issues
  • Include screenshot(s) showing how this pull request works and fixes the issue(s)

@ianktc ianktc self-assigned this Aug 6, 2026
@ianktc

ianktc commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

I found the same issue with @with_db_session annotation committing on dry_run=True without an explicit rollback. This is addressed by adding an explicit rollback. if not dry_run then commit else rollback. I believe the same issue exists in tdg and jbda imports.

Explanation:

  1. _import_odpt(dry_run=dry_run) is called with no db_session kwarg (line 88 of import_odpt_handler).
  2. with_db_session's wrapper sees kwargs.get("db_session") is None, so it creates a Database instance and opens db.start_db_session() itself, injecting the new session into kwargs["db_session"].
  3. start_db_session (lines 242–250) is a plain try/except: yield session (runs _import_odpt's whole body) → session.commit() if nothing raised → session.rollback() only if an exception propagated out.
  4. _import_odpt returns a normal dict in the dry-run case (it never raises an exception) so branch is always "commit," never "rollback."

@ianktc
ianktc requested a review from cka-y August 10, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set up automatic import of ODPT Feeds

2 participants