forked from mysociety/alaveteli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase_collation.rb
65 lines (55 loc) · 1.58 KB
/
database_collation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- encoding : utf-8 -*-
#
# Public: Class to check whether the current database supports collation for
# a given language. Prefer the class method .supports? rather than creating a
# new instance.
class DatabaseCollation
MINIMUM_POSTGRESQL_VERSION = 90112
attr_reader :connection
# Public: Does the connected database support collation in the given locale?
# Delegates to an instance configured with a connection from the
# ActiveRecord::Base connection pool. See DatabaseCollation#supports? for
# more documentation.
def self.supports?(locale)
ActiveRecord::Base.connection_pool.with_connection do |connection|
i = new(connection)
i.supports?(locale)
end
end
def initialize(connection)
@connection = connection
end
# Public: Does the connected database support collation in the given locale?
#
# locale - String locale name
#
# Examples
#
# database.supports? 'en_GB'
# # => true
# database.supports? 'es'
# # => false
#
# Returns a Boolean
def supports?(locale)
exist? && supported_collations.include?(locale)
end
private
def exist?
postgresql? && postgresql_version >= MINIMUM_POSTGRESQL_VERSION
end
def postgresql?
adapter_name == 'PostgreSQL'
end
def postgresql_version
@postgresql_version ||= connection.send(:postgresql_version) if postgresql?
end
def supported_collations
@supported_collations ||= connection.
execute(%q(SELECT collname FROM pg_collation;)).
map { |row| row['collname'] }
end
def adapter_name
@adapter_name ||= connection.adapter_name
end
end