From 0d8ab801cc01512ef7019e17bce4928e61c18927 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 8 Feb 2017 17:43:47 +0000 Subject: [PATCH] fixup! Make supported_collations lookup dependent on the collencoding value --- lib/database_collation.rb | 13 ++++++++----- spec/lib/database_collation_spec.rb | 7 ++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/database_collation.rb b/lib/database_collation.rb index 7450edb2bb4..4b855b94f99 100644 --- a/lib/database_collation.rb +++ b/lib/database_collation.rb @@ -54,11 +54,14 @@ def postgresql_version end def supported_collations - @supported_collations ||= connection. - execute("SELECT collname FROM pg_collation " \ - "WHERE collencoding = '-1' " \ - "OR collencoding = '#{database_encoding}';"). - 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 diff --git a/spec/lib/database_collation_spec.rb b/spec/lib/database_collation_spec.rb index 209882aa1ba..3119ccad0f4 100644 --- a/spec/lib/database_collation_spec.rb +++ b/spec/lib/database_collation_spec.rb @@ -90,16 +90,17 @@ def mock_connection(connection_double_opts = {}) "'alaveteli_test';"). and_return([{ "encoding" => "8" }]) + # Simulate SQL filtering of returned collations allow(connection). to receive(:execute). with("SELECT collname FROM pg_collation " \ "WHERE collencoding = '-1' OR collencoding = '8';"). - and_return(filter_collations(installed_collations, '8')) + and_return(filter_collations(installed_collations, %w(-1 8))) connection end -def filter_collations(collations, encoding) +def filter_collations(collations, encodings) collations. - select { |x| x["collencoding"] == encoding || x["collencoding"] == "-1" } + select { |collation| encodings.include?(collation["collencoding"]) } end