MDL-45214 always try to use ID sort in get_events_select()

This should make PostgreSQL behave more like MySQL
and resolve the problem when events were appearing in different
order.
This commit is contained in:
Petr Skoda 2014-04-23 13:54:28 +08:00
parent ff35a0129f
commit a8d7d6175a
6 changed files with 30 additions and 2 deletions

View File

@ -61,4 +61,26 @@ trait reader {
}
return $this->store;
}
/**
* Adds ID column to $sort to make sure events from one request
* within 1 second are returned in the same order.
*
* @param string $sort
* @return string sort string
*/
protected static function tweak_sort_by_id($sort) {
if (empty($sort)) {
// Mysql does this - unlikely to be used in real life because $sort is always expected.
$sort = "id ASC";
} else if (stripos($sort, 'timecreated') === false) {
$sort .= ", id ASC";
} else if (stripos($sort, 'timecreated DESC') !== false) {
$sort .= ", id DESC";
} else {
$sort .= ", id ASC";
}
return $sort;
}
}

View File

@ -167,6 +167,8 @@ class store implements \tool_log\log\writer, \core\log\sql_select_reader {
return array();
}
$sort = self::tweak_sort_by_id($sort);
$events = array();
$records = $this->extdb->get_records_select($dbtable, $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);

View File

@ -152,7 +152,7 @@ class logstore_database_store_testcase extends advanced_testcase {
// Test reading.
$this->assertSame(3, $store->get_events_select_count('', array()));
$events = $store->get_events_select('', array(), 'id', 0, 0);
$events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);

View File

@ -86,6 +86,8 @@ class store implements \tool_log\log\store, \core\log\sql_select_reader {
public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
global $DB;
$sort = self::tweak_sort_by_id($sort);
// Replace the query with hardcoded mappings required for core.
list($selectwhere, $params, $sort) = self::replace_sql_legacy($selectwhere, $params, $sort);

View File

@ -69,6 +69,8 @@ class store implements \tool_log\log\writer, \core\log\sql_internal_reader {
public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
global $DB;
$sort = self::tweak_sort_by_id($sort);
$events = array();
$records = $DB->get_records_select('logstore_standard_log', $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);

View File

@ -122,7 +122,7 @@ class logstore_standard_store_testcase extends advanced_testcase {
// Test reading.
$this->assertSame(3, $store->get_events_select_count('', array()));
$events = $store->get_events_select('', array(), 'id', 0, 0);
$events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);