2 if(!defined('sugarEntry') || !sugarEntry)
3 die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
39 require_once ('modules/DynamicFields/DynamicField.php');
40 require_once ('modules/DynamicFields/FieldCases.php');
44 $db = DBManagerFactory:: getInstance();
47 if (!isset ($dbManager)) {
48 $dbManager = DBManagerFactory::getInstance();
51 $result = $db->query('SELECT * FROM fields_meta_data WHERE deleted = 0 ORDER BY custom_module');
54 * get the real field_meta_data
56 while ($row = $db->fetchByAssoc($result)) {
57 $the_modules = $row['custom_module'];
58 if (!isset ($modules[$the_modules])) {
59 $modules[$the_modules] = array ();
61 $modules[$the_modules][$row['name']] = $row['name'];
65 if (!isset ($_REQUEST['run'])) {
67 echo "SIMULATION MODE - NO CHANGES WILL BE MADE EXCEPT CLEARING CACHE";
70 foreach ($modules as $the_module => $fields) {
71 $class_name = $beanList[$the_module];
72 echo "<br><br>Scanning $the_module <br>";
74 require_once ($beanFiles[$class_name]);
75 $mod = new $class_name ();
76 if (!$db->tableExists($mod->table_name."_cstm")) {
77 $mod->custom_fields = new DynamicField();
78 $mod->custom_fields->setup($mod);
79 $mod->custom_fields->createCustomTable();
82 if ($db->dbType == 'oci8') {
83 } elseif ($db->dbType == 'mssql') {
84 $def_query="SELECT col.name as field, col_type.name as type, col.is_nullable, col.precision as data_precision, col.max_length as data_length, col.scale data_scale";
85 $def_query.=" FROM sys.columns col";
86 $def_query.=" join sys.types col_type on col.user_type_id=col_type.user_type_id ";
87 $def_query.=" where col.object_id = (select object_id(sys.schemas.name + '.' + sys.tables.name)";
88 $def_query.=" from sys.tables join sys.schemas on sys.schemas.schema_id = sys.tables.schema_id";
89 $def_query.=" where sys.tables.name='".$mod->table_name."_cstm')";
90 $result = $db->query($def_query);
93 $result = $db->query("DESCRIBE $mod->table_name"."_cstm");
96 while ($row = $db->fetchByAssoc($result)) {
97 $col = strtolower(empty ($row['Field']) ? $row['field'] : $row['Field']);
98 $the_field = $mod->custom_fields->getField($col);
99 $type = strtolower(empty ($row['Type']) ? $row['type'] : $row['Type']);
100 if ($db->dbType == 'oci8' or $db->dbType == 'mssql') {
101 if ($row['data_precision']!= '' and $row['data_scale']!='') {
102 $type.='(' . $row['data_precision'];
103 if (!empty($row['data_scale'])) {
104 $type.=',' . $row['data_scale'];
108 if ($row['data_length']!= '' && (strtolower($row['type'])=='varchar' or strtolower($row['type'])=='varchar2')) {
109 $type.='(' . $row['data_length'] . ')';
114 if (!isset ($fields[$col]) && $col != 'id_c') {
116 $db->query("ALTER TABLE $mod->table_name"."_cstm DROP COLUMN $col");
118 unset ($fields[$col]);
119 echo "Dropping Column $col from $mod->table_name"."_cstm for module $the_module<br>";
121 if ($col != 'id_c') {
122 //$db_data_type = $dbManager->helper->getColumnType(strtolower($the_field->data_type));
123 $db_data_type = strtolower(str_replace(' ' , '', $the_field->get_db_type()));
125 $type = strtolower(str_replace(' ' , '', $type));
126 if (strcmp($db_data_type,$type) != 0) {
128 echo "Fixing Column Type for $col changing $type to ".$db_data_type."<br>";
130 $db->query($the_field->get_db_modify_alter_table($mod->table_name.'_cstm'));
135 unset ($fields[$col]);
140 echo sizeof($fields)." field(s) missing from $mod->table_name"."_cstm<br>";
141 foreach ($fields as $field) {
142 echo "Adding Column $field to $mod->table_name"."_cstm<br>";
144 $mod->custom_fields->add_existing_custom_field($field);
149 DynamicField :: deleteCache();
152 echo '<a href="index.php?module=Administration&action=UpgradeFields&run=true">Execute non-simulation mode</a>';