Skip to content

Commit

Permalink
fixup! Make supported_collations lookup dependent on the collencoding…
Browse files Browse the repository at this point in the history
… value
  • Loading branch information
garethrees committed Feb 8, 2017
1 parent 107c581 commit 0d8ab80
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 8 additions & 5 deletions lib/database_collation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions spec/lib/database_collation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0d8ab80

Please sign in to comment.