mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Ensure ordering by title is consistent between database engines
This commit is contained in:
@@ -46,6 +46,8 @@ class ShortUrlListRepository extends EntitySpecificationRepository implements Sh
|
||||
'DISTINCT s AS shortUrl',
|
||||
'(' . $buildVisitsSubQuery('v', excludingBots: false) . ') AS ' . OrderableField::VISITS->value,
|
||||
'(' . $buildVisitsSubQuery('v2', excludingBots: true) . ') AS ' . OrderableField::NON_BOT_VISITS->value,
|
||||
// This is added only to have a consistent order by title between database engines
|
||||
'COALESCE(s.title, \'\') AS title',
|
||||
)
|
||||
->setMaxResults($filtering->limit)
|
||||
->setFirstResult($filtering->offset)
|
||||
@@ -62,15 +64,17 @@ class ShortUrlListRepository extends EntitySpecificationRepository implements Sh
|
||||
private function processOrderByForList(QueryBuilder $qb, ShortUrlsListFiltering $filtering): void
|
||||
{
|
||||
$fieldName = $filtering->orderBy->field;
|
||||
$order = $filtering->orderBy->direction;
|
||||
|
||||
match (true) {
|
||||
$direction = $filtering->orderBy->direction;
|
||||
[$sort, $order] = match (true) {
|
||||
// With no explicit order by, fallback to dateCreated-DESC
|
||||
$fieldName === null => $qb->orderBy('s.dateCreated', 'DESC'),
|
||||
$fieldName === null => ['s.dateCreated', 'DESC'],
|
||||
$fieldName === OrderableField::VISITS->value,
|
||||
$fieldName === OrderableField::NON_BOT_VISITS->value => $qb->orderBy($fieldName, $order),
|
||||
default => $qb->orderBy('s.' . $fieldName, $order),
|
||||
$fieldName === OrderableField::NON_BOT_VISITS->value,
|
||||
$fieldName === OrderableField::TITLE->value => [$fieldName, $direction],
|
||||
default => ['s.' . $fieldName, $direction],
|
||||
};
|
||||
|
||||
$qb->orderBy($sort, $order);
|
||||
}
|
||||
|
||||
public function countList(ShortUrlsCountFiltering $filtering): int
|
||||
|
||||
@@ -201,12 +201,12 @@ class ListShortUrlsTest extends ApiTestCase
|
||||
self::SHORT_URL_SHLINK_WITH_TITLE,
|
||||
], 'valid_api_key'];
|
||||
yield [['orderBy' => 'title-DESC'], [
|
||||
self::SHORT_URL_SHLINK_WITH_TITLE,
|
||||
self::SHORT_URL_META,
|
||||
self::SHORT_URL_CUSTOM_SLUG,
|
||||
self::SHORT_URL_DOCS,
|
||||
self::SHORT_URL_CUSTOM_DOMAIN,
|
||||
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
|
||||
self::SHORT_URL_SHLINK_WITH_TITLE,
|
||||
], 'valid_api_key'];
|
||||
yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [
|
||||
self::SHORT_URL_CUSTOM_DOMAIN,
|
||||
|
||||
Reference in New Issue
Block a user