Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Postgres collation support check #3724

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
and dashboard for managing to-do items and requests. This is functionality
being piloted in the UK and is not yet recommended for use in other locales
(Steve Day, Martin Wright, Louise Crow)
* Fix bug that meant a Postgres collation that was not compatible with the local
database encoding could be chosen (Liz Conlan)

## Upgrade Notes
* Please update any overriden templates and theme code that reference times and
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