Commit 8824f7a27541b2ac899ce381f3ec6a5bfb9f87a3

Authored by kevin_fourie
1 parent 64f498bd

Merged in from STABLE trunk...

KTS-3604
"Upgrade failed from 3.5.2c to 3.5.3."
Fixed. Auto increment function now ensures there is no 0 id.

Committed by: Megan Watson
Reviewed by: Conrad Vermuelen


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/STABLE/branches/3.5.3-Release-Branch@9137 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/upgrades/UpgradeFunctions.inc.php
@@ -61,7 +61,7 @@ class UpgradeFunctions { @@ -61,7 +61,7 @@ class UpgradeFunctions {
61 '3.1.6.3' => array('cleanupGroupMembership'), 61 '3.1.6.3' => array('cleanupGroupMembership'),
62 '3.5.0' => array('cleanupOldKTAdminVersionNotifier', 'updateConfigFile35', 'registerIndexingTasks'), 62 '3.5.0' => array('cleanupOldKTAdminVersionNotifier', 'updateConfigFile35', 'registerIndexingTasks'),
63 '3.5.2' => array('setStorageEngine','dropForeignKeys','dropPrimaryKeys','dropIndexes','createPrimaryKeys','createForeignKeys','createIndexes', 'removeSlashesFromObjects'), 63 '3.5.2' => array('setStorageEngine','dropForeignKeys','dropPrimaryKeys','dropIndexes','createPrimaryKeys','createForeignKeys','createIndexes', 'removeSlashesFromObjects'),
64 - '3.5.3' => array('moveConfigSettingsToDB','removeAdminVersionNotifier','removeOldSearchPlugins','addAutoIncrementToTables') 64 + '3.5.3' => array('moveConfigSettingsToDB','removeAdminVersionNotifier','removeOldSearchPlugins','addAutoIncrementToTables', 'addAutoIncrementToTables2')
65 ); 65 );
66 66
67 var $descriptions = array( 67 var $descriptions = array(
@@ -95,7 +95,8 @@ class UpgradeFunctions { @@ -95,7 +95,8 @@ class UpgradeFunctions {
95 'moveConfigSettingsToDB' => 'Move the configuration settings from the config.ini file into the new database table.', 95 'moveConfigSettingsToDB' => 'Move the configuration settings from the config.ini file into the new database table.',
96 'removeAdminVersionNotifier' => 'Remove the old Admin Version Notifier Plugin.', 96 'removeAdminVersionNotifier' => 'Remove the old Admin Version Notifier Plugin.',
97 'removeOldSearchPlugins' => 'Remove the old Search Plugins.', 97 'removeOldSearchPlugins' => 'Remove the old Search Plugins.',
98 - 'addAutoIncrementToTables' => 'Update all db tables to use auto_increment.' 98 + 'addAutoIncrementToTables' => 'Update all current db tables to use auto_increment.',
  99 + 'addAutoIncrementToTables2' => 'Update all new db tables to use auto_increment.'
99 ); 100 );
100 var $phases = array( 101 var $phases = array(
101 "setPermissionFolder" => 1, 102 "setPermissionFolder" => 1,
@@ -115,37 +116,67 @@ class UpgradeFunctions { @@ -115,37 +116,67 @@ class UpgradeFunctions {
115 ); 116 );
116 117
117 var $priority = array( 118 var $priority = array(
118 - 'addAutoIncrementToTables'=>1 119 + 'addAutoIncrementToTables'=>1,
  120 + 'addAutoIncrementToTables2'=>-1
119 ); 121 );
120 122
  123 + function addAutoIncrementToTables2()
  124 + {
  125 + return self::addAutoIncrementToTables();
  126 + }
  127 +
121 /** 128 /**
122 * Set all tables in the DB to auto increment, thereby removing the use of the zseq tables 129 * Set all tables in the DB to auto increment, thereby removing the use of the zseq tables
123 */ 130 */
124 function addAutoIncrementToTables() 131 function addAutoIncrementToTables()
125 { 132 {
  133 + static $doneTables = array();
  134 +
126 global $default; 135 global $default;
127 DBUtil::setupAdminDatabase(); 136 DBUtil::setupAdminDatabase();
128 $db = $default->_admindb; 137 $db = $default->_admindb;
129 138
130 // Get all tables in the database 139 // Get all tables in the database
131 - $query = 'SHOW TABLES'; 140 + $query = "SHOW TABLES";
132 $tableList = DBUtil::getResultArray($query, $db); 141 $tableList = DBUtil::getResultArray($query, $db);
133 142
134 - // Ensure that if there is a zero id on plugins and upgrades it is updated  
135 - $query = "UPDATE plugins SET id = (SELECT max(id)+1 FROM plugins) WHERE id = 0";  
136 - DBUtil::runQuery($query, $db);  
137 -  
138 - $query = "UPDATE upgrades SET id = (SELECT max(id)+1 FROM plugins) WHERE id = 0";  
139 - DBUtil::runQuery($query, $db);  
140 -  
141 // Loop through tables and add auto increment 143 // Loop through tables and add auto increment
142 foreach ($tableList as $tableArr){ 144 foreach ($tableList as $tableArr){
143 $key = key($tableArr); 145 $key = key($tableArr);
144 $tableName = $tableArr[$key]; 146 $tableName = $tableArr[$key];
145 147
146 - // Some tables don't have an id column, we ignore those errors and continue 148 + if(in_array($tableName, $doneTables)){
  149 + // already been set - skip
  150 + continue;
  151 + }
  152 +
  153 +
  154 + $doneTables[] = $tableName;
  155 +
  156 + if(strpos($tableName, 'zseq_', 0) !== false){
  157 + // ignore zseq tables
  158 + continue;
  159 + }
  160 +
  161 + $query = "SELECT max(id) FROM {$tableName}";
  162 + $aId = DBUtil::getOneResult($query);
  163 +
  164 + if(PEAR::isError($aId)){
  165 + // Means that the table doesn't have an id column
  166 + continue;
  167 + }
  168 +
  169 + // If there's no result, then the table may be empty
  170 + if(!empty($aId)){
  171 + $id = (int)$aId['max(id)'] + 1;
  172 +
  173 + $query = "UPDATE {$tableName} SET id = {$id} WHERE id = 0";
  174 + $res = DBUtil::runQuery($query, $db);
  175 + }
  176 +
  177 + // Update the table, set id to auto_increment
147 $query = "ALTER TABLE {$tableName} CHANGE `id` `id` int (11) NOT NULL AUTO_INCREMENT"; 178 $query = "ALTER TABLE {$tableName} CHANGE `id` `id` int (11) NOT NULL AUTO_INCREMENT";
148 - DBUtil::runQuery($query, $db); 179 + $res = DBUtil::runQuery($query, $db);
149 } 180 }
150 } 181 }
151 182
@@ -1308,7 +1339,7 @@ class UpgradeFunctions { @@ -1308,7 +1339,7 @@ class UpgradeFunctions {
1308 if(file_exists($oldPath)) return rmdir($oldPath); 1339 if(file_exists($oldPath)) return rmdir($oldPath);
1309 } 1340 }
1310 // }}} 1341 // }}}
1311 - 1342 +
1312 // {{{ removeOldSearchPlugins 1343 // {{{ removeOldSearchPlugins
1313 function removeOldSearchPlugins() { 1344 function removeOldSearchPlugins() {
1314 global $default; 1345 global $default;
@@ -1322,7 +1353,7 @@ class UpgradeFunctions { @@ -1322,7 +1353,7 @@ class UpgradeFunctions {
1322 UpgradeFunctions::rm_recursive($oldPath1); 1353 UpgradeFunctions::rm_recursive($oldPath1);
1323 $oldPath2 = KT_DIR . "/plugins/generalmetadata/"; 1354 $oldPath2 = KT_DIR . "/plugins/generalmetadata/";
1324 UpgradeFunctions::rm_recursive($oldPath2); 1355 UpgradeFunctions::rm_recursive($oldPath2);
1325 - 1356 +
1326 // FIXME: We should check that they all worked 1357 // FIXME: We should check that they all worked
1327 return true; 1358 return true;
1328 } 1359 }
sql/mysql/upgrade/3.5.3/add_autoinc.sql deleted
1 -select @id :=max(id)+1 from plugins;  
2 -update plugins set id = @id where id = 0;  
3 -select @id :=max(id)+1 from upgrades;  
4 -update upgrades set id = @id where id = 0;  
5 -alter table active_sessions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
6 -alter table archive_restoration_request change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
7 -alter table archiving_settings change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
8 -alter table archiving_type_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
9 -alter table authentication_sources change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
10 -alter table column_entries change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
11 -alter table dashlet_disables change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
12 -alter table data_types change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
13 -alter table discussion_comments change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
14 -alter table discussion_threads change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
15 -alter table document_archiving_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
16 -alter table document_content_version change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
17 -alter table document_fields change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
18 -alter table document_fields_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
19 -alter table document_incomplete change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
20 -alter table document_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
21 -alter table document_link_types change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
22 -alter table document_metadata_version change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
23 -alter table document_role_allocations change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
24 -alter table document_subscriptions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
25 -alter table document_transaction_types_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
26 -alter table document_transactions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
27 -alter table document_type_fields_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
28 -alter table document_type_fieldsets_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
29 -alter table document_types_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
30 -alter table documents change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
31 -alter table field_behaviours change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
32 -alter table field_value_instances change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
33 -alter table fieldsets change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
34 -alter table folder_doctypes_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
35 -alter table folder_subscriptions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
36 -alter table folder_transactions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
37 -alter table folders change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
38 -alter table folders_users_roles_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
39 -alter table groups_groups_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
40 -alter table groups_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
41 -alter table help change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
42 -alter table help_replacement change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
43 -alter table interceptor_instances change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
44 -alter table links change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
45 -alter table metadata_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
46 -alter table metadata_lookup_tree change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
47 -alter table mime_extractors change `id` `id` mediumint(9) NOT NULL AUTO_INCREMENT;  
48 -alter table mime_documents change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
49 -alter table mime_types change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
50 -alter table news change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
51 -alter table notifications change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
52 -alter table organisations_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
53 -alter table permission_assignments change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
54 -alter table permission_descriptors change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
55 -alter table permission_dynamic_conditions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
56 -alter table permission_lookup_assignments change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
57 -alter table permission_lookups change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
58 -alter table permission_objects change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
59 -alter table permissions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
60 -alter table plugin_helper change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
61 -alter table plugin_rss change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
62 -alter table plugins change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
63 -alter table role_allocations change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
64 -alter table roles change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
65 -alter table saved_searches change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
66 -alter table scheduler_tasks change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
67 -alter table search_saved change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
68 -alter table status_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
69 -alter table system_settings change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
70 -alter table tag_words change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
71 -alter table time_period change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
72 -alter table time_unit_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
73 -alter table units_lookup change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
74 -alter table units_organisations_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
75 -alter table upgrades change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
76 -alter table user_history change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
77 -alter table users change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
78 -alter table users_groups_link change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
79 -alter table workflow_state_permission_assignments change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
80 -alter table workflow_states change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
81 -alter table workflow_transitions change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
82 -alter table workflow_trigger_instances change `id` `id` int (11) NOT NULL AUTO_INCREMENT;  
83 -alter table workflows change `id` `id` int (11) NOT NULL AUTO_INCREMENT;