diff --git a/lib/database_collation.rb b/lib/database_collation.rb index 5753ee56328..76745454f9e 100644 --- a/lib/database_collation.rb +++ b/lib/database_collation.rb @@ -55,7 +55,9 @@ def postgresql_version def supported_collations @supported_collations ||= connection. - execute(%q(SELECT collname FROM pg_collation;)). + execute("SELECT collname FROM pg_collation " \ + "WHERE collencoding = '-1' " \ + "OR collencoding = '#{database_encoding}';"). map { |row| row['collname'] } end diff --git a/spec/lib/database_collation_spec.rb b/spec/lib/database_collation_spec.rb index 1c194f431f9..209882aa1ba 100644 --- a/spec/lib/database_collation_spec.rb +++ b/spec/lib/database_collation_spec.rb @@ -45,11 +45,21 @@ expect(database.supports?('es')).to be false end - it 'supports collation if the collation exists' do + it 'does not support collation if the collation has a different encoding' do + database = DatabaseCollation.new(mock_connection) + expect(database.supports?('en_GB.utf8')).to be false + end + + it 'supports collation if the collation exists for current encoding' do database = DatabaseCollation.new(mock_connection) expect(database.supports?('en_GB')).to be true end + it 'supports installed collations with "-1" (universal) encoding' do + database = DatabaseCollation.new(mock_connection) + expect(database.supports?('default')).to be true + end + end end @@ -64,18 +74,32 @@ def mock_connection(connection_double_opts = {}) connection = double('ActiveRecord::FakeConnection', connection_double_opts) installed_collations = [ - { "collname" => "default" }, - { "collname" => "C" }, - { "collname" => "POSIX" }, - { "collname" => "C.UTF-8" }, - { "collname" => "en_GB" }, - { "collname" => "en_GB.utf8" } + { "collname" => "default", "collencoding" => "-1"}, + { "collname" => "C", "collencoding" => "-1" }, + { "collname" => "POSIX", "collencoding" => "-1" }, + { "collname" => "C.UTF-8", "collencoding" => "6" }, + { "collname" => "en_GB", "collencoding" => "8" }, + { "collname" => "en_GB.utf8", "collencoding" => "6" } ] + allow(connection).to receive(:current_database).and_return("alaveteli_test") + + allow(connection). + to receive(:execute). + with("SELECT encoding FROM pg_database WHERE datname = " \ + "'alaveteli_test';"). + and_return([{ "encoding" => "8" }]) + allow(connection). to receive(:execute). - with(%q(SELECT collname FROM pg_collation;)). - and_return(installed_collations) + with("SELECT collname FROM pg_collation " \ + "WHERE collencoding = '-1' OR collencoding = '8';"). + and_return(filter_collations(installed_collations, '8')) connection end + +def filter_collations(collations, encoding) + collations. + select { |x| x["collencoding"] == encoding || x["collencoding"] == "-1" } +end