Skip to content

Commit

Permalink
Merge branch 'hotfix/0.27.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
lizconlan committed Feb 13, 2017
2 parents c3a5d68 + f806fe0 commit 1bdfeee
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config/initializers/alaveteli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
load "util.rb"

# Application version
ALAVETELI_VERSION = '0.27.0.3'
ALAVETELI_VERSION = '0.27.0.4'

# Add new inflection rules using the following format
# (all these examples are active by default):
Expand Down
13 changes: 13 additions & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.27.0.4

## Highlighted Features

* Fix a bug that meant a Postgres collation that was not compatible with the
local database encoding could be chosen (Liz Conlan)

# 0.27.0.3

## Highlighted Features
Expand Down Expand Up @@ -97,6 +104,12 @@
app/views/user/rate_limited.html.erb
app/views/user/show.html.erb
app/views/widgets/show.html.erb
# 0.26.0.9

## Highlighted Features

* Fix a bug that meant a Postgres collation that was not compatible with the
local database encoding could be chosen (Liz Conlan)

# 0.26.0.8

Expand Down
20 changes: 17 additions & 3 deletions lib/database_collation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,23 @@ def postgresql_version
end

def supported_collations
@supported_collations ||= connection.
execute(%q(SELECT collname FROM pg_collation;)).
map { |row| row['collname'] }
sql = <<-EOF.strip_heredoc.squish
SELECT collname FROM pg_collation
WHERE collencoding = '-1'
OR collencoding = '#{ database_encoding }';
EOF

@supported_collations ||=
connection.execute(sql).map { |row| row['collname'] }
end

def database_encoding
sql = <<-EOF.strip_heredoc.squish
SELECT encoding FROM pg_database
WHERE datname = '#{ connection.current_database }';
EOF

@database_encoding ||= connection.execute(sql).first["encoding"]
end

def adapter_name
Expand Down
43 changes: 34 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,33 @@ 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" }])

# Simulate SQL filtering of returned collations
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, %w(-1 8)))

connection
end

def filter_collations(collations, encodings)
collations.
select { |collation| encodings.include?(collation["collencoding"]) }
end

0 comments on commit 1bdfeee

Please sign in to comment.