ALTER TABLE AKUZ.T_EVENT_REHABILITATION_CARD ADD "rehabcourse" uuid NULL;

UPDATE AKUZ.T_EVENT_REHABILITATION_CARD EVENT_CARD
	SET RehabCourse = REHAB_COURSE.RehabCourseID
FROM AKUZ.T_REHAB_COURSE REHAB_COURSE 
WHERE EVENT_CARD.EventBase = REHAB_COURSE.EventBase;

ALTER TABLE AKUZ.T_MKF_DIAGNOSIS ALTER COLUMN "eventrehabilitationcard" SET NOT NULL;

ALTER TABLE AKUZ.T_EVENT_REHABILITATION_CARD ADD CONSTRAINT FK_T_REHAB_COURSE_T_EVENT_REHABILITATION_CARD_RehabCourse
FOREIGN KEY("rehabcourse") REFERENCES AKUZ.T_REHAB_COURSE (RehabCourseID);

ALTER TABLE AKUZ.T_REHAB_COURSE DROP CONSTRAINT FK_T_EVENT_BASE_T_REHAB_COURSE_EventBase;

ALTER TABLE AKUZ.T_REHAB_COURSE DROP COLUMN "eventbase";

do $$ begin
IF EXISTS(select * from pg_indexes where schemaname ilike 'AKUZ' and tablename ilike 'T_EVENT_REHABILITATION_CARD' and indexname ilike 'UQ_T_EVENT_REHABILITATION_CARD_RehabCourse')
THEN
drop index AKUZ.UQ_T_EVENT_REHABILITATION_CARD_RehabCourse;
END IF;
end; $$;
ALTER TABLE AKUZ.T_EVENT_REHABILITATION_CARD DROP CONSTRAINT if exists UQ_T_EVENT_REHABILITATION_CARD_RehabCourse;
ALTER TABLE AKUZ.T_EVENT_REHABILITATION_CARD DROP COLUMN if exists RehabCourse_UQ;

CREATE UNIQUE INDEX UQ_T_EVENT_REHABILITATION_CARD_RehabCourse ON AKUZ.T_EVENT_REHABILITATION_CARD(RehabCourse);



