Skip to content

Commit a939506

Browse files
committed
Change the default null value for timestamps to false
1 parent 94c8715 commit a939506

File tree

8 files changed

+38
-81
lines changed

8 files changed

+38
-81
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Change the default `null` value for `timestamps` to `false`.
2+
3+
*Rafael Mendonça França*
4+
15
* Return an array of pools from `connection_pools`.
26

37
*Rafael Mendonça França*

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ def default_primary_key
5656
end
5757
end
5858

59-
module TimestampDefaultDeprecation # :nodoc:
60-
def emit_warning_if_null_unspecified(options)
61-
return if options.key?(:null)
62-
63-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
64-
`#timestamp` was called without specifying an option for `null`. In Rails 5,
65-
this behavior will change to `null: false`. You should manually specify
66-
`null: true` to prevent the behavior of your existing migrations from changing.
67-
MSG
68-
end
69-
end
70-
7159
class ReferenceDefinition # :nodoc:
7260
def initialize(
7361
name,
@@ -167,8 +155,6 @@ def foreign_table_name
167155
# The table definitions
168156
# The Columns are stored as a ColumnDefinition in the +columns+ attribute.
169157
class TableDefinition
170-
include TimestampDefaultDeprecation
171-
172158
# An array of ColumnDefinition objects, representing the column changes
173159
# that have been defined.
174160
attr_accessor :indexes
@@ -375,7 +361,9 @@ def foreign_key(table_name, options = {}) # :nodoc:
375361
# t.timestamps null: false
376362
def timestamps(*args)
377363
options = args.extract_options!
378-
emit_warning_if_null_unspecified(options)
364+
365+
options[:null] = false if options[:null].nil?
366+
379367
column(:created_at, :datetime, options)
380368
column(:updated_at, :datetime, options)
381369
end

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,14 +851,14 @@ def columns_for_distinct(columns, orders) #:nodoc:
851851
columns
852852
end
853853

854-
include TimestampDefaultDeprecation
855854
# Adds timestamps (+created_at+ and +updated_at+) columns to +table_name+.
856855
# Additional options (like <tt>null: false</tt>) are forwarded to #add_column.
857856
#
858857
# add_timestamps(:suppliers, null: false)
859858
#
860859
def add_timestamps(table_name, options = {})
861-
emit_warning_if_null_unspecified(options)
860+
options[:null] = false if options[:null].nil?
861+
862862
add_column table_name, :created_at, :datetime, options
863863
add_column table_name, :updated_at, :datetime, options
864864
end

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module ConnectionAdapters # :nodoc:
2525
autoload :TableDefinition
2626
autoload :Table
2727
autoload :AlterTable
28-
autoload :TimestampDefaultDeprecation
2928
end
3029

3130
autoload_at 'active_record/connection_adapters/abstract/connection_pool' do

activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def change
99
<% end -%>
1010
<% end -%>
1111
<% if options[:timestamps] %>
12-
t.timestamps null: false
12+
t.timestamps
1313
<% end -%>
1414
end
1515
<% attributes_with_index.each do |attribute| -%>

activerecord/test/cases/ar_schema_test.rb

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -93,69 +93,38 @@ def test_normalize_version
9393
assert_equal "20131219224947", ActiveRecord::SchemaMigration.normalize_migration_number("20131219224947")
9494
end
9595

96-
def test_timestamps_without_null_is_deprecated_on_create_table
97-
assert_deprecated do
98-
ActiveRecord::Schema.define do
99-
create_table :has_timestamps do |t|
100-
t.timestamps
101-
end
96+
def test_timestamps_without_null_set_null_to_false_on_create_table
97+
ActiveRecord::Schema.define do
98+
create_table :has_timestamps do |t|
99+
t.timestamps
102100
end
103101
end
102+
103+
assert !@connection.columns(:has_timestamps).find { |c| c.name == 'created_at' }.null
104+
assert !@connection.columns(:has_timestamps).find { |c| c.name == 'updated_at' }.null
104105
end
105106

106-
def test_timestamps_without_null_is_deprecated_on_change_table
107-
assert_deprecated do
108-
ActiveRecord::Schema.define do
109-
create_table :has_timestamps
107+
def test_timestamps_without_null_set_null_to_false_on_change_table
108+
ActiveRecord::Schema.define do
109+
create_table :has_timestamps
110110

111-
change_table :has_timestamps do |t|
112-
t.timestamps
113-
end
111+
change_table :has_timestamps do |t|
112+
t.timestamps default: Time.now
114113
end
115114
end
116-
end
117115

118-
def test_timestamps_without_null_is_deprecated_on_add_timestamps
119-
assert_deprecated do
120-
ActiveRecord::Schema.define do
121-
create_table :has_timestamps
122-
add_timestamps :has_timestamps
123-
end
124-
end
116+
assert !@connection.columns(:has_timestamps).find { |c| c.name == 'created_at' }.null
117+
assert !@connection.columns(:has_timestamps).find { |c| c.name == 'updated_at' }.null
125118
end
126119

127-
def test_no_deprecation_warning_from_timestamps_on_create_table
128-
assert_not_deprecated do
129-
ActiveRecord::Schema.define do
130-
create_table :has_timestamps do |t|
131-
t.timestamps null: true
132-
end
133-
134-
drop_table :has_timestamps
135-
136-
create_table :has_timestamps do |t|
137-
t.timestamps null: false
138-
end
139-
end
120+
def test_timestamps_without_null_set_null_to_false_on_add_timestamps
121+
ActiveRecord::Schema.define do
122+
create_table :has_timestamps
123+
add_timestamps :has_timestamps, default: Time.now
140124
end
141-
end
142-
143-
def test_no_deprecation_warning_from_timestamps_on_change_table
144-
assert_not_deprecated do
145-
ActiveRecord::Schema.define do
146-
create_table :has_timestamps
147-
change_table :has_timestamps do |t|
148-
t.timestamps null: true
149-
end
150125

151-
drop_table :has_timestamps
152-
153-
create_table :has_timestamps
154-
change_table :has_timestamps do |t|
155-
t.timestamps null: false, default: Time.now
156-
end
157-
end
158-
end
126+
assert !@connection.columns(:has_timestamps).find { |c| c.name == 'created_at' }.null
127+
assert !@connection.columns(:has_timestamps).find { |c| c.name == 'updated_at' }.null
159128
end
160129
end
161130
end

activerecord/test/cases/migration/change_schema_test.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,29 @@ def test_create_table_raises_when_redefining_custom_primary_key_column
195195
end
196196

197197
def test_create_table_with_timestamps_should_create_datetime_columns
198-
# FIXME: Remove the silence when we change the default `null` behavior
199-
ActiveSupport::Deprecation.silence do
200-
connection.create_table table_name do |t|
201-
t.timestamps
202-
end
198+
connection.create_table table_name do |t|
199+
t.timestamps
203200
end
204201
created_columns = connection.columns(table_name)
205202

206203
created_at_column = created_columns.detect {|c| c.name == 'created_at' }
207204
updated_at_column = created_columns.detect {|c| c.name == 'updated_at' }
208205

209-
assert created_at_column.null
210-
assert updated_at_column.null
206+
assert !created_at_column.null
207+
assert !updated_at_column.null
211208
end
212209

213210
def test_create_table_with_timestamps_should_create_datetime_columns_with_options
214211
connection.create_table table_name do |t|
215-
t.timestamps :null => false
212+
t.timestamps null: true
216213
end
217214
created_columns = connection.columns(table_name)
218215

219216
created_at_column = created_columns.detect {|c| c.name == 'created_at' }
220217
updated_at_column = created_columns.detect {|c| c.name == 'updated_at' }
221218

222-
assert !created_at_column.null
223-
assert !updated_at_column.null
219+
assert created_at_column.null
220+
assert updated_at_column.null
224221
end
225222

226223
def test_create_table_without_a_block

railties/test/generators/model_generator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test_model_with_polymorphic_belongs_to_attribute_generates_belongs_to_associ
223223

224224
def test_migration_with_timestamps
225225
run_generator
226-
assert_migration "db/migrate/create_accounts.rb", /t.timestamps null: false/
226+
assert_migration "db/migrate/create_accounts.rb", /t.timestamps/
227227
end
228228

229229
def test_migration_timestamps_are_skipped

0 commit comments

Comments
 (0)