Skip to content

Instantly share code, notes, and snippets.

@StaverDmitry
Last active May 6, 2017 13:00
Show Gist options
  • Save StaverDmitry/656f3d556d29a578fdc37eaa3dd134eb to your computer and use it in GitHub Desktop.
Save StaverDmitry/656f3d556d29a578fdc37eaa3dd134eb to your computer and use it in GitHub Desktop.
Passenger.find_in_batches batch_size: 1000 do |passengers|
execute <<-SQL
UPDATE passengers AS p
SET cruise_index = crs.name,
person_index = person.name
FROM passengers, enquiries e, tours t, directions d, cruises crs,
(SELECT id, name FROM (
SELECT p.id, concat(coalesce(par.first_name, c.first_name), ' ',coalesce(par.last_name, c.last_name)) AS name
FROM passengers p
LEFT JOIN partners par
ON p.partner_id = par.id
LEFT JOIN customers c
ON c.id = p.customer_id) as t where name <> ' ') as person
WHERE p.enquiry_id = e.id
AND e.tour_id = t.id
AND t.direction_id = d.id
AND d.cruise_id = crs.id
AND p.id = person.id
AND passengers.id in (#{passengers.map(&:id).join ','})
SQL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment