diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb index b3d5b3ed82a72..fc6327f823206 100644 --- a/activerecord/lib/active_record/migration/compatibility.rb +++ b/activerecord/lib/active_record/migration/compatibility.rb @@ -99,6 +99,11 @@ def references(*args, **options) end end alias :belongs_to :references + + def column(name, type, index: nil, **options) + options[:precision] ||= nil + super + end end def create_table(table_name, **options) @@ -146,6 +151,11 @@ def timestamps(**options) options[:precision] ||= nil super end + + def column(name, type, index: nil, **options) + options[:precision] ||= nil + super + end end module CommandRecorder diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index 983feaf6fd2eb..7ea05774d4638 100644 --- a/activerecord/test/cases/migration/compatibility_test.rb +++ b/activerecord/test/cases/migration/compatibility_test.rb @@ -351,7 +351,127 @@ def migrate(x) connection.drop_table :more_testings rescue nil end - def test_datetime_doesnt_set_precision_on_change_table + def test_datetime_doesnt_set_precision_on_change_table_4_2 + create_migration = Class.new(ActiveRecord::Migration[4.2]) { + def migrate(x) + create_table :more_testings do |t| + t.datetime :published_at + end + end + }.new + + change_migration = Class.new(ActiveRecord::Migration[4.2]) { + def migrate(x) + change_table :more_testings do |t| + t.datetime :published_at, default: Time.now + end + end + }.new + + ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate + + assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default) + ensure + connection.drop_table :more_testings rescue nil + end + + def test_datetime_doesnt_set_precision_on_change_table_5_0 + create_migration = Class.new(ActiveRecord::Migration[5.0]) { + def migrate(x) + create_table :more_testings do |t| + t.datetime :published_at + end + end + }.new + + change_migration = Class.new(ActiveRecord::Migration[5.0]) { + def migrate(x) + change_table :more_testings do |t| + t.datetime :published_at, default: Time.now + end + end + }.new + + ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate + + assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default) + ensure + connection.drop_table :more_testings rescue nil + end + + def test_datetime_doesnt_set_precision_on_change_table_5_1 + create_migration = Class.new(ActiveRecord::Migration[5.1]) { + def migrate(x) + create_table :more_testings do |t| + t.datetime :published_at + end + end + }.new + + change_migration = Class.new(ActiveRecord::Migration[5.1]) { + def migrate(x) + change_table :more_testings do |t| + t.datetime :published_at, default: Time.now + end + end + }.new + + ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate + + assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default) + ensure + connection.drop_table :more_testings rescue nil + end + + def test_datetime_doesnt_set_precision_on_change_table_5_2 + create_migration = Class.new(ActiveRecord::Migration[5.2]) { + def migrate(x) + create_table :more_testings do |t| + t.datetime :published_at + end + end + }.new + + change_migration = Class.new(ActiveRecord::Migration[5.2]) { + def migrate(x) + change_table :more_testings do |t| + t.datetime :published_at, default: Time.now + end + end + }.new + + ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate + + assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default) + ensure + connection.drop_table :more_testings rescue nil + end + + def test_datetime_doesnt_set_precision_on_change_table_6_0 + create_migration = Class.new(ActiveRecord::Migration[6.0]) { + def migrate(x) + create_table :more_testings do |t| + t.datetime :published_at + end + end + }.new + + change_migration = Class.new(ActiveRecord::Migration[6.0]) { + def migrate(x) + change_table :more_testings do |t| + t.datetime :published_at, default: Time.now + end + end + }.new + + ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate + + assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default) + ensure + connection.drop_table :more_testings rescue nil + end + + def test_datetime_doesnt_set_precision_on_change_table_6_1 create_migration = Class.new(ActiveRecord::Migration[6.1]) { def migrate(x) create_table :more_testings do |t|