-- First drop the existing constraints.
-- Note: due to errors in preceding upgrade scripts, some constraints may not be present.
ALTER TABLE phoneline_forwarding DROP CONSTRAINT phoneline_forwarding_when_busy_check;
ALTER TABLE phoneline_forwarding DROP CONSTRAINT phoneline_forwarding_when_dnd_check;
ALTER TABLE phoneline_forwarding DROP CONSTRAINT phoneline_forwarding_when_loggedoff_check;
ALTER TABLE phoneline_forwarding DROP CONSTRAINT phoneline_forwarding_when_noanswer_check;
ALTER TABLE phoneline_forwarding DROP CONSTRAINT phoneline_forwarding_when_unconditional_check;
ALTER TABLE phoneline_forwarding DROP CONSTRAINT phoneline_forwarding_when_unreachable_check;

BEGIN;

-- Then change existing table entries.
UPDATE phoneline_forwarding SET when_busy = 'busy' WHERE when_busy IN ('busytone', 'busysignal');
UPDATE phoneline_forwarding SET when_dnd = 'busy' WHERE when_dnd IN ('busytone', 'busysignal');
UPDATE phoneline_forwarding SET when_loggedoff = 'busy' WHERE when_loggedoff IN ('busytone', 'busysignal');
UPDATE phoneline_forwarding SET when_noanswer = 'busy' WHERE when_noanswer IN ('busytone', 'busysignal');
UPDATE phoneline_forwarding SET when_unconditional = 'busy' WHERE when_unconditional IN ('busytone', 'busysignal');
UPDATE phoneline_forwarding SET when_unreachable = 'busy' WHERE when_unreachable IN ('busytone', 'busysignal');

-- Finally add new constraints.
ALTER TABLE phoneline_forwarding ADD CONSTRAINT phoneline_forwarding_when_busy_check
  CHECK (when_busy IN ('voicemail','operator','forward','busy','refuse','mobile','ask','hunt'));
ALTER TABLE phoneline_forwarding ADD CONSTRAINT phoneline_forwarding_when_dnd_check
  CHECK (when_dnd IN ('voicemail','operator','forward','busy','refuse','mobile','ask','hunt'));
ALTER TABLE phoneline_forwarding ADD CONSTRAINT phoneline_forwarding_when_loggedoff_check
  CHECK (when_loggedoff IN ('voicemail','operator','forward','busy','refuse','mobile','ask','hunt'));
ALTER TABLE phoneline_forwarding ADD CONSTRAINT phoneline_forwarding_when_noanswer_check
  CHECK (when_noanswer IN ('voicemail','operator','forward','busy','refuse','mobile','ask','hunt'));
ALTER TABLE phoneline_forwarding ADD CONSTRAINT phoneline_forwarding_when_unconditional_check
  CHECK (when_unconditional IN ('ring','voicemail','operator','forward','busy','refuse','mobile','ask','hunt'));
ALTER TABLE phoneline_forwarding ADD CONSTRAINT phoneline_forwarding_when_unreachable_check
  CHECK (when_unreachable IN ('voicemail','operator','forward','busy','refuse','mobile','ask','hunt'));

COMMIT;
