Skip to content

Instantly share code, notes, and snippets.

@khalsah
Forked from zacclark/migrate.rb
Created May 11, 2011 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khalsah/967217 to your computer and use it in GitHub Desktop.
Save khalsah/967217 to your computer and use it in GitHub Desktop.
VHGDB - Migrating statuses to actions
class MoveStatusDataToStatusChangeActions < ActiveRecord::Migration
def self.up
execute <<-EOQ
INSERT INTO actions(status_code, attendance_id, type, occurred_at, created_at, updated_at, archived_title, archived_company_id)
SELECT
statuses.status, statuses.attendance_id, 'StatusChangeAction', statuses.created_at, statuses.created_at, statuses.updated_at, titles.title, companies.id
FROM statuses
JOIN attendances ON attendances.id = statuses.attendance_id
JOIN people ON people.id = attendances.person_id
JOIN employments ON employments.id = people.current_employment_id
JOIN titles ON titles.id = employments.current_title_id
JOIN companies ON companies.id = employments.company_id
EOQ
execute <<-EOQ
UPDATE attendances
SET
latest_status_change_action_id =
(
SELECT actions.id
FROM actions
WHERE actions.attendance_id = attendances.id
AND actions.type = 'StatusChangeAction'
ORDER BY actions.occurred_at DESC, actions.updated_at DESC
LIMIT 1
)
EOQ
end
def self.down
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment