move SQL logic from pg functions to transit_trips.rb