donations/totals report fails on Postgres database

Created on 10 March 2024, 4 months ago
Updated 11 March 2024, 4 months ago

Problem/Motivation

With Drupal 10.2.4 set up running against a Postgres database, loading the totals report results in a generic Drupal error.

Steps to reproduce

Simply load the /admin/reports/donation/totals endpoint. The generic error page will come up. Logs show the following:
Drupal\Core\Database\DatabaseExceptionWrapper: Exception in Donations[give_donations]: SQLSTATE[42883]: Undefined function: 7 ERROR: function from_unixtime(integer) does not exist LINE 1: SELECT TO_CHAR(FROM_UNIXTIME(give_donation.created), 'YYYY')... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.: SELECT TO_CHAR(FROM_UNIXTIME(give_donation.created), 'YYYY') AS "give_donation_created", COUNT(give_donation.id) AS "id", SUM(give_donation.amount) AS "give_donation_amount", MIN(give_donation.id) AS "id_1", MIN(users_field_data_give_donation.uid) AS "users_field_data_give_donation_uid" FROM "give_donation" "give_donation" INNER JOIN "users_field_data" "users_field_data_give_donation" ON give_donation.uid = users_field_data_give_donation.uid GROUP BY "give_donation_created" ORDER BY "give_donation_created" DESC NULLS LAST LIMIT 26 OFFSET 0; Array ( ) in main() (line 19 of /var/www/html/drupal/web/index.php).

Proposed resolution

Replace line 100 of src/Plugin/views/field/ViewsDateFormatSqlField.php:
$formula = $this->query->getDateFormat("FROM_UNIXTIME($this->tableAlias.$this->realField)", $this->format_string);

with:

    $db_driver = \Drupal\Core\Database\Database::getConnection()->driver();

    if ($db_driver == "pgsql") {
      $formula = $this->query->getDateFormat("DATE(TO_TIMESTAMP($this->tableAlias.$this->realField))", $this->format_string);
    } else {
      $formula = $this->query->getDateFormat("FROM_UNIXTIME($this->tableAlias.$this->realField)", $this->options['format_string']);
    }

This solution was found in a similar issue on another plugin here 🐛 PostgreSQL SQL Error: Undefined function FROM_UNIXTIME Active .

Remaining tasks

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024