DO
$$
BEGIN	
	IF EXISTS (SELECT column_name 
				FROM information_schema.columns 
				WHERE table_schema = 'vclib' and table_name='t_email_message' and column_name='ispack')
	THEN
       execute 'insert into vclib.t_scheduled_report(operationid, ispack, password)
              select t1.operationid, t1.ispack, t1.password
              from vclib.t_email_message t1';
	END IF;
END
$$
LANGUAGE plpgsql;

alter table vclib.T_EMAIL_MESSAGE
drop column if exists "ispack",
drop column if exists "password";

ALTER TABLE vclib.t_email_attachment DROP CONSTRAINT IF EXISTS fk_t_email_message_t_email_attachment_emailmessage;
ALTER TABLE vclib.t_email_attachment DROP CONSTRAINT IF EXISTS fk_t_scheduled_report_t_email_attachment_emailmessage;
ALTER TABLE vclib.t_email_attachment ADD CONSTRAINT fk_t_scheduled_report_t_email_attachment_emailmessage FOREIGN KEY(emailmessage)
REFERENCES vclib.t_scheduled_report (operationid);

