-- ============================================================
-- P1.10: selection_result 选育结果 —— 晋级溯源字段（规格 §4 line 565）
-- ------------------------------------------------------------
-- 幂等。bre_selection_result 表已由 bre_tables.sql 建好。
-- ============================================================

ALTER TABLE bre_selection_result ADD COLUMN IF NOT EXISTS clone_id integer REFERENCES bre_clone(id);
CREATE INDEX IF NOT EXISTS ix_bre_selection_result_clone_id ON bre_selection_result(clone_id);

ALTER TABLE bre_selection_result ADD COLUMN IF NOT EXISTS rule_id integer REFERENCES bre_selection_rule(id);
CREATE INDEX IF NOT EXISTS ix_bre_selection_result_rule_id ON bre_selection_result(rule_id);

ALTER TABLE bre_selection_result ADD COLUMN IF NOT EXISTS from_stage varchar(32);
ALTER TABLE bre_selection_result ADD COLUMN IF NOT EXISTS to_stage varchar(32);

ALTER TABLE bre_selection_result ADD COLUMN IF NOT EXISTS approved_by integer REFERENCES bre_personnel(id);
CREATE INDEX IF NOT EXISTS ix_bre_selection_result_approved_by ON bre_selection_result(approved_by);

ALTER TABLE bre_selection_result ADD COLUMN IF NOT EXISTS trial_study_id integer REFERENCES bre_trial_study(id);
CREATE INDEX IF NOT EXISTS ix_bre_selection_result_trial_study_id ON bre_selection_result(trial_study_id);
