2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 global $current_user,$beanFiles;
43 $db = & DBManagerFactory::getInstance();
44 if ($db->dbType == 'oci8') {
46 echo "<p>".$mod_strings['ERR_NOT_FOR_ORACLE']."</p>";
50 if ($db->dbType == 'mysql') {
52 echo "<p>".$mod_strings['ERR_NOT_FOR_MYSQL']."</p>";
56 if(is_admin($current_user) || isset($from_sync_client)){
62 if(isset($_REQUEST['do_action'])){
63 switch($_REQUEST['do_action']){
70 header('Location: index.php?module=Administration&action=expandDatabase&do_action=do_export&to_pdf=true');
77 if(!$export && empty($_REQUEST['repair_silent'])){
78 echo get_module_title($mod_strings['LBL_EXPAND_DATABASE_COLUMNS'], $mod_strings['LBL_EXPAND_DATABASE_COLUMNS'].':'.$_REQUEST['do_action'], true);
81 $alter_queries = array();
82 $restore_quries = array();
83 $sql = "SELECT SO.name AS table_name, SC.name AS column_name, CONVERT(int, SC.length) AS length, SC.isnullable, type_name(SC.xusertype) AS type
84 FROM sys.sysobjects AS SO INNER JOIN sys.syscolumns AS SC ON SC.id = SO.id
86 AND (type_name(SC.xusertype) IN ('varchar', 'char', ' text '))
87 AND (SC.name NOT LIKE '%_id') AND (SC.name NOT LIKE 'id_%') AND (SC.name <> 'id')
88 ORDER BY SO.name, column_name";
89 $result = $db->query($sql);
92 $theAlterQueries = '';
93 $theRestoreQueries = '';
94 $alter_queries = array();
95 while ($row = $db->fetchByAssoc($result)) {
96 $length = (int)$row['length'];
98 $newLength = ($length * 3 < 255) ? $length * 3 : 255;
99 $sql = 'ALTER TABLE ' . $row['table_name'] . ' ALTER COLUMN ' . $row['column_name'] . ' ' . $row['type'] . ' (' . $newLength . ')';
100 $theAlterQueries .= $sql . "\n";
101 $alter_queries[] = $sql;
103 $sql2 = 'ALTER TABLE ' . $row['table_name'] . ' ALTER COLUMN ' . $row['column_name'] . ' ' . $row['type'] . ' (' . $length . ')';
104 $theRestoreQueries .= $sql2 . "\n";
108 //If there are no ALTER queries to run, echo message
109 if(count($alter_queries) == 0) {
110 echo $mod_strings['ERR_NO_COLUMNS_TO_EXPAND'];
113 // Create a backup file to restore columns to original length
115 $fh = sugar_fopen('restoreExpand.sql', 'w');
116 if(-1 == fwrite($fh, $theRestoreQueries)) {
117 $GLOBALS['log']->error($mod_strings['ERR_CANNOT_CREATE_RESTORE_FILE']);
118 echo($mod_strings['ERR_CANNOT_CREATE_RESTORE_FILE']);
120 $GLOBALS['log']->info($mod_strings['LBL_CREATE_RESOTRE_FILE']);
121 echo($mod_strings['LBL_CREATE_RESOTRE_FILE']);
124 foreach($alter_queries as $key=>$value) {
130 header("Content-Disposition: attachment; filename=expandSugarDB.sql");
131 header("Content-Type: text/sql; charset={$app_strings['LBL_CHARSET']}");
132 header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
133 header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
134 header( "Cache-Control: post-check=0, pre-check=0", false );
135 header("Content-Length: ".strlen($theAlterQueries));
136 echo $theAlterQueries;
139 if(empty($_REQUEST['repair_silent'])) {
140 echo nl2br($theAlterQueries);
147 if(empty($_REQUEST['repair_silent']) && empty($_REQUEST['do_action'])) {
148 if(!file_exists('restoreExpand.sql')) {
149 echo " <b>{$mod_strings['LBL_REPAIR_ACTION']}</b><br>
150 <form name='repairdb'>
151 <input type='hidden' name='action' value='expandDatabase'>
152 <input type='hidden' name='module' value='Administration'>
154 <select name='do_action'>
155 <option value='display'>".$mod_strings['LBL_REPAIR_DISPLAYSQL']."
156 <option value='export'>".$mod_strings['LBL_REPAIR_EXPORTSQL']."
157 <option value='execute'>".$mod_strings['LBL_REPAIR_EXECUTESQL']."
158 </select><input type='submit' class='button' value='".$mod_strings['LBL_GO']."'>
160 ".$mod_strings['LBL_EXPAND_DATABASE_TEXT'];
162 echo "<b>{$mod_strings['LBL_EXPAND_DATABASE_FINISHED_ERROR']}</b><br>";
166 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);