// drop old indices
if (isset($indices[$table])) {
foreach ($indices[$table] as $index) {
- $queries[] = "DROP INDEX {$index['name']} ON {$table}";
+ if (CRM_Core_BAO_SchemaHandler::checkIfIndexExists($table, $index['name'])) {
+ $queries[] = "DROP INDEX {$index['name']} ON {$table}";
+ }
}
}
// deal with columns
foreach ($hash as $column => $type) {
$queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
- $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}";
- $queries[] = "ALTER TABLE {$table} DROP {$column}";
+ if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
+ $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}";
+ $queries[] = "ALTER TABLE {$table} DROP {$column}";
+ }
}
// add view
if (!empty($cols[$alterType])) {
foreach ($cols[$alterType] as $col) {
$line = $this->_getColumnQuery($col, $create);
- CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}");
+ CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}", array(), TRUE, NULL, FALSE, FALSE);
}
}
}
foreach ($cols['OBSOLETE'] as $col) {
$line = $this->_getColumnQuery($col, $create);
// This is just going to make a not null column to nullable
- CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}");
+ CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}", array(), TRUE, NULL, FALSE, FALSE);
}
}
* @return array
*/
private function _getCreateQuery($table) {
- $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE {$table}");
+ $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE {$table}", array(), TRUE, NULL, FALSE, FALSE);
$dao->fetch();
$create = explode("\n", $dao->Create_Table);
return $create;
--- /dev/null
+<?php
+
+/**
+ * Class CRM_Core_DAOTest
+ * @group headless
+ */
+class CRM_Logging_LoggingTest extends CiviUnitTestCase {
+
+ public function setUp() {
+ parent::setUp();
+ }
+
+ public function tearDown() {
+ CRM_Core_I18n_Schema::makeSinglelingual('en_US');
+ $logging = new CRM_Logging_Schema();
+ $logging->dropAllLogTables();
+ parent::tearDown();
+ }
+
+ /**
+ * Test creating logging schema when database is in multilingual mode.
+ */
+ public function testMultilingualLogging() {
+ CRM_Core_I18n_Schema::makeMultilingual('en_US');
+ $logging = new CRM_Logging_Schema();
+ $logging->enableLogging();
+ $value = CRM_Core_DAO::singleValueQuery("SELECT id FROM log_civicrm_contact LIMIT 1", array(), FALSE, FALSE);
+ $this->assertNotNull($value, 'Logging not enabled successfully');
+ $logging->disableLogging();
+ }
+
+
+ /**
+ * Test creating logging schema when database is in multilingual mode.
+ * Also test altering a multilingual table.
+ */
+ public function testMultilingualAlterSchemaLogging() {
+ CRM_Core_I18n_Schema::makeMultilingual('en_US');
+ $logging = new CRM_Logging_Schema();
+ $logging->enableLogging();
+ $value = CRM_Core_DAO::singleValueQuery("SELECT id FROM log_civicrm_contact LIMIT 1", array(), FALSE, FALSE);
+ $this->assertNotNull($value, 'Logging not enabled successfully');
+ $logging->disableLogging();
+ CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` ADD COLUMN `logging_test` INT DEFAULT NULL", array(), FALSE, NULL, FALSE, TRUE);
+ CRM_Core_I18n_Schema::rebuildMultilingualSchema(array('en_US'));
+ $logging->enableLogging();
+ $query = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE `log_civicrm_option_value`", array(), TRUE, NULL, FALSE, FALSE);
+ $query->fetch();
+ $create = explode("\n", $query->Create_Table);
+ CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`", array(), FALSE, NULL, FALSE, TRUE);
+ $this->assertTrue(in_array(" `logging_test` int(11) DEFAULT NULL", $create));
+ $logging->disableLogging();
+ }
+
+}