Skip to content

Commit

Permalink
Make supported_collations lookup dependent on the collencoding value
Browse files Browse the repository at this point in the history
  • Loading branch information
lizconlan committed Feb 7, 2017
1 parent 2b39cf4 commit 1d8f2a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/database_collation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 33 additions & 9 deletions spec/lib/database_collation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 1d8f2a9

Please sign in to comment.