-- After copying, delete the two columns as they are no longer required
ALTER TABLE `LoanAccountSchedule` DROP COLUMN `last_installment_amount` , DROP COLUMN `installment_amount` ;

-- Removing installment_amount from LoanScheduleAmountRef because it is not required for uniqueness (I think!)
ALTER TABLE `LoanScheduleAmountRef` DROP COLUMN `installment_amount` 
, DROP INDEX `loanscheduleamountref_proj_num_idx` 
, ADD UNIQUE INDEX `loanscheduleamountref_proj_num_idx` (`product_id` ASC, `interest_rate` ASC, `repay_freq_id` ASC, `loan_amount` ASC, `proj_num_installments` ASC, `first_repay_moratorium_days` ASC, `disbursement_date` ASC) 
, DROP INDEX `loanscheduleamountref_num_idx` 
, ADD UNIQUE INDEX `loanscheduleamountref_num_idx` (`product_id` ASC, `interest_rate` ASC, `repay_freq_id` ASC, `loan_amount` ASC, `num_installments` ASC, `first_repay_moratorium_days` ASC, `disbursement_date` ASC) ;

ALTER TABLE `LoanScheduleAmountRef` 
  ADD COLUMN `account_id` MEDIUMINT UNSIGNED DEFAULT NULL  AFTER `disbursement_date` , 
  ADD CONSTRAINT `LoanScheduleAmountRef_ibfk_3`
  FOREIGN KEY (`account_id` )
  REFERENCES `LoanAccount` (`id` )
  ON DELETE NO ACTION
  ON UPDATE CASCADE
, ADD INDEX `account_id` (`account_id` ASC) ;

ALTER TABLE `LoanScheduleAmountRef` 
DROP INDEX `loanscheduleamountref_proj_num_idx` 
, ADD UNIQUE INDEX `loanscheduleamountref_proj_num_idx` (`product_id` ASC, `interest_rate` ASC, `repay_freq_id` ASC, `loan_amount` ASC, `proj_num_installments` ASC, `first_repay_moratorium_days` ASC, `disbursement_date` ASC, `account_id` ASC) 
, DROP INDEX `loanscheduleamountref_num_idx` 
, ADD UNIQUE INDEX `loanscheduleamountref_num_idx` (`product_id` ASC, `interest_rate` ASC, `repay_freq_id` ASC, `loan_amount` ASC, `num_installments` ASC, `first_repay_moratorium_days` ASC, `disbursement_date` ASC, `account_id` ASC) ;

ALTER TABLE `LoanScheduleDateRef` DROP COLUMN `proj_num_installments` 
, DROP INDEX `loanscheduledateref_proj_num_idx` ;

