Skip to content

Commit e98a6bd

Browse files
authored
Merge pull request rails#48969 from skipkayhil/hm-fix-change-column-precision
Fix 6.1 change_column setting datetime precision
2 parents 36ebb6b + c2f838e commit e98a6bd

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix change_column setting datetime precision for 6.1 Migrations
2+
3+
*Hartley McGuire*
4+
15
* Add `ActiveRecord::Base#id_value` alias to access the raw value of a record's id column.
26

37
This alias is only provided for models that declare an `:id` column.

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ def add_column(table_name, column_name, type, **options)
178178
super
179179
end
180180

181+
def change_column(table_name, column_name, type, **options)
182+
if type == :datetime
183+
options[:precision] ||= nil
184+
end
185+
186+
type = PostgreSQLCompat.compatible_timestamp_type(type, connection)
187+
super
188+
end
189+
181190
def create_table(table_name, **options)
182191
if block_given?
183192
super { |t| yield compatible_table_definition(t) }

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,28 @@ def migrate(x)
520520
assert connection.column_exists?(:testings, :published_at, **precision_implicit_default)
521521
end
522522

523+
def test_datetime_doesnt_set_precision_on_change_column_6_1
524+
create_migration = Class.new(ActiveRecord::Migration[6.1]) {
525+
def migrate(x)
526+
create_table :more_testings do |t|
527+
t.date :published_at
528+
end
529+
end
530+
}.new(nil, 0)
531+
532+
change_migration = Class.new(ActiveRecord::Migration[6.1]) {
533+
def migrate(x)
534+
change_column :more_testings, :published_at, :datetime
535+
end
536+
}.new(nil, 1)
537+
538+
ActiveRecord::Migrator.new(:up, [create_migration, change_migration], @schema_migration, @internal_metadata).migrate
539+
540+
assert connection.column_exists?(:more_testings, :published_at, **precision_implicit_default)
541+
ensure
542+
connection.drop_table :more_testings rescue nil
543+
end
544+
523545
def test_change_table_allows_if_exists_option_on_7_0
524546
migration = Class.new(ActiveRecord::Migration[7.0]) {
525547
def migrate(x)

0 commit comments

Comments
 (0)