From ea64815caf8318dfad8ce7c589dc87213c0b330c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A9=20Dupuis?= Date: Wed, 3 Apr 2024 16:14:54 -0700 Subject: [PATCH] Add a Date decoder to the pg adapter to type cast dates at the connection level Fix #51448 Type cast columns of type `date` to ruby `Date` when running a raw query through `ActiveRecord::Base.connection.select_all`. --- activerecord/CHANGELOG.md | 11 +++++++++++ .../connection_adapters/postgresql_adapter.rb | 1 + 2 files changed, 12 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9c946097f2614..6767b4354ae8f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,14 @@ +* `PostgreSQLAdapter` now decodes columns of type date to `Date` instead of string. + + Ex: + ```ruby + ActiveRecord::Base.connection + .select_value("select '2024-01-01'::date").class #=> Date + ``` + + *JoƩ Dupuis* + + * `ActiveRecord::Base.transaction` now yields an `ActiveRecord::Transation` object. This allows to register callbacks on it. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index f1962ff494095..114324d38da5f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -1159,6 +1159,7 @@ def add_pg_decoders "bool" => PG::TextDecoder::Boolean, "timestamp" => PG::TextDecoder::TimestampUtc, "timestamptz" => PG::TextDecoder::TimestampWithTimeZone, + "date" => PG::TextDecoder::Date, } known_coder_types = coders_by_name.keys.map { |n| quote(n) }