diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index b34806019f3c..4551b89a64f6 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -934,4 +934,16 @@ public function getLogTablesForContact() { return array_intersect($tables, $this->tables); } + /** + * Retrieve missing log tables. + * + * @return array + */ + public function getMissingLogTables() { + if ($this->tablesExist()) { + return array_diff($this->tables, array_keys($this->logs)); + } + return array(); + } + } diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index 72cfd636a63a..41781473d615 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -76,4 +76,31 @@ public function checkIndices() { return $messages; } + /** + * @return array + */ + public function checkMissingLogTables() { + $messages = array(); + $logging = new CRM_Logging_Schema(); + $missingLogTables = $logging->getMissingLogTables(); + + if ($missingLogTables) { + $msg = new CRM_Utils_Check_Message( + __FUNCTION__, + ts("You don't have logging enabled on some tables. This may cause errors on performing insert/update operation on them."), + ts('Missing Log Tables'), + \Psr\Log\LogLevel::WARNING, + 'fa-server' + ); + $msg->addAction( + ts('Create Missing Log Tables'), + ts('Create missing log tables now? This may take few minutes.'), + 'api3', + array('System', 'createmissinglogtables') + ); + $messages[] = $msg; + } + return $messages; + } + } diff --git a/api/v3/System.php b/api/v3/System.php index b4b4974bf0d7..83291c00cd14 100644 --- a/api/v3/System.php +++ b/api/v3/System.php @@ -412,3 +412,19 @@ function civicrm_api3_system_updateindexes() { CRM_Core_BAO_SchemaHandler::createMissingIndices($missingIndices); return civicrm_api3_create_success(1); } + +/** + * Creates missing log tables. + * + * CRM-20838 - This adds any missing log tables into the database. + */ +function civicrm_api3_system_createmissinglogtables() { + $schema = new CRM_Logging_Schema(); + $missingLogTables = $schema->getMissingLogTables(); + if (!empty($missingLogTables)) { + foreach ($missingLogTables as $tableName) { + $schema->fixSchemaDifferencesFor($tableName, NULL, FALSE); + } + } + return civicrm_api3_create_success(1); +} diff --git a/tests/phpunit/api/v3/LoggingTest.php b/tests/phpunit/api/v3/LoggingTest.php index 27ff65e00b3f..15b8ec428b70 100644 --- a/tests/phpunit/api/v3/LoggingTest.php +++ b/tests/phpunit/api/v3/LoggingTest.php @@ -127,6 +127,18 @@ public function testUpdateLegacyLogTable() { ); } + /** + * Check if we can create missing log tables using api. + */ + public function testCreateMissingLogTables() { + $this->callAPISuccess('Setting', 'create', array('logging' => TRUE)); + CRM_Core_DAO::executeQuery("DROP TABLE log_civicrm_contact"); + $this->callAPISuccess('System', 'createmissinglogtables', array()); + + //Assert if log_civicrm_contact is created. + $this->checkLogTableCreated(); + } + /** * Check we can update legacy log tables using the api function. */