Skip to content

Commit

Permalink
Merge pull request #43964 from ghiculescu/timestamp-tz-blank
Browse files Browse the repository at this point in the history
Active Record / PostgreSQL / timestamptz: handle blank inputs
  • Loading branch information
kamipo committed Dec 22, 2021
2 parents bcd76f1 + 9917bde commit 9270fae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def type
end

def cast_value(value)
return if value.blank?

time = super
return time if time.is_a?(ActiveSupport::TimeWithZone)

Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/date_time_precision_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ def test_formatting_datetime_according_to_precision_when_time_zone_aware_using_t
end
end

def test_writing_a_blank_attribute
@connection.create_table(:foos, force: true) do |t|
t.datetime :happened_at
end

assert_nil Foo.create!(happened_at: nil).happened_at
assert_nil Foo.create!(happened_at: "").happened_at
end

if current_adapter?(:PostgreSQLAdapter)
def test_writing_a_blank_attribute
with_postgresql_datetime_type(:timestamptz) do
@connection.create_table(:foos, force: true) do |t|
t.datetime :happened_at
end

assert_nil Foo.create!(happened_at: nil).happened_at
assert_nil Foo.create!(happened_at: "").happened_at
end
end
end

def test_schema_dump_includes_datetime_precision
@connection.create_table(:foos, force: true) do |t|
t.timestamps precision: 6
Expand Down

0 comments on commit 9270fae

Please sign in to comment.