Merge pull request #42606 from robertomiranda/r/test-compatibility

Fix migration compatibility for default precision value on datetime columns
This commit is contained in:
Guillermo Iguaran 2021-06-25 17:32:33 -07:00 committed by GitHub
commit de737ea267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 18 deletions

@ -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

@ -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