Skip to content

Commit

Permalink
Merge pull request #46140 from ahoglund/ahoglund/nil-precision-option
Browse files Browse the repository at this point in the history
Check for Existing but nil `:precision` Option
  • Loading branch information
rafaelfranca committed Sep 30, 2022
2 parents a4eeb2b + a7703ce commit 910af8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ def check_constraint(expression, **options)
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
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/migration/compatibility_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ def migrate(x)
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)
Expand Down

0 comments on commit 910af8f

Please sign in to comment.