diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb index da7ac8e984b64..b3d5b3ed82a72 100644 --- a/activerecord/lib/active_record/migration/compatibility.rb +++ b/activerecord/lib/active_record/migration/compatibility.rb @@ -48,6 +48,10 @@ def self.compatible_timestamp_type(type, connection) end def add_column(table_name, column_name, type, **options) + if type == :datetime + options[:precision] ||= nil + end + type = PostgreSQLCompat.compatible_timestamp_type(type, connection) super end @@ -65,6 +69,11 @@ def new_column_definition(name, type, **options) type = PostgreSQLCompat.compatible_timestamp_type(type, @conn) super end + + def column(name, type, index: nil, **options) + options[:precision] ||= nil + super + end end private @@ -265,6 +274,8 @@ def add_column(table_name, column_name, type, **options) if type == :primary_key type = :integer options[:primary_key] = true + elsif type == :datetime + options[:precision] ||= nil end super end @@ -295,11 +306,6 @@ def timestamps(**options) options[:null] = true if options[:null].nil? super end - - def column(name, type, index: nil, **options) - options[:precision] ||= nil - super - end end def add_reference(table_name, ref_name, **options) @@ -329,14 +335,6 @@ def remove_index(table_name, column_name = nil, **options) super end - def add_column(table_name, column_name, type, **options) - if type == :datetime - options[:precision] ||= nil - end - - super - end - private def compatible_table_definition(t) class << t diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index 2d538e8944505..983feaf6fd2eb 100644 --- a/activerecord/test/cases/migration/compatibility_test.rb +++ b/activerecord/test/cases/migration/compatibility_test.rb @@ -336,7 +336,7 @@ def migrate(x) end def test_datetime_doesnt_set_precision_on_create_table - migration = Class.new(ActiveRecord::Migration[4.2]) { + migration = Class.new(ActiveRecord::Migration[6.1]) { def migrate(x) create_table :more_testings do |t| t.datetime :published_at @@ -352,7 +352,7 @@ def migrate(x) end def test_datetime_doesnt_set_precision_on_change_table - create_migration = Class.new(ActiveRecord::Migration[4.2]) { + create_migration = Class.new(ActiveRecord::Migration[6.1]) { def migrate(x) create_table :more_testings do |t| t.datetime :published_at @@ -360,7 +360,7 @@ def migrate(x) end }.new - change_migration = Class.new(ActiveRecord::Migration[4.2]) { + change_migration = Class.new(ActiveRecord::Migration[6.1]) { def migrate(x) change_table :more_testings do |t| t.datetime :published_at, default: Time.now @@ -375,8 +375,20 @@ def migrate(x) connection.drop_table :more_testings rescue nil end - def test_datetime_doesnt_set_precision_on_add_column - migration = Class.new(ActiveRecord::Migration[4.2]) { + def test_datetime_doesnt_set_precision_on_add_column_5_0 + migration = Class.new(ActiveRecord::Migration[5.0]) { + def migrate(x) + add_column :testings, :published_at, :datetime, default: Time.now + end + }.new + + ActiveRecord::Migrator.new(:up, [migration], @schema_migration).migrate + + assert connection.column_exists?(:testings, :published_at, **precision_implicit_default) + end + + def test_datetime_doesnt_set_precision_on_add_column_6_1 + migration = Class.new(ActiveRecord::Migration[6.1]) { def migrate(x) add_column :testings, :published_at, :datetime, default: Time.now end