2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 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 ********************************************************************************/
38 $db = DBManagerFactory::getInstance();
39 if(!$db->supports('fix:expandDatabase')) {
41 echo "<p>".$mod_strings['ERR_NOT_IMPLEMENTED']."</p>";
45 global $current_user,$beanFiles;
48 if(is_admin($current_user) || isset($from_sync_client)){
54 if(isset($_REQUEST['do_action'])){
55 switch($_REQUEST['do_action']){
62 header('Location: index.php?module=Administration&action=expandDatabase&do_action=do_export&to_pdf=true');
69 if(!$export && empty($_REQUEST['repair_silent'])){
70 echo getClassicModuleTitle($mod_strings['LBL_EXPAND_DATABASE_COLUMNS'], array($mod_strings['LBL_EXPAND_DATABASE_COLUMNS'],$_REQUEST['do_action']), true);
73 $alter_queries = array();
74 $restore_quries = array();
75 $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
76 FROM sys.sysobjects AS SO INNER JOIN sys.syscolumns AS SC ON SC.id = SO.id
78 AND (type_name(SC.xusertype) IN ('varchar', 'char', ' text '))
79 AND (SC.name NOT LIKE '%_id') AND (SC.name NOT LIKE 'id_%') AND (SC.name <> 'id')
80 ORDER BY SO.name, column_name";
81 $result = $db->query($sql);
84 $theAlterQueries = '';
85 $theRestoreQueries = '';
86 $alter_queries = array();
87 while ($row = $db->fetchByAssoc($result)) {
88 $length = (int)$row['length'];
90 $newLength = ($length * 3 < 255) ? $length * 3 : 255;
91 $sql = 'ALTER TABLE ' . $row['table_name'] . ' ALTER COLUMN ' . $row['column_name'] . ' ' . $row['type'] . ' (' . $newLength . ')';
92 $theAlterQueries .= $sql . "\n";
93 $alter_queries[] = $sql;
95 $sql2 = 'ALTER TABLE ' . $row['table_name'] . ' ALTER COLUMN ' . $row['column_name'] . ' ' . $row['type'] . ' (' . $length . ')';
96 $theRestoreQueries .= $sql2 . "\n";
100 //If there are no ALTER queries to run, echo message
101 if(count($alter_queries) == 0) {
102 echo $mod_strings['ERR_NO_COLUMNS_TO_EXPAND'];
105 // Create a backup file to restore columns to original length
107 $fh = sugar_fopen('restoreExpand.sql', 'w');
108 if(-1 == fwrite($fh, $theRestoreQueries)) {
109 $GLOBALS['log']->error($mod_strings['ERR_CANNOT_CREATE_RESTORE_FILE']);
110 echo($mod_strings['ERR_CANNOT_CREATE_RESTORE_FILE']);
112 $GLOBALS['log']->info($mod_strings['LBL_CREATE_RESOTRE_FILE']);
113 echo($mod_strings['LBL_CREATE_RESOTRE_FILE']);
116 foreach($alter_queries as $key=>$value) {
122 header("Content-Disposition: attachment; filename=expandSugarDB.sql");
123 header("Content-Type: text/sql; charset={$app_strings['LBL_CHARSET']}");
124 header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
125 header( "Last-Modified: " . TimeDate::httpTime() );
126 header( "Cache-Control: post-check=0, pre-check=0", false );
127 header("Content-Length: ".strlen($theAlterQueries));
128 echo $theAlterQueries;
131 if(empty($_REQUEST['repair_silent'])) {
132 echo nl2br($theAlterQueries);
139 if(empty($_REQUEST['repair_silent']) && empty($_REQUEST['do_action'])) {
140 if(!file_exists('restoreExpand.sql')) {
141 echo " <b>{$mod_strings['LBL_REPAIR_ACTION']}</b><br>
142 <form name='repairdb'>
143 <input type='hidden' name='action' value='expandDatabase'>
144 <input type='hidden' name='module' value='Administration'>
146 <select name='do_action'>
147 <option value='display'>".$mod_strings['LBL_REPAIR_DISPLAYSQL']."
148 <option value='export'>".$mod_strings['LBL_REPAIR_EXPORTSQL']."
149 <option value='execute'>".$mod_strings['LBL_REPAIR_EXECUTESQL']."
150 </select><input type='submit' class='button' value='".$mod_strings['LBL_GO']."'>
152 ".$mod_strings['LBL_EXPAND_DATABASE_TEXT'];
154 echo "<b>{$mod_strings['LBL_EXPAND_DATABASE_FINISHED_ERROR']}</b><br>";
158 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);