-- Fixes two real bugs found in the performance/appraisal module:
--
-- 1. manager_review.php updates a `manager_reviewed_at` column that never
--    existed in the schema at all -- every manager review submission was
--    failing outright. Added to match the existing `hr_reviewed_at`
--    column's pattern.
--
-- 2. appraisal_scores had no way to prevent duplicate rows for the same
--    (appraisal_id, kpi_id) pair -- e.g. a double-submitted form, or a
--    browser back-button resubmission, would create two competing score
--    rows for the same KPI instead of cleanly updating the one that
--    already exists. The unique key below is what makes
--    "INSERT ... ON DUPLICATE KEY UPDATE" possible for these scores.
--
-- Safe to run more than once.

ALTER TABLE `employee_appraisals`
    ADD COLUMN IF NOT EXISTS `manager_reviewed_at` DATETIME DEFAULT NULL AFTER `manager_comment`;

ALTER TABLE `appraisal_scores`
    ADD UNIQUE KEY IF NOT EXISTS `uniq_appraisal_kpi` (`appraisal_id`, `kpi_id`);
