Merge pull request #46140 from ahoglund/ahoglund/nil-precision-option

Check for Existing but nil `:precision` Option
This commit is contained in:
Rafael Mendonça França 2022-09-30 16:12:45 -04:00 committed by GitHub
commit 910af8f3c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -514,6 +514,10 @@ module ActiveRecord
def timestamps(**options)
options[:null] = false if options[:null].nil?
if !options.key?(:precision) && @conn.supports_datetime_with_precision?
options[:precision] = 6
end
column(:created_at, :datetime, **options)
column(:updated_at, :datetime, **options)
end

View File

@ -274,6 +274,23 @@ module ActiveRecord
end
end
def test_timestamps_sets_default_precision_on_create_table
migration = Class.new(ActiveRecord::Migration[6.1]) {
def migrate(x)
create_table :more_testings do |t|
t.timestamps
end
end
}.new
ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
assert connection.column_exists?(:more_testings, :created_at, **{ precision: 6 })
assert connection.column_exists?(:more_testings, :updated_at, **{ precision: 6 })
ensure
connection.drop_table :more_testings rescue nil
end
def test_datetime_doesnt_set_precision_on_create_table
migration = Class.new(ActiveRecord::Migration[6.1]) {
def migrate(x)