Beginning work on the two remaining custom field groups for conversion
Posted by mholmes on 25 Aug 2010 in Activity log
These are the remaining items to convert:
Some groups of custom fields can be converted into a one-to-many + text field * Topic of dissent: individual fields can be the items in the one-to-many table; then there needs to be a text field in which the dissent is described (can we call the fields, eg., "topic of dissent" and "topic of dissent: description")? * New group composed of remaining fields in 'Attitude towards peasants and politics' (2-10) and 'Perceived intention of dissenting words' (1-6), call this new one-to-many field "Concerns of authorities'. Can we simply paste any of the detailed info contained in any of these fields (ie., not true-false indications), as well as any contents of the 'Comments' field in 'Perceived intention...', into the accompanying text field?
These are the Topics of Dissent fields (all shortstring):
- 62 1 Against the king (AR)
- 49 2 Assignats, maximum, requisitions, taxes, biens nat...
- 89 3 Ideas on politics
- 53 4 Inflammatory statements (vive Louis XVII; ça n'ira...
- 52 5 Local politics
- 54 6 None - just attroupement, incomplete, etc.
- 63 7 Other
- 47 8 Politics (royalism, counter-revolution, etc.)
- 48 9 Prices, subsistences, disette (problems)
- 50 10 Recruitment, war effort incl. news of successes/se...
- 51 11 Religion, incl. Catholics/Protestants
This is the script:
/* First we need to add a new column for the string data. We specify NOT NULL so that we can later append data. If there is NULL in the field, it proves impossible to append, for some reason. */ ALTER TABLE `documents` ADD `doc_disTopicDesc` VARCHAR(4096) NOT NULL; /* Now we create the tables for dissent topics. I'm using the dc_ prefix for this table, as dt_ is already taken by dissentTypes. */ DROP TABLE IF EXISTS `docs_to_disTopics`; DROP TABLE IF EXISTS `disTopics`; CREATE TABLE IF NOT EXISTS `disTopics` ( `dc_id` int(11) NOT NULL auto_increment, `dc_name` varchar(128) collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`dc_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `docs_to_disTopics` ( `dtc_dtc_id` int(11) NOT NULL auto_increment, `dtc_doc_id_fk` int(11) NOT NULL, `dtc_disTopic_id_fk` int(11) NOT NULL, PRIMARY KEY (`dtc_dtc_id`), KEY `dtc_disTopic_id_fk_idx` (`dtc_disTopic_id_fk`), KEY `dtc_doc_id_fk_idx` (`dtc_doc_id_fk`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; /* Now we create the foreign key constraints for the second table. */ ALTER TABLE `docs_to_disTopics` ADD CONSTRAINT `docs_to_disTopics_ibfk_1` FOREIGN KEY (`dtc_doc_id_fk`) REFERENCES `documents` (`doc_id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `docs_to_disTopics_ibfk_2` FOREIGN KEY (`dtc_disTopic_id_fk`) REFERENCES `disTopics` (`dc_id`) ON DELETE CASCADE ON UPDATE CASCADE; /* Now we create the new fields from the old. We use all field types in this case. */ INSERT IGNORE INTO `disTopics` (`dc_name`) ( SELECT `cf_name` FROM `customFields` WHERE (`cf_group_1` LIKE "%Topic of dissent%") ORDER BY `cf_name`); /* Now we go through copying the data over from the old custom fields. */ /* This is the mapping data: 62 1 Against the king (AR) 49 2 Assignats, maximum, requisitions, taxes, biens nat... 89 3 Ideas on politics 53 4 Inflammatory statements (vive Louis XVII; ça n'ira... 52 5 Local politics 54 6 None - just attroupement, incomplete, etc. 63 7 Other 47 8 Politics (royalism, counter-revolution, etc.) 48 9 Prices, subsistences, disette (problems) 50 10 Recruitment, war effort incl. news of successes/se... 51 11 Religion, incl. Catholics/Protestants */ /* (62, 49, 89, 53, 52, 54, 63, 47, 48, 50, 51) */ /* Converting these fields is slightly more complex. Some contain variations on "True" and "Yes", while others have string data. Where there is any data, the field is deemed "true"; where the data is not just "True" or "Yes", we need to append the string to the doc_disTopicDesc field in the documents table. */ /* First, create the booleans for the one-to-many relationship. */ INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "1", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "62"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "2", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "49"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "3", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "89"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "4", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "53"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "5", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "52"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "6", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "54"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "7", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "63"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "8", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "47"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "9", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "48"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "10", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "50"; INSERT INTO `docs_to_disTopics` (`dtc_disTopic_id_fk`, `dtc_doc_id_fk`) SELECT "11", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "51"; /* Now copy over the string data. */ /* 1 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "62" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "62") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 2 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "49" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "49") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 3 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "89" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "89") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 4 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "53" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "53") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 5 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "52" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "52") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 6 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "54" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "54") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 7 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "63" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "63") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 8 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "47" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "47") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 9 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "48" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "48") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 10 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "50" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "50") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* 11 */ UPDATE `documents` SET `doc_disTopicDesc` = concat(`doc_disTopicDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "51" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "51") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` LIKE "True%") AND NOT (`customFieldData`.`cfd_str` LIKE "Yes%") ); /* (62, 49, 89, 53, 52, 54, 63, 47, 48, 50, 51) */ /* Then, we delete the original custom field data. WARNING: IRREVOCABLE! */ DELETE FROM `customFieldData` WHERE `cfd_to_cf_id` IN (62, 49, 89, 53, 52, 54, 63, 47, 48, 50, 51); /* Finally, we delete the original custom field definitions. WARNING: IRREVOCABLE! */ DELETE FROM `customFields` WHERE `cf_id` IN (62, 49, 89, 53, 52, 54, 63, 47, 48, 50, 51);
This has now been executed on both dev and live.