In the installation script added the ability to add indexes to the database without addition of columns.

This commit is contained in:
Dmitriy Simushev 2012-02-26 14:00:24 +00:00
parent 61c52eb127
commit 354ffafa0b
3 changed files with 77 additions and 35 deletions

View File

@ -263,4 +263,22 @@ function get_columns($tablename, $link)
}
}
function get_indexes($tablename, $link)
{
global $mysqldb, $errors;
$result = mysql_query("SELECT index_name FROM information_schema.statistics where table_schema = '$mysqldb' and table_name = '$tablename' and index_name != 'PRIMARY'", $link);
if ($result) {
$arr = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$arr[] = $row[0];
}
mysql_free_result($result);
return $arr;
} else {
$errors[] = "Cannot get indexes for table \"$tablename\". Error: " . mysql_error($link);
return false;
}
}
?>

View File

@ -65,7 +65,9 @@ if ($act == "silentcreateall") {
mysql_query("DROP TABLE IF EXISTS $id", $link) or show_install_err(' Query failed: ' . mysql_error($link));
}
} else if ($act == "addcolumns") {
$absent = array();
// Add absent columns
$absent_columns = array();
foreach ($dbtables as $id => $columns) {
$curr_columns = get_columns($id, $link);
if ($curr_columns === false) {
@ -73,127 +75,137 @@ if ($act == "silentcreateall") {
}
$tocreate = array_diff(array_keys($columns), $curr_columns);
foreach ($tocreate as $v) {
$absent[] = "$id.$v";
$absent_columns[] = "$id.$v";
}
}
if (in_array("${mysqlprefix}chatmessage.agentId", $absent)) {
if (in_array("${mysqlprefix}chatmessage.agentId", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD agentId int NOT NULL DEFAULT 0 AFTER ikind", $link);
runsql("update ${mysqlprefix}chatmessage, ${mysqlprefix}chatoperator set agentId = operatorid where agentId = 0 AND ikind = 2 AND (vclocalename = tname OR vccommonname = tname)", $link);
}
if (in_array("${mysqlprefix}chatthread.agentId", $absent)) {
if (in_array("${mysqlprefix}chatthread.agentId", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD agentId int NOT NULL DEFAULT 0 AFTER agentName", $link);
runsql("update ${mysqlprefix}chatthread, ${mysqlprefix}chatoperator set agentId = operatorid where agentId = 0 AND (vclocalename = agentName OR vccommonname = agentName)", $link);
}
if (in_array("${mysqlprefix}chatthread.dtmchatstarted", $absent)) {
if (in_array("${mysqlprefix}chatthread.dtmchatstarted", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD dtmchatstarted datetime DEFAULT 0 AFTER dtmcreated", $link);
runsql("update ${mysqlprefix}chatthread set dtmchatstarted = dtmcreated", $link);
}
if (in_array("${mysqlprefix}chatthread.agentTyping", $absent)) {
if (in_array("${mysqlprefix}chatthread.agentTyping", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD agentTyping int DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatthread.userTyping", $absent)) {
if (in_array("${mysqlprefix}chatthread.userTyping", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD userTyping int DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatthread.messageCount", $absent)) {
if (in_array("${mysqlprefix}chatthread.messageCount", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD messageCount varchar(16)", $link);
runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD INDEX idx_threadid_ikind (threadid, ikind)", $link);
runsql("UPDATE ${mysqlprefix}chatthread t SET t.messageCount = (SELECT COUNT(*) FROM ${mysqlprefix}chatmessage WHERE ${mysqlprefix}chatmessage.threadid = t.threadid AND ikind = 1)", $link);
runsql("ALTER TABLE ${mysqlprefix}chatmessage DROP INDEX idx_threadid_ikind", $link);
}
if (in_array("${mysqlprefix}chatthread.nextagent", $absent)) {
if (in_array("${mysqlprefix}chatthread.nextagent", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD nextagent int NOT NULL DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatthread.shownmessageid", $absent)) {
if (in_array("${mysqlprefix}chatthread.shownmessageid", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD shownmessageid int NOT NULL DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatthread.userid", $absent)) {
if (in_array("${mysqlprefix}chatthread.userid", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD userid varchar(255) DEFAULT \"\"", $link);
}
if (in_array("${mysqlprefix}chatoperator.iperm", $absent)) {
if (in_array("${mysqlprefix}chatoperator.iperm", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD iperm int DEFAULT 65535", $link);
}
if (in_array("${mysqlprefix}chatoperator.istatus", $absent)) {
if (in_array("${mysqlprefix}chatoperator.istatus", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD istatus int DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatoperator.idisabled", $absent)) {
if (in_array("${mysqlprefix}chatoperator.idisabled", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD idisabled int DEFAULT 0 AFTER istatus", $link);
}
if (in_array("${mysqlprefix}chatoperator.vcavatar", $absent)) {
if (in_array("${mysqlprefix}chatoperator.vcavatar", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcavatar varchar(255)", $link);
}
if (in_array("${mysqlprefix}chatoperator.vcjabbername", $absent)) {
if (in_array("${mysqlprefix}chatoperator.vcjabbername", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcjabbername varchar(255)", $link);
}
if (in_array("${mysqlprefix}chatoperator.vcemail", $absent)) {
if (in_array("${mysqlprefix}chatoperator.vcemail", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcemail varchar(64)", $link);
}
if (in_array("${mysqlprefix}chatoperator.dtmrestore", $absent)) {
if (in_array("${mysqlprefix}chatoperator.dtmrestore", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD dtmrestore datetime DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatoperator.vcrestoretoken", $absent)) {
if (in_array("${mysqlprefix}chatoperator.vcrestoretoken", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcrestoretoken varchar(64)", $link);
}
if (in_array("${mysqlprefix}chatresponses.vctitle", $absent)) {
if (in_array("${mysqlprefix}chatresponses.vctitle", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatresponses ADD vctitle varchar(100) NOT NULL DEFAULT '' AFTER groupid", $link);
}
if (in_array("${mysqlprefix}chatthread.groupid", $absent)) {
if (in_array("${mysqlprefix}chatthread.groupid", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD groupid int references ${mysqlprefix}chatgroup(groupid)", $link);
}
if (in_array("${mysqlprefix}chatthread.userAgent", $absent)) {
if (in_array("${mysqlprefix}chatthread.userAgent", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatthread ADD userAgent varchar(255)", $link);
}
if (in_array("${mysqlprefix}chatgroup.vcemail", $absent)) {
if (in_array("${mysqlprefix}chatgroup.vcemail", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD vcemail varchar(64)", $link);
}
if (in_array("${mysqlprefix}chatgroup.iweight", $absent)) {
if (in_array("${mysqlprefix}chatgroup.iweight", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD iweight int DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatgroup.parent", $absent)) {
if (in_array("${mysqlprefix}chatgroup.parent", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD parent int DEFAULT NULL AFTER groupid", $link);
}
$res = mysql_query("select null from information_schema.statistics where table_schema = '$mysqldb' and table_name = '${mysqlprefix}chatgroup' and index_name = 'parent'", $link);
if ($res && mysql_num_rows($res) == 0) {
// Add absent indexes
$absent_indexes = array();
foreach ($dbtables_indexes as $id => $indexes) {
$curr_indexes = get_indexes($id, $link);
if ($curr_indexes === false) {
show_install_err($errors[0]);
}
$tocreate = array_diff(array_keys($indexes), $curr_indexes);
foreach ($tocreate as $i) {
$absent_indexes[] = "$id.$i";
}
}
if (in_array("${mysqlprefix}chatgroup.parent", $absent_indexes)) {
runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD INDEX (parent)", $link);
}
$res = mysql_query("select null from information_schema.statistics where table_schema = '$mysqldb' and table_name = '${mysqlprefix}chatmessage' and index_name = 'idx_agentid'", $link);
if ($res && mysql_num_rows($res) == 0) {
if (in_array("${mysqlprefix}chatmessage.idx_agentid", $absent_indexes)) {
runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD INDEX idx_agentid (agentid)", $link);
}
$res = mysql_query("select null from information_schema.statistics where table_schema = '$mysqldb' and table_name = '${mysqlprefix}chatsitevisitor' and index_name = 'threadid'", $link);
if ($res && mysql_num_rows($res) == 0) {
if (in_array("${mysqlprefix}chatsitevisitor.threadid", $absent_indexes)) {
runsql("ALTER TABLE ${mysqlprefix}chatsitevisitor ADD INDEX (threadid)", $link);
}
$res = mysql_query("select null from information_schema.statistics where table_schema = '$mysqldb' and table_name = '${mysqlprefix}visitedpage' and index_name = 'visitorid'", $link);
if ($res && mysql_num_rows($res) == 0) {
if (in_array("${mysqlprefix}visitedpage.visitorid", $absent_indexes)) {
runsql("ALTER TABLE ${mysqlprefix}visitedpage ADD INDEX (visitorid)", $link);
}
}
}

View File

@ -199,7 +199,7 @@ function check_tables($link)
function check_columns($link)
{
global $dbtables, $dbtables_can_update, $errors, $page, $webimroot;
global $dbtables, $dbtables_can_update, $dbtables_indexes, $errors, $page, $webimroot;
$need_to_create_columns = false;
foreach ($dbtables as $id => $columns) {
@ -221,7 +221,19 @@ function check_columns($link)
}
}
if ($need_to_create_columns) {
$need_to_create_indexes = false;
foreach ($dbtables_indexes as $id => $indexes) {
$curr_indexes = get_indexes($id, $link);
if ($curr_indexes === false) {
return false;
}
$tocreate = array_diff(array_keys($indexes), $curr_indexes);
if (count($tocreate) != 0) {
$need_to_create_indexes = true;
}
}
if ($need_to_create_columns || $need_to_create_indexes) {
$page['nextstep'] = getlocal("install.4.create");
$page['nextstepurl'] = "$webimroot/install/dbperform.php?act=addcolumns";
$page['nextnotice'] = getlocal("install.4.notice");