From 9b9d9c7bf9437f498ac4e925de0594920cf4baa9 Mon Sep 17 00:00:00 2001 From: Roberto Miranda Date: Fri, 25 Jun 2021 17:28:36 +0100 Subject: [PATCH 1/3] Test default precision against 6.1 --- activerecord/test/cases/migration/compatibility_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index 2d538e8944505..95b0486d89170 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 @@ -376,7 +376,7 @@ def migrate(x) end def test_datetime_doesnt_set_precision_on_add_column - migration = Class.new(ActiveRecord::Migration[4.2]) { + migration = Class.new(ActiveRecord::Migration[6.1]) { def migrate(x) add_column :testings, :published_at, :datetime, default: Time.now end From 9e3320ad26d5537e3f96b1e6622a26392391ac73 Mon Sep 17 00:00:00 2001 From: Roberto Miranda Date: Fri, 25 Jun 2021 17:44:33 +0100 Subject: [PATCH 2/3] Fix migrations compatibility for default precision value on datetime columns --- .../active_record/migration/compatibility.rb | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) 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 From 365acb675d5c3589398c35bfbabbaf3776b77cc1 Mon Sep 17 00:00:00 2001 From: Roberto Miranda Date: Fri, 25 Jun 2021 17:44:55 +0100 Subject: [PATCH 3/3] Add test case for add_column on rails 5.0 migrations --- .../test/cases/migration/compatibility_test.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index 95b0486d89170..983feaf6fd2eb 100644 --- a/activerecord/test/cases/migration/compatibility_test.rb +++ b/activerecord/test/cases/migration/compatibility_test.rb @@ -375,7 +375,19 @@ def migrate(x) connection.drop_table :more_testings rescue nil end - def test_datetime_doesnt_set_precision_on_add_column + 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