"Concerns of authorities" script ready
Posted by mholmes on 26 Aug 2010 in Activity log
Script is ready and tested on dev -- I'll run it on live after backing up this afternoon:
/* 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_authConcernDesc` VARCHAR(4096) NOT NULL; /* Now we create the tables for dissent topics. I'm using the ac_ prefix for this table. */ DROP TABLE IF EXISTS `docs_to_authConcerns`; DROP TABLE IF EXISTS `authConcerns`; CREATE TABLE IF NOT EXISTS `authConcerns` ( `ac_id` int(11) NOT NULL auto_increment, `ac_name` varchar(128) collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`ac_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `docs_to_authConcerns` ( `dta_dta_id` int(11) NOT NULL auto_increment, `dta_doc_id_fk` int(11) NOT NULL, `dta_authConcern_id_fk` int(11) NOT NULL, PRIMARY KEY (`dta_dta_id`), KEY `dta_authConcern_id_fk_idx` (`dta_authConcern_id_fk`), KEY `dta_doc_id_fk_idx` (`dta_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_authConcerns` ADD CONSTRAINT `docs_to_authConcerns_ibfk_1` FOREIGN KEY (`dta_doc_id_fk`) REFERENCES `documents` (`doc_id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `docs_to_authConcerns_ibfk_2` FOREIGN KEY (`dta_authConcern_id_fk`) REFERENCES `authConcerns` (`ac_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 `authConcerns` (`ac_name`) ( SELECT `cf_name` FROM `customFields` WHERE ((`cf_group_1` LIKE "Attitude towards%") OR (`cf_group_1` LIKE "Perceived intention%")) AND (`cf_id` != "25") ORDER BY `cf_name`); /* 15 1 Attempt to dissuade/convince 43 2 Bucolic innocence, "it's not in them", etc. 87 3 Easily led astray, focus on speakers instead 18 4 Expressing opinion, just complaining 17 5 Expression of regret, eliciting agreement 66 6 Importance of words vs. actions 60 7 People regarded with contempt, dismissed 16 8 Provocation, moteur in attroupement 111 9 Sedition: Assumption that peasant is not the guilt... 108 10 Sedition: Concern that public opinion might be aff... 107 11 Sedition: Concern with consequences, publicity 106 12 Sedition: Concern with intention, premeditation 105 13 Sedition: Words vs. actions; violence taken more s... 12 14 Spreading of news/rumour 80 15 Terms for instigators 25 is a comments field; data should just be carried over from that. */ /* 15, 43, 87, 18, 17, 66, 60, 16, 111, 108, 107, 106, 105, 12, 80, 25 */ /* Booleans: 12, 15, 16, 17, 18, 66, 87, 105, 108 */ /* text: 25, 60 */ /* strings: 43, 80, 106, 107, 111 */ /* Booleans first. 12, 15, 16, 17, 18, 66, 87, 105, 108 */ INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "14", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "12" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "1", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "15" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "8", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "16" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "5", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "17" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "4", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "18" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "6", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "66" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "3", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "87" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "13", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "105" AND `cfd_bool` = TRUE; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "10", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "108" AND `cfd_bool` = TRUE; /* Now the others. Existence of the record means we should create a boolean. There are no empty records. */ /* Now we create booleans from the other fields where there is any content. */ /* text: 60 */ INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "7", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "60"; /* strings: 43, 80, 106, 107, 111 */ INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "2", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "43"; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "15", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "80"; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "12", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "106"; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "11", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "107"; INSERT INTO `docs_to_authConcerns` (`dta_authConcern_id_fk`, `dta_doc_id_fk`) SELECT "9", `cfd_to_doc_id` FROM `customFieldData` WHERE `cfd_to_cf_id` = "111"; /* Now we carry over the old values from string fields. */ /* First the longer text. 25, 60 */ UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_text` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "25" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "25") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_text`) > 0) AND NOT (`customFieldData`.`cfd_text` = "Yes.") ); UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_text` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "60" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "60") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_text`) > 0) AND NOT (`customFieldData`.`cfd_text` = "Yes.") ); /* Now the strings: 43, 80, 106, 107, 111 */ UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "43" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "43") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` = "Yes.") ); UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "80" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "80") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` = "Yes.") ); UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "106" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "106") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` = "Yes.") ); UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "107" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "107") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` = "Yes.") ); UPDATE `documents` SET `doc_authConcernDesc` = concat(`doc_authConcernDesc`, (SELECT `cfd_str` FROM `customFieldData` WHERE `customFieldData`.`cfd_to_cf_id` = "111" AND `customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`), '\n' ) WHERE EXISTS ( SELECT * FROM `customFieldData` WHERE (`customFieldData`.`cfd_to_cf_id` = "111") AND (`customFieldData`.`cfd_to_doc_id` = `documents`.`doc_id`) AND (LENGTH(`customFieldData`.`cfd_str`) > 0) AND NOT (`customFieldData`.`cfd_str` = "Yes.") ); /* Then, we delete the original custom field data. WARNING: IRREVOCABLE! */ DELETE FROM `customFieldData` WHERE `cfd_to_cf_id` IN (15, 43, 87, 18, 17, 66, 60, 16, 111, 108, 107, 106, 105, 12, 80, 25); /* Finally, we delete the original custom field definitions. WARNING: IRREVOCABLE! */ DELETE FROM `customFields` WHERE `cf_id` IN (15, 43, 87, 18, 17, 66, 60, 16, 111, 108, 107, 106, 105, 12, 80, 25);