2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'include/database/DBManagerFactory.php';
40 class RepairDatabaseTest extends Sugar_PHPUnit_Framework_TestCase
45 public function setUp()
48 $this->markTestSkipped('Skip for now');
49 $this->db = DBManagerFactory::getInstance();
50 if($this->db->dbType == 'mysql')
52 $sql = 'ALTER TABLE meetings ALTER COLUMN status SET DEFAULT NULL';
53 $sql2 = 'ALTER TABLE calls ALTER COLUMN status SET DEFAULT NULL';
54 $sql3 = 'ALTER TABLE tasks ALTER COLUMN status SET DEFAULT NULL';
57 $this->db->query($sql);
58 $this->db->query($sql2);
59 $this->db->query($sql3);
65 public function tearDown()
67 if($this->db->dbType == 'mysql')
69 $sql = "ALTER TABLE meetings ALTER COLUMN status SET DEFAULT 'Planned'";
70 $sql2 = "ALTER TABLE calls ALTER COLUMN status SET DEFAULT 'Planned'";
71 $sql3 = "ALTER TABLE tasks ALTER COLUMN status SET DEFAULT 'Not Started'";
73 $this->db->query($sql);
74 $this->db->query($sql2);
75 $this->db->query($sql3);
79 public function testRepairTableParams()
81 if($this->db->dbType != 'mysql')
83 $this->markTestSkipped('Skip if not mysql db');
87 $bean = new Meeting();
88 $result = $this->getRepairTableParamsResult($bean);
89 $this->assertRegExp('/ALTER TABLE meetings\s+?modify column status varchar\(100\) DEFAULT \'Planned\' NULL/i', $result);
93 $result = $this->getRepairTableParamsResult($bean);
94 $this->assertTrue(!empty($result));
95 $this->assertRegExp('/ALTER TABLE calls\s+?modify column status varchar\(100\) DEFAULT \'Planned\' NULL/i', $result);
99 $result = $this->getRepairTableParamsResult($bean);
100 $this->assertTrue(!empty($result));
101 $this->assertRegExp('/ALTER TABLE tasks\s+?modify column status varchar\(100\) DEFAULT \'Not Started\' NULL/i', $result);
105 private function getRepairTableParamsResult($bean)
107 $indices = $bean->getIndices();
108 $fielddefs = $bean->getFieldDefinitions();
109 $tablename = $bean->getTableName();
111 //Clean the indicies to prevent duplicate definitions
112 $new_indices = array();
113 foreach($indices as $ind_def)
115 $new_indices[$ind_def['name']] = $ind_def;
120 if (isset($dictionary[$bean->getObjectName()]['engine']) && !empty($dictionary[$bean->getObjectName()]['engine']) )
122 $engine = $dictionary[$bean->getObjectName()]['engine'];
126 $result = $this->db->repairTableParams($bean->table_name, $fielddefs, $new_indices, false, $engine);