Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix migration compatibility for default precision value on datetime columns (Round 2) #42631

Merged
merged 1 commit into from Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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