Skip to content

Commit 910af8f

Browse files
authored
Merge pull request rails#46140 from ahoglund/ahoglund/nil-precision-option
Check for Existing but nil `:precision` Option
2 parents a4eeb2b + a7703ce commit 910af8f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ def check_constraint(expression, **options)
514514
def timestamps(**options)
515515
options[:null] = false if options[:null].nil?
516516

517+
if !options.key?(:precision) && @conn.supports_datetime_with_precision?
518+
options[:precision] = 6
519+
end
520+
517521
column(:created_at, :datetime, **options)
518522
column(:updated_at, :datetime, **options)
519523
end

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ def migrate(x)
274274
end
275275
end
276276

277+
def test_timestamps_sets_default_precision_on_create_table
278+
migration = Class.new(ActiveRecord::Migration[6.1]) {
279+
def migrate(x)
280+
create_table :more_testings do |t|
281+
t.timestamps
282+
end
283+
end
284+
}.new
285+
286+
ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
287+
288+
assert connection.column_exists?(:more_testings, :created_at, **{ precision: 6 })
289+
assert connection.column_exists?(:more_testings, :updated_at, **{ precision: 6 })
290+
ensure
291+
connection.drop_table :more_testings rescue nil
292+
end
293+
277294
def test_datetime_doesnt_set_precision_on_create_table
278295
migration = Class.new(ActiveRecord::Migration[6.1]) {
279296
def migrate(x)

0 commit comments

Comments
 (0)