Skip to content

Commit 06b026f

Browse files
Fix migration compatibility for default precision value on datetime columns (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
1 parent 5f7c5ad commit 06b026f

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

activerecord/lib/active_record/migration/compatibility.rb

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ def references(*args, **options)
9999
end
100100
end
101101
alias :belongs_to :references
102+
103+
def column(name, type, index: nil, **options)
104+
options[:precision] ||= nil
105+
super
106+
end
102107
end
103108

104109
def create_table(table_name, **options)
@@ -146,6 +151,11 @@ def timestamps(**options)
146151
options[:precision] ||= nil
147152
super
148153
end
154+
155+
def column(name, type, index: nil, **options)
156+
options[:precision] ||= nil
157+
super
158+
end
149159
end
150160

151161
module CommandRecorder

activerecord/test/cases/migration/compatibility_test.rb

+121-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,127 @@ def migrate(x)
351351
connection.drop_table :more_testings rescue nil
352352
end
353353

354-
def test_datetime_doesnt_set_precision_on_change_table
354+
def test_datetime_doesnt_set_precision_on_change_table_4_2
355+
create_migration = Class.new(ActiveRecord::Migration[4.2]) {
356+
def migrate(x)
357+
create_table :more_testings do |t|
358+
t.datetime :published_at
359+
end
360+
end
361+
}.new
362+
363+
change_migration = Class.new(ActiveRecord::Migration[4.2]) {
364+
def migrate(x)
365+
change_table :more_testings do |t|
366+
t.datetime :published_at, default: Time.now
367+
end
368+
end
369+
}.new
370+
371+
ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate
372+
373+
assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default)
374+
ensure
375+
connection.drop_table :more_testings rescue nil
376+
end
377+
378+
def test_datetime_doesnt_set_precision_on_change_table_5_0
379+
create_migration = Class.new(ActiveRecord::Migration[5.0]) {
380+
def migrate(x)
381+
create_table :more_testings do |t|
382+
t.datetime :published_at
383+
end
384+
end
385+
}.new
386+
387+
change_migration = Class.new(ActiveRecord::Migration[5.0]) {
388+
def migrate(x)
389+
change_table :more_testings do |t|
390+
t.datetime :published_at, default: Time.now
391+
end
392+
end
393+
}.new
394+
395+
ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate
396+
397+
assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default)
398+
ensure
399+
connection.drop_table :more_testings rescue nil
400+
end
401+
402+
def test_datetime_doesnt_set_precision_on_change_table_5_1
403+
create_migration = Class.new(ActiveRecord::Migration[5.1]) {
404+
def migrate(x)
405+
create_table :more_testings do |t|
406+
t.datetime :published_at
407+
end
408+
end
409+
}.new
410+
411+
change_migration = Class.new(ActiveRecord::Migration[5.1]) {
412+
def migrate(x)
413+
change_table :more_testings do |t|
414+
t.datetime :published_at, default: Time.now
415+
end
416+
end
417+
}.new
418+
419+
ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate
420+
421+
assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default)
422+
ensure
423+
connection.drop_table :more_testings rescue nil
424+
end
425+
426+
def test_datetime_doesnt_set_precision_on_change_table_5_2
427+
create_migration = Class.new(ActiveRecord::Migration[5.2]) {
428+
def migrate(x)
429+
create_table :more_testings do |t|
430+
t.datetime :published_at
431+
end
432+
end
433+
}.new
434+
435+
change_migration = Class.new(ActiveRecord::Migration[5.2]) {
436+
def migrate(x)
437+
change_table :more_testings do |t|
438+
t.datetime :published_at, default: Time.now
439+
end
440+
end
441+
}.new
442+
443+
ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate
444+
445+
assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default)
446+
ensure
447+
connection.drop_table :more_testings rescue nil
448+
end
449+
450+
def test_datetime_doesnt_set_precision_on_change_table_6_0
451+
create_migration = Class.new(ActiveRecord::Migration[6.0]) {
452+
def migrate(x)
453+
create_table :more_testings do |t|
454+
t.datetime :published_at
455+
end
456+
end
457+
}.new
458+
459+
change_migration = Class.new(ActiveRecord::Migration[6.0]) {
460+
def migrate(x)
461+
change_table :more_testings do |t|
462+
t.datetime :published_at, default: Time.now
463+
end
464+
end
465+
}.new
466+
467+
ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration).migrate
468+
469+
assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default)
470+
ensure
471+
connection.drop_table :more_testings rescue nil
472+
end
473+
474+
def test_datetime_doesnt_set_precision_on_change_table_6_1
355475
create_migration = Class.new(ActiveRecord::Migration[6.1]) {
356476
def migrate(x)
357477
create_table :more_testings do |t|

0 commit comments

Comments
 (0)