USE vn;

ALTER TABLE vn.worker
    ADD COLUMN `fi` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
    ADD COLUMN `birth` date DEFAULT NULL,
    ADD COLUMN `isDisable` tinyint(1) NOT NULL DEFAULT '0',
    ADD COLUMN `isFreelance` tinyint(1) NOT NULL DEFAULT '0',
    ADD COLUMN `isSsDiscounted` tinyint(1) NOT NULL DEFAULT '0',
    ADD COLUMN `sex` enum('M','F') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'F' COMMENT 'M Masculino F Femenino';

UPDATE vn.worker w
        JOIN vn.person p ON  p.workerFk = w.id
    SET w.fi = p.fi,
        w.birth = p.birth,
        w.isDisable = p.isDisable,
        w.isFreelance = p.isFreelance, 
        w.isSsDiscounted = p.isSsDiscounted, 
        w.sex = p.sex;

ALTER TABLE `vn`.`person` 
    RENAME TO  `vn`.`person__` ;

ALTER TABLE `vn`.`worker` 
    DROP FOREIGN KEY `Clientes`;

ALTER TABLE `vn`.`worker` 
    DROP COLUMN `clientFk__`,
    DROP COLUMN `birthed__`,
    DROP COLUMN `address__`,
    DROP COLUMN `fi__`,
    DROP COLUMN `notes__`,
    DROP COLUMN `ended__`,
    DROP COLUMN `started__`,
    DROP COLUMN `laborCategory__`,
    DROP COLUMN `typeBussines__`,
    DROP COLUMN `user__`,
    DROP COLUMN `extension__`,
    DROP COLUMN `email__`,
    DROP COLUMN `password__`,
    DROP INDEX `Id_Cliente_Interno` ,
    DROP INDEX `user` ;


ALTER TABLE `postgresql`.`bank_account` 
    DROP FOREIGN KEY `bank_account_client_id`;

ALTER TABLE `postgresql`.`profile` 
    DROP FOREIGN KEY `profile_FK`;

ALTER TABLE `postgresql`.`bank_account` 
    CHANGE COLUMN `client_id` `client_id__` INT(11) NULL DEFAULT NULL ,
    DROP INDEX `fki_bank_profile`;
    
ALTER TABLE `postgresql`.`profile` 
    ADD COLUMN `workerFk` INT(10) UNSIGNED NULL DEFAULT NULL AFTER `profile_type_id`;

UPDATE postgresql.profile pr
        JOIN vn.person__ p ON p.id = pr.person_id
    SET pr.workerFk = p.workerFk;

ALTER TABLE `postgresql`.`profile` 
    ADD INDEX `profile_workerFk_idx` (`workerFk` ASC);

ALTER TABLE `postgresql`.`profile` 
    CHANGE COLUMN `person_id` `person_id__` INT(11) NULL DEFAULT NULL ;

ALTER TABLE `postgresql`.`profile` 
    ADD CONSTRAINT `profile_workerFk`
    FOREIGN KEY (`workerFk`)
    REFERENCES `vn`.`worker` (`id`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE;

-- Hay que revisar 2 contratos con provider null
ALTER TABLE `postgresql`.`business` 
	ADD COLUMN `companyCodeFk` CHAR(3) COLLATE utf8mb3_unicode_ci DEFAULT NULL AFTER `provider_id`;

UPDATE postgresql.business b
	SET b.companyCodeFk ='VNH'
    WHERE b.provider_id = 187;
    
UPDATE postgresql.business b
	SET b.companyCodeFk ='FTH'
    WHERE b.provider_id = 194;    
  
UPDATE postgresql.business b
	SET b.companyCodeFk ='EFL'
    WHERE b.provider_id = 201;      

UPDATE postgresql.business b
	SET b.companyCodeFk ='ORN'
    WHERE b.provider_id = 305;      

UPDATE postgresql.business b
	SET b.companyCodeFk ='ORN'
    WHERE b.provider_id = 305; 

ALTER TABLE `postgresql`.`business` 
	DROP FOREIGN KEY `bussiness_provider`;

ALTER TABLE `postgresql`.`business` 
	CHANGE COLUMN `provider_id` `provider_id__` INT(11) NULL DEFAULT NULL ,
	DROP INDEX `bussiness_provider` ;   


ALTER TABLE `vn2008`.`payroll_employee` 
    ADD COLUMN `workerFk` INT(11) UNSIGNED NULL AFTER `ContratoTemporal`;

ALTER TABLE `vn2008`.`payroll_employee` 
    ADD INDEX `payroll_employee_workerFk_idx` (`workerFk` ASC);

UPDATE vn2008.payroll_employee pe
        JOIN vn.person__ p ON p.id = pe.person_id
    SET pe.workerFk = p.workerFk;

ALTER TABLE `vn2008`.`payroll_employee` 
    ADD CONSTRAINT `payroll_employee_workerFk`
        FOREIGN KEY (`workerFk`)
        REFERENCES `vn`.`worker` (`id`)
        ON DELETE RESTRICT
        ON UPDATE CASCADE;

ALTER TABLE `vn2008`.`payroll_employee` 
    CHANGE COLUMN `Person_id` `Person_id__` INT(11) NOT NULL ;

ALTER TABLE `postgresql`.`income_employee` 
    ADD COLUMN `workerFk` INT(11) UNSIGNED NULL AFTER `odbc_date`,
    ADD INDEX `income_employee_workerFk_idx` (`workerFk` ASC) ;

UPDATE postgresql.income_employee ie
        JOIN vn.person__ p ON p.id = ie.person_id
    SET ie.workerFk = p.workerFk;

DELETE FROM postgresql.income_employee 
    WHERE workerFk IS NULL; -- 1 registro de 2005 a 0

ALTER TABLE `postgresql`.`income_employee` 
    CHANGE COLUMN `workerFk` `workerFk` INT(11) UNSIGNED NOT NULL ;

ALTER TABLE `postgresql`.`income_employee` 
    ADD CONSTRAINT `income_employee_workerFk`
    FOREIGN KEY (`workerFk`)
    REFERENCES `vn`.`worker` (`id`)
    ON UPDATE CASCADE;

ALTER TABLE `postgresql`.`income_employee` 
    DROP FOREIGN KEY `income_employee_FK`;

ALTER TABLE `postgresql`.`income_employee` 
    CHANGE COLUMN `person_id` `person_id__` INT(11) NULL DEFAULT NULL ,
    DROP INDEX `fperson_id` ;

ALTER TABLE `vn`.`company` 
	ADD UNIQUE INDEX `companyCode_UNIQUE` (`code` ASC);

ALTER TABLE `postgresql`.`business` 
    ADD CONSTRAINT `business_companyCodeFk`
    FOREIGN KEY (companyCodeFk)
    REFERENCES `vn`.`company` (`code`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE;