-
-
Save khalsah/967217 to your computer and use it in GitHub Desktop.
VHGDB - Migrating statuses to actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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