Skip to content

Commit

Permalink
Merge pull request #42606 from robertomiranda/r/test-compatibility
Browse files Browse the repository at this point in the history
Fix migration compatibility for default precision value on datetime columns
  • Loading branch information
guilleiguaran committed Jun 26, 2021
2 parents 6f1ff6f + 365acb6 commit de737ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
24 changes: 11 additions & 13 deletions activerecord/lib/active_record/migration/compatibility.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions activerecord/test/cases/migration/compatibility_test.rb
Expand Up @@ -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
Expand All @@ -352,15 +352,15 @@ 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
end
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
Expand All @@ -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
Expand Down

0 comments on commit de737ea

Please sign in to comment.