
ALTER TABLE `vn`.`worker` MODIFY COLUMN `maritalStatus__` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;
UPDATE `vn`.`worker` `w`  
    SET `w`.`maritalStatus__` = NULL;

UPDATE `vn`.`worker` `w` 
    JOIN `vn`.`person` `p` ON `p`.`workerFk` = `w`.`id` 
    JOIN `postgresql`.`profile` `pr` ON `pr`.`person_id` = `p`.`id` 
    JOIN `vn2008`.`profile_labour_payroll` `pl` ON `pl`.`profile_id` = `pr`.`profile_id` 
    SET `w`.`maritalStatus__` = `pl`.`estadocivil`;

ALTER TABLE `vn`.`worker` ADD `originCountryFk` mediumint(8) unsigned NULL COMMENT 'País de origen';
ALTER TABLE `vn`.`worker` ADD `educationLevelFk` SMALLINT NULL;
ALTER TABLE `vn`.`worker` ADD `SSN` varchar(15) NULL;
ALTER TABLE `vn`.`worker` CHANGE `maritalStatus__` `maritalStatus` enum('S','M') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;
ALTER TABLE `vn`.`worker` MODIFY COLUMN `maritalStatus` enum('S','M') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;
ALTER TABLE `vn`.`worker` CHANGE `maritalStatus` maritalStatus enum('S','M') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL AFTER sectorFk;
ALTER TABLE `vn`.`worker` ADD CONSTRAINT `worker_FK_2` FOREIGN KEY (`educationLevelFk`) REFERENCES `vn`.`educationLevel`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `vn`.`worker` ADD CONSTRAINT `worker_FK_1` FOREIGN KEY (`originCountryFk`) REFERENCES `vn`.`country`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

INSERT INTO `vn`.`country` (`country`, `CEE`, `code`, `politicalCountryFk`, `isUeeMember`, `a3Code`)
    VALUES 
        ('Argentina',2,'AR',80,0,32),
        ('Cuba',2,'CU',81,0,192),
        ('Guinea Ecuatorial',2,'GQ',82,0,226),
        ('Guinea',2,'GN',83,0,324),
        ('Honduras',2,'HN',84,0,340),
        ('Mali',2,'ML',85,0,466),
        ('Nicaragua',2,'NI',86,0,558),
        ('Pakistán',2,'PK',87,0,586),
        ('Paraguay',2,'PY',88,0,600),
        ('Senegal',2,'SN',89,0,686),
        ('Uruguay',2,'UY',90,0,858),
        ('Venezuela',2,'VE',91,0,862),
        ('Bulgaria',2,'BG',92,1,100),
        ('Georgia',2,'GE',93,0,268);

UPDATE `vn`.`worker` `w` 
    JOIN `vn`.`person` `p` ON `p`.`workerFk` = `w`.`id` 
    JOIN `postgresql`.`profile` `pr` ON `pr`.`person_id` = `p`.`id` 
    JOIN `vn2008`.`profile_labour_payroll` `pl` ON `pl`.`profile_id` = `pr`.`profile_id` 
    JOIN `vn`.`country` `co` ON `co`.`a3Code` = `pl`.`codpais` 
    SET `w`.`originCountryFk` = `co`.`id`;

UPDATE `vn`.`worker` `w` 
    JOIN `vn`.`person` `p` ON `p`.`workerFk` = `w`.`id` 
    JOIN `postgresql`.`profile` `pr` ON `pr`.`person_id` = `p`.`id` 
    JOIN `vn2008`.`profile_labour_payroll` pl ON `pl`.`profile_id` = `pr`.`profile_id` 
    SET `w`.`SSN` = CONCAT(`pl`.`NSSProvincia`, `pl`.`NssNumero`, `pl`.`NssDC`);

RENAME TABLE `vn2008`.`profile_labour_payroll` TO `vn2008`.`profile_labour_payroll__`;
