Skip to content

Commit 0792661

Browse files
committed
Remove deprecation warning when using :interval column is used in PostgreSQL database
1 parent 415709a commit 0792661

File tree

4 files changed

+24
-45
lines changed

4 files changed

+24
-45
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Remove deprecation warning when using `:interval` column is used in PostgreSQL database.
2+
3+
Now, interval columns will return `ActiveSupport::Duration` objects instead of strings.
4+
5+
To keep the old behavior, you can add this line to your model:
6+
7+
```ruby
8+
attribute :column, :string
9+
```
10+
11+
*Rafael Mendonça França*
12+
113
* Remove deprecated support to YAML load `ActiveRecord::Base` instance in the Rails 4.2 and 4.1 formats.
214

315
*Rafael Mendonça França*

activerecord/lib/active_record/model_schema.rb

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ def load_schema!
571571
@columns_hash.each do |name, column|
572572
type = connection.lookup_cast_type_from_column(column)
573573
type = _convert_type_from_options(type)
574-
warn_if_deprecated_type(column)
575574
define_attribute(
576575
name,
577576
type,
@@ -630,32 +629,6 @@ def _convert_type_from_options(type)
630629
type
631630
end
632631
end
633-
634-
def warn_if_deprecated_type(column)
635-
return if attributes_to_define_after_schema_loads.key?(column.name)
636-
return unless column.respond_to?(:array?)
637-
638-
if column.array?
639-
array_arguments = ", array: true"
640-
else
641-
array_arguments = ""
642-
end
643-
644-
if column.sql_type.start_with?("interval")
645-
precision_arguments = column.precision.presence && ", precision: #{column.precision}"
646-
ActiveSupport::Deprecation.warn(<<~WARNING)
647-
The behavior of the `:interval` type will be changing in Rails 7.0
648-
to return an `ActiveSupport::Duration` object. If you'd like to keep
649-
the old behavior, you can add this line to #{self.name} model:
650-
651-
attribute :#{column.name}, :string#{precision_arguments}#{array_arguments}
652-
653-
If you'd like the new behavior today, you can add this line:
654-
655-
attribute :#{column.name}, :interval#{precision_arguments}#{array_arguments}
656-
WARNING
657-
end
658-
end
659632
end
660633
end
661634
end

activerecord/test/cases/adapters/postgresql/interval_test.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ class PostgresqlIntervalTest < ActiveRecord::PostgreSQLTestCase
77
include SchemaDumpingHelper
88

99
class IntervalDataType < ActiveRecord::Base
10-
attribute :maximum_term, :interval
11-
attribute :minimum_term, :interval, precision: 3
12-
attribute :default_term, :interval
13-
attribute :all_terms, :interval, array: true
14-
attribute :legacy_term, :string
10+
attribute :legacy_term, :string
1511
end
1612

17-
class DeprecatedIntervalDataType < ActiveRecord::Base; end
18-
1913
def setup
2014
@connection = ActiveRecord::Base.connection
2115
@connection.transaction do
@@ -26,21 +20,17 @@ def setup
2620
t.interval "all_terms", array: true
2721
t.interval "legacy_term"
2822
end
29-
@connection.create_table("deprecated_interval_data_types") do |t|
30-
t.interval "duration"
31-
end
3223
end
3324
@column_max = IntervalDataType.columns_hash["maximum_term"]
3425
@column_min = IntervalDataType.columns_hash["minimum_term"]
3526
assert(@column_max.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLColumn))
3627
assert(@column_min.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLColumn))
3728
assert_nil @column_max.precision
38-
assert_equal 3, @column_min.precision
29+
assert_equal 3, @column_min.precision
3930
end
4031

4132
teardown do
4233
@connection.execute "DROP TABLE IF EXISTS interval_data_types"
43-
@connection.execute "DROP TABLE IF EXISTS deprecated_interval_data_types"
4434
end
4535

4636
def test_column
@@ -94,12 +84,6 @@ def test_average_interval_type
9484
assert_instance_of ActiveSupport::Duration, value
9585
end
9686

97-
def test_deprecated_legacy_type
98-
assert_deprecated do
99-
DeprecatedIntervalDataType.new
100-
end
101-
end
102-
10387
def test_schema_dump_with_default_value
10488
output = dump_table_schema "interval_data_types"
10589
assert_match %r{t\.interval "default_term", default: "P3Y"}, output

guides/source/7_0_release_notes.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ Please refer to the [Changelog][active-record] for detailed changes.
106106

107107
* Remove deprecated support to YAML load `ActiveRecord::Base` instance in the Rails 4.2 and 4.1 formats.
108108

109+
* Remove deprecation warning when using `:interval` column is used in PostgreSQL database.
110+
111+
Now, interval columns will return `ActiveSupport::Duration` objects instead of strings.
112+
113+
To keep the old behavior, you can add this line to your model:
114+
115+
```ruby
116+
attribute :column, :string
117+
```
118+
109119
### Deprecations
110120

111121
### Notable changes

0 commit comments

Comments
 (0)