1<?php 2 3/* 4 * Add all indexes we've implemented over time. Check if exists 5 * to avoid errors. 6 */ 7class AddIndexMigration extends MigrationTask { 8 var $description = "Add indexes accumulated over time"; 9 var $indexes = array( 10 'department:flags', 11 'draft:staff_id', 12 'draft:namespace', 13 'file:type', 14 'file:created', 15 'file:size', 16 'form:type', 17 'form_field:form_id', 18 'form_field:sort', 19 'queue:staff_id', 20 'queue:parent_id', 21 'staff:isactive', 22 'staff:onvacation', 23 'task:flags', 24 'ticket:ticket_pid', 25 'team_member:staff_id', 26 'user:default_email_id', 27 'user:name' 28 ); 29 30 function run($max_time) { 31 global $ost; 32 33 foreach ($this->indexes as $index) { 34 list($t, $i) = explode(':', $index); 35 36 // Check if INDEX already exists via SHOW INDEX 37 $sql = sprintf("SHOW INDEX FROM `%s` WHERE `Column_name` = '%s' AND `Key_name` != 'PRIMARY'", 38 TABLE_PREFIX.$t, $i); 39 40 // Hardfail if we cannot check if exists 41 if(!($res=db_query($sql))) 42 return $this->error('Unable to query DB for Add Index migration!'); 43 44 $count = db_num_rows($res); 45 46 if (!$count || ($count && ($count == 0))) { 47 // CREATE INDEX if not exists 48 $create = sprintf('CREATE INDEX `%s` ON `%s`.`%s` (`%s`)', 49 $i, DBNAME, TABLE_PREFIX.$t, $i); 50 51 if(!($res=db_query($create))) { 52 $message = "Unable to create index `$i` on `".TABLE_PREFIX.$t."`."; 53 // Log the error but don't send the alert email 54 $ost->logError('Upgrader: Add Index Migrater', $message, false); 55 } 56 } 57 } 58 59 //add permissions to staff 60 foreach (Staff::objects() as $staff) { 61 $perms = array(); 62 foreach ($staff->getPermissionInfo() as $value => $setting) 63 $perms[] = $value; 64 65 array_push($perms, 'visibility.departments', 'visibility.agents'); 66 $staff->updatePerms($perms); 67 $staff->save(); 68 } 69 70 //add 2fa template 71 foreach (array('email2fa-staff') as $type) { 72 $i18n = new Internationalization(); 73 $tpl = $i18n->getTemplate("templates/page/{$type}.yaml"); 74 if (!($page = $tpl->getData())) 75 // No such template on disk 76 continue; 77 78 if ($id = db_result(db_query('select id from '.PAGE_TABLE 79 .' where `type`='.db_input($type)))) 80 // Already have a template for the content type 81 continue; 82 83 $sql = 'INSERT INTO '.PAGE_TABLE.' SET type='.db_input($type) 84 .', name='.db_input($page['name']) 85 .', body='.db_input($page['body']) 86 .', notes='.db_input($page['notes']) 87 .', created=NOW(), updated=NOW(), isactive=1'; 88 db_query($sql); 89 } 90 91 // See if there are missing events that should be added to the database 92 $event_type = array('login', 'logout', 'message', 'note'); 93 foreach($event_type as $eType) { 94 $sql = sprintf("SELECT * FROM `%s` WHERE name = '%s'", 95 TABLE_PREFIX.'event', $eType); 96 97 $res=db_query($sql); 98 $count = db_num_rows($res); 99 100 if($count > 0) { 101 $message = "Event '$eType' already exists."; 102 $ost->logError('Upgrader: Add Events', $message, false); 103 } else { 104 // Add event 105 $sql = sprintf("INSERT INTO `%s` (`id`, `name`, `description`) 106 VALUES 107 ('','%s',NULL)", 108 TABLE_PREFIX.'event', $eType); 109 110 if(!($res=db_query($sql))) { 111 $message = "Unable to add $eType event to `".TABLE_PREFIX.'event'."`."; 112 $ost->logError('Upgrader: Add Events', $message, false); 113 } 114 } 115 } 116 } 117} 118return 'AddIndexMigration'; 119 120?> 121