Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20838 - Add check for missing log tables on system status #10628

Merged
merged 2 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
27 changes: 27 additions & 0 deletions CRM/Utils/Check/Component/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
16 changes: 16 additions & 0 deletions api/v3/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 12 additions & 0 deletions tests/phpunit/api/v3/LoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down