Skip to content

Comments

Sort key fixes#923

Open
jmattheis wants to merge 3 commits intomasterfrom
sort-key-fixes
Open

Sort key fixes#923
jmattheis wants to merge 3 commits intomasterfrom
sort-key-fixes

Conversation

@jmattheis
Copy link
Member

This PR fixes the sort_key index creation reported in #920 and sorting in general, as it was broken for MySQL and Postgres. ORDER BY isn't consistently implemented by Sqlite, Postgres and Mysql. For Postgres and Mysql it's case insensitive which breaks fractional sorting.

Fixes #920

Sqlite sorting is by default case sensitive. Postgres & mysql seem to be
case insensitive without a common sql way to for case sensitive sorting.
@jmattheis jmattheis requested a review from a team as a code owner February 21, 2026 21:44
@codecov
Copy link

codecov bot commented Feb 21, 2026

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.78%. Comparing base (fcbb870) to head (ab0c7b1).

Files with missing lines Patch % Lines
database/database.go 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #923      +/-   ##
==========================================
+ Coverage   78.70%   78.78%   +0.07%     
==========================================
  Files          57       57              
  Lines        2517     2526       +9     
==========================================
+ Hits         1981     1990       +9     
  Misses        397      397              
  Partials      139      139              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@eternal-flame-AD eternal-flame-AD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Postgres and Mysql it's case insensitive which breaks fractional sorting.

That doesn't sound right, I tried on postgres and it seems to be case sensitive, am I missing something?

gotify=# select * from applications order by sort_key asc;
  3 | AbVEBAyV6LKUcDL |       1 | test3 |             | f        |       |    
            0 |           | a0V
  4 | ANE_hnibLVDWzWX |       1 | test4 |             | f        |       |    
            0 |           | a0l
  2 | A9Iy-DqWEPvrS1k |       1 | test2 |             | f        |       |    
            0 |           | a1
  1 | A-0KhQKUv33Q9GI |       1 | test1 |             | f        |       |    
            0 |           | a2

}

var apps []*model.Application
if err := db.Order("user_id, sort_key, id ASC").Find(&apps).Error; err != nil && err != gorm.ErrRecordNotFound {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not work on MySQL ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work, but given that there is a check before that the sort_key is null, it shouldn't be needed, and as the sort_key sorting can be inconsistent between database engines, I'd rather remove this.

@@ -40,10 +42,14 @@ func (d *GormDatabase) CreateApplication(application *model.Application) error {
return d.DB.Transaction(func(tx *gorm.DB) error {
if application.SortKey == "" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't sound right, I tried on postgres and it seems to be case sensitive, am I missing something?

Your example seems sorted case insensitive. Here a more explicit example:

psql (18.1, server 18.2 (Debian 18.2-1.pgdg13+1))
Type "help" for help.

gotify=# create table test (sort_key text not null); insert into test values ('a0V'), ('a0l'), ('a1'), ('Zz'), ('a0A');
CREATE TABLE
INSERT 0 5
gotify=# select * from test order by sort_key asc;
 sort_key
----------
 a0A
 a0l
 a0V
 a1
 Zz
(5 rows)

a0l is between a0A and a0V, but should actually be sorted like this:

$ python
Python 3.14.2 (main, Jan  2 2026, 14:27:39) [GCC 15.2.1 20251112] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ['a0V', 'a0l', 'a1', 'a0A', 'a2', 'Zz']
>>> a.sort()
>>> a
['Zz', 'a0A', 'a0V', 'a0l', 'a1', 'a2']

(using docker.io/postgres:18)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Error 1170 (42000): BLOB/TEXT column 'sort_key' used in key specification without a key length

2 participants