aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb5
-rw-r--r--activerecord/test/cases/adapters/postgresql/datatype_test.rb41
-rw-r--r--activerecord/test/schema/postgresql_specific_schema.rb2
3 files changed, 37 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 18a5b4b1e4..209553b26e 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -575,6 +575,11 @@ module ActiveRecord
true
end
+ # Range datatypes weren't introduced until PostgreSQL 9.2
+ def supports_ranges?
+ postgresql_version >= 90200
+ end
+
# Returns the configured supported identifier length supported by PostgreSQL
def table_alias_length
@table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i
diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
index 8d7a78c513..7351ed9013 100644
--- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -46,7 +46,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
@connection.execute("INSERT INTO postgresql_arrays (id, commission_by_quarter, nicknames) VALUES (1, '{35000,21000,18000,17000}', '{foo,bar,baz}')")
@first_array = PostgresqlArray.find(1)
- @connection.execute <<_SQL
+ @connection.execute <<_SQL if @connection.supports_ranges?
INSERT INTO postgresql_ranges (
date_range,
num_range,
@@ -64,7 +64,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
)
_SQL
- @connection.execute <<_SQL
+ @connection.execute <<_SQL if @connection.supports_ranges?
INSERT INTO postgresql_ranges (
date_range,
num_range,
@@ -82,7 +82,7 @@ _SQL
)
_SQL
- @connection.execute <<_SQL
+ @connection.execute <<_SQL if @connection.supports_ranges?
INSERT INTO postgresql_ranges (
date_range,
num_range,
@@ -100,7 +100,7 @@ _SQL
)
_SQL
- @connection.execute <<_SQL
+ @connection.execute <<_SQL if @connection.supports_ranges?
INSERT INTO postgresql_ranges (
date_range,
num_range,
@@ -118,7 +118,7 @@ _SQL
)
_SQL
- @connection.execute <<_SQL
+ @connection.execute <<_SQL if @connection.supports_ranges?
INSERT INTO postgresql_ranges (
date_range,
num_range,
@@ -136,11 +136,13 @@ _SQL
)
_SQL
- @first_range = PostgresqlRange.find(1)
- @second_range = PostgresqlRange.find(2)
- @third_range = PostgresqlRange.find(3)
- @fourth_range = PostgresqlRange.find(4)
- @empty_range = PostgresqlRange.find(5)
+ if @connection.supports_ranges?
+ @first_range = PostgresqlRange.find(1)
+ @second_range = PostgresqlRange.find(2)
+ @third_range = PostgresqlRange.find(3)
+ @fourth_range = PostgresqlRange.find(4)
+ @empty_range = PostgresqlRange.find(5)
+ end
@connection.execute("INSERT INTO postgresql_tsvectors (id, text_vector) VALUES (1, ' ''text'' ''vector'' ')")
@@ -183,6 +185,7 @@ _SQL
end
def test_data_type_of_range_types
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal :daterange, @first_range.column_for_attribute(:date_range).type
assert_equal :numrange, @first_range.column_for_attribute(:num_range).type
assert_equal :tsrange, @first_range.column_for_attribute(:ts_range).type
@@ -238,6 +241,7 @@ _SQL
end
def test_int4range_values
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal 1...11, @first_range.int4_range
assert_equal 2...10, @second_range.int4_range
assert_equal 2...Float::INFINITY, @third_range.int4_range
@@ -246,6 +250,7 @@ _SQL
end
def test_int8range_values
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal 10...101, @first_range.int8_range
assert_equal 11...100, @second_range.int8_range
assert_equal 11...Float::INFINITY, @third_range.int8_range
@@ -254,6 +259,7 @@ _SQL
end
def test_daterange_values
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal Date.new(2012, 1, 2)...Date.new(2012, 1, 5), @first_range.date_range
assert_equal Date.new(2012, 1, 3)...Date.new(2012, 1, 4), @second_range.date_range
assert_equal Date.new(2012, 1, 3)...Float::INFINITY, @third_range.date_range
@@ -262,6 +268,7 @@ _SQL
end
def test_numrange_values
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal BigDecimal.new('0.1')..BigDecimal.new('0.2'), @first_range.num_range
assert_equal BigDecimal.new('0.1')...BigDecimal.new('0.2'), @second_range.num_range
assert_equal BigDecimal.new('0.1')...BigDecimal.new('Infinity'), @third_range.num_range
@@ -270,6 +277,7 @@ _SQL
end
def test_tsrange_values
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
tz = ::ActiveRecord::Base.default_timezone
assert_equal Time.send(tz, 2010, 1, 1, 14, 30, 0)..Time.send(tz, 2011, 1, 1, 14, 30, 0), @first_range.ts_range
assert_equal Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 1, 1, 14, 30, 0), @second_range.ts_range
@@ -279,6 +287,7 @@ _SQL
end
def test_tstzrange_values
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal Time.parse('2010-01-01 09:30:00 UTC')..Time.parse('2011-01-01 17:30:00 UTC'), @first_range.tstz_range
assert_equal Time.parse('2010-01-01 09:30:00 UTC')...Time.parse('2011-01-01 17:30:00 UTC'), @second_range.tstz_range
assert_equal Time.parse('2010-01-01 09:30:00 UTC')...Float::INFINITY, @third_range.tstz_range
@@ -292,6 +301,7 @@ _SQL
end
def test_create_tstzrange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
tstzrange = Time.parse('2010-01-01 14:30:00 +0100')...Time.parse('2011-02-02 14:30:00 CDT')
range = PostgresqlRange.new(:tstz_range => tstzrange)
assert range.save
@@ -301,6 +311,7 @@ _SQL
end
def test_update_tstzrange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
new_tstzrange = Time.parse('2010-01-01 14:30:00 CDT')...Time.parse('2011-02-02 14:30:00 CET')
assert @first_range.tstz_range = new_tstzrange
assert @first_range.save
@@ -313,6 +324,7 @@ _SQL
end
def test_create_tsrange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
tz = ::ActiveRecord::Base.default_timezone
tsrange = Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 2, 2, 14, 30, 0)
range = PostgresqlRange.new(:ts_range => tsrange)
@@ -322,6 +334,7 @@ _SQL
end
def test_update_tsrange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
tz = ::ActiveRecord::Base.default_timezone
new_tsrange = Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 2, 2, 14, 30, 0)
assert @first_range.ts_range = new_tsrange
@@ -335,6 +348,7 @@ _SQL
end
def test_create_numrange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
numrange = BigDecimal.new('0.5')...BigDecimal.new('1')
range = PostgresqlRange.new(:num_range => numrange)
assert range.save
@@ -343,6 +357,7 @@ _SQL
end
def test_update_numrange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
new_numrange = BigDecimal.new('0.5')...BigDecimal.new('1')
assert @first_range.num_range = new_numrange
assert @first_range.save
@@ -355,6 +370,7 @@ _SQL
end
def test_create_daterange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
daterange = Range.new(Date.new(2012, 1, 1), Date.new(2013, 1, 1), true)
range = PostgresqlRange.new(:date_range => daterange)
assert range.save
@@ -363,6 +379,7 @@ _SQL
end
def test_update_daterange
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
new_daterange = Date.new(2012, 2, 3)...Date.new(2012, 2, 10)
assert @first_range.date_range = new_daterange
assert @first_range.save
@@ -375,6 +392,7 @@ _SQL
end
def test_create_int4range
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
int4range = Range.new(3, 50, true)
range = PostgresqlRange.new(:int4_range => int4range)
assert range.save
@@ -383,6 +401,7 @@ _SQL
end
def test_update_int4range
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
new_int4range = 6...10
assert @first_range.int4_range = new_int4range
assert @first_range.save
@@ -395,6 +414,7 @@ _SQL
end
def test_create_int8range
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
int8range = Range.new(30, 50, true)
range = PostgresqlRange.new(:int8_range => int8range)
assert range.save
@@ -403,6 +423,7 @@ _SQL
end
def test_update_int8range
+ skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
new_int8range = 60000...10000000
assert @first_range.int8_range = new_int8range
assert @first_range.save
diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb
index bf6f1ca191..83b50030bd 100644
--- a/activerecord/test/schema/postgresql_specific_schema.rb
+++ b/activerecord/test/schema/postgresql_specific_schema.rb
@@ -73,7 +73,7 @@ _SQL
);
_SQL
- execute <<_SQL
+ execute <<_SQL if supports_ranges?
CREATE TABLE postgresql_ranges (
id SERIAL PRIMARY KEY,
date_range daterange,