Commit b11c218a authored by Harish Ved's avatar Harish Ved Committed by Rémy Coutable

Fix: Use case in-sensitive ordering by name for groups

parent 47555ef8
......@@ -12,8 +12,8 @@ module Sortable
scope :order_created_asc, -> { reorder(created_at: :asc) }
scope :order_updated_desc, -> { reorder(updated_at: :desc) }
scope :order_updated_asc, -> { reorder(updated_at: :asc) }
scope :order_name_asc, -> { reorder(name: :asc) }
scope :order_name_desc, -> { reorder(name: :desc) }
scope :order_name_asc, -> { reorder("lower(name) asc") }
scope :order_name_desc, -> { reorder("lower(name) desc") }
end
module ClassMethods
......
---
title: "Use case in-sensitive ordering by name for dashboard"
merge_request: 18553
author: "@vedharish"
type: fixed
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class ProjectNameLowerIndex < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME = false
INDEX_NAME = 'index_projects_on_lower_name'
disable_ddl_transaction!
def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout
execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON projects (LOWER(name))"
end
def down
return unless Gitlab::Database.postgresql?
disable_statement_timeout
if supports_drop_index_concurrently?
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME}"
else
execute "DROP INDEX IF EXISTS #{INDEX_NAME}"
end
end
end
......@@ -8,6 +8,7 @@ task setup_postgresql: :environment do
require Rails.root.join('db/migrate/20170503185032_index_redirect_routes_path_for_like')
require Rails.root.join('db/migrate/20171220191323_add_index_on_namespaces_lower_name.rb')
require Rails.root.join('db/migrate/20180215181245_users_name_lower_index.rb')
require Rails.root.join('db/migrate/20180504195842_project_name_lower_index.rb')
require Rails.root.join('db/post_migrate/20180306164012_add_path_index_to_redirect_routes.rb')
NamespacesProjectsPathLowerIndexes.new.up
......@@ -18,5 +19,6 @@ task setup_postgresql: :environment do
IndexRedirectRoutesPathForLike.new.up
AddIndexOnNamespacesLowerName.new.up
UsersNameLowerIndex.new.up
ProjectNameLowerIndex.new.up
AddPathIndexToRedirectRoutes.new.up
end
require 'spec_helper'
describe Sortable do
describe '.order_by' do
let(:relation) { Group.all }
describe 'ordering by id' do
it 'ascending' do
expect(relation).to receive(:reorder).with(id: :asc)
relation.order_by('id_asc')
end
it 'descending' do
expect(relation).to receive(:reorder).with(id: :desc)
relation.order_by('id_desc')
end
end
describe 'ordering by created day' do
it 'ascending' do
expect(relation).to receive(:reorder).with(created_at: :asc)
relation.order_by('created_asc')
end
it 'descending' do
expect(relation).to receive(:reorder).with(created_at: :desc)
relation.order_by('created_desc')
end
it 'order by "date"' do
expect(relation).to receive(:reorder).with(created_at: :desc)
relation.order_by('created_date')
end
end
describe 'ordering by name' do
it 'ascending' do
expect(relation).to receive(:reorder).with("lower(name) asc")
relation.order_by('name_asc')
end
it 'descending' do
expect(relation).to receive(:reorder).with("lower(name) desc")
relation.order_by('name_desc')
end
end
describe 'ordering by Updated Time' do
it 'ascending' do
expect(relation).to receive(:reorder).with(updated_at: :asc)
relation.order_by('updated_asc')
end
it 'descending' do
expect(relation).to receive(:reorder).with(updated_at: :desc)
relation.order_by('updated_desc')
end
end
it 'does not call reorder in case of unrecognized ordering' do
expect(relation).not_to receive(:reorder)
relation.order_by('random_ordering')
end
end
describe 'sorting groups' do
def ordered_group_names(order)
Group.all.order_by(order).map(&:name)
end
let!(:ref_time) { Time.parse('2018-05-01 00:00:00') }
let!(:group1) { create(:group, name: 'aa', id: 1, created_at: ref_time - 15.seconds, updated_at: ref_time) }
let!(:group2) { create(:group, name: 'AAA', id: 2, created_at: ref_time - 10.seconds, updated_at: ref_time - 5.seconds) }
let!(:group3) { create(:group, name: 'BB', id: 3, created_at: ref_time - 5.seconds, updated_at: ref_time - 10.seconds) }
let!(:group4) { create(:group, name: 'bbb', id: 4, created_at: ref_time, updated_at: ref_time - 15.seconds) }
it 'sorts groups by id' do
expect(ordered_group_names('id_asc')).to eq(%w(aa AAA BB bbb))
expect(ordered_group_names('id_desc')).to eq(%w(bbb BB AAA aa))
end
it 'sorts groups by name via case-insentitive comparision' do
expect(ordered_group_names('name_asc')).to eq(%w(aa AAA BB bbb))
expect(ordered_group_names('name_desc')).to eq(%w(bbb BB AAA aa))
end
it 'sorts groups by created_at' do
expect(ordered_group_names('created_asc')).to eq(%w(aa AAA BB bbb))
expect(ordered_group_names('created_desc')).to eq(%w(bbb BB AAA aa))
expect(ordered_group_names('created_date')).to eq(%w(bbb BB AAA aa))
end
it 'sorts groups by updated_at' do
expect(ordered_group_names('updated_asc')).to eq(%w(bbb BB AAA aa))
expect(ordered_group_names('updated_desc')).to eq(%w(aa AAA BB bbb))
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment