Skip to content

Commit

Permalink
Fix migration compatibility for default precision value on datetime c…
Browse files Browse the repository at this point in the history
…olumns (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
  • Loading branch information
robertomiranda committed Jun 28, 2021
1 parent 5f7c5ad commit 06b026f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/migration/compatibility.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
122 changes: 121 additions & 1 deletion activerecord/test/cases/migration/compatibility_test.rb
Expand Up @@ -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|
Expand Down

0 comments on commit 06b026f

Please sign in to comment.