mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
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:
parent
ff35a0129f
commit
a8d7d6175a
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user