Skip to content

Commit de737ea

Browse files
Merge pull request rails#42606 from robertomiranda/r/test-compatibility
Fix migration compatibility for default precision value on datetime columns
2 parents 6f1ff6f + 365acb6 commit de737ea

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def self.compatible_timestamp_type(type, connection)
4848
end
4949

5050
def add_column(table_name, column_name, type, **options)
51+
if type == :datetime
52+
options[:precision] ||= nil
53+
end
54+
5155
type = PostgreSQLCompat.compatible_timestamp_type(type, connection)
5256
super
5357
end
@@ -65,6 +69,11 @@ def new_column_definition(name, type, **options)
6569
type = PostgreSQLCompat.compatible_timestamp_type(type, @conn)
6670
super
6771
end
72+
73+
def column(name, type, index: nil, **options)
74+
options[:precision] ||= nil
75+
super
76+
end
6877
end
6978

7079
private
@@ -265,6 +274,8 @@ def add_column(table_name, column_name, type, **options)
265274
if type == :primary_key
266275
type = :integer
267276
options[:primary_key] = true
277+
elsif type == :datetime
278+
options[:precision] ||= nil
268279
end
269280
super
270281
end
@@ -295,11 +306,6 @@ def timestamps(**options)
295306
options[:null] = true if options[:null].nil?
296307
super
297308
end
298-
299-
def column(name, type, index: nil, **options)
300-
options[:precision] ||= nil
301-
super
302-
end
303309
end
304310

305311
def add_reference(table_name, ref_name, **options)
@@ -329,14 +335,6 @@ def remove_index(table_name, column_name = nil, **options)
329335
super
330336
end
331337

332-
def add_column(table_name, column_name, type, **options)
333-
if type == :datetime
334-
options[:precision] ||= nil
335-
end
336-
337-
super
338-
end
339-
340338
private
341339
def compatible_table_definition(t)
342340
class << t

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def migrate(x)
336336
end
337337

338338
def test_datetime_doesnt_set_precision_on_create_table
339-
migration = Class.new(ActiveRecord::Migration[4.2]) {
339+
migration = Class.new(ActiveRecord::Migration[6.1]) {
340340
def migrate(x)
341341
create_table :more_testings do |t|
342342
t.datetime :published_at
@@ -352,15 +352,15 @@ def migrate(x)
352352
end
353353

354354
def test_datetime_doesnt_set_precision_on_change_table
355-
create_migration = Class.new(ActiveRecord::Migration[4.2]) {
355+
create_migration = Class.new(ActiveRecord::Migration[6.1]) {
356356
def migrate(x)
357357
create_table :more_testings do |t|
358358
t.datetime :published_at
359359
end
360360
end
361361
}.new
362362

363-
change_migration = Class.new(ActiveRecord::Migration[4.2]) {
363+
change_migration = Class.new(ActiveRecord::Migration[6.1]) {
364364
def migrate(x)
365365
change_table :more_testings do |t|
366366
t.datetime :published_at, default: Time.now
@@ -375,8 +375,20 @@ def migrate(x)
375375
connection.drop_table :more_testings rescue nil
376376
end
377377

378-
def test_datetime_doesnt_set_precision_on_add_column
379-
migration = Class.new(ActiveRecord::Migration[4.2]) {
378+
def test_datetime_doesnt_set_precision_on_add_column_5_0
379+
migration = Class.new(ActiveRecord::Migration[5.0]) {
380+
def migrate(x)
381+
add_column :testings, :published_at, :datetime, default: Time.now
382+
end
383+
}.new
384+
385+
ActiveRecord::Migrator.new(:up, [migration], @schema_migration).migrate
386+
387+
assert connection.column_exists?(:testings, :published_at, **precision_implicit_default)
388+
end
389+
390+
def test_datetime_doesnt_set_precision_on_add_column_6_1
391+
migration = Class.new(ActiveRecord::Migration[6.1]) {
380392
def migrate(x)
381393
add_column :testings, :published_at, :datetime, default: Time.now
382394
end

0 commit comments

Comments
 (0)