From 06b026f0244dbbb5b86472fec778c4bdf630b0ba Mon Sep 17 00:00:00 2001 From: Roberto Miranda Date: Mon, 28 Jun 2021 21:03:02 +0100 Subject: [PATCH] Fix migration compatibility for default precision value on datetime columns (Round 2) add test cases on default precision from 4.2 to 6.1 Fix compatibility on rails 6.0 Fix compatibility from 5.2 to 4.2 --- .../active_record/migration/compatibility.rb | 10 ++ .../cases/migration/compatibility_test.rb | 122 +++++++++++++++++- 2 files changed, 131 insertions(+), 1 deletion(-) 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|