You are here

public function Tasks::checkBinaryOutput in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php \Drupal\Core\Database\Driver\pgsql\Install\Tasks::checkBinaryOutput()

Check Binary Output.

Unserializing does not work on Postgresql 9 when bytea_output is 'hex'.

File

core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php, line 135

Class

Tasks
Specifies installation tasks for PostgreSQL databases.

Namespace

Drupal\Core\Database\Driver\pgsql\Install

Code

public function checkBinaryOutput() {

  // PostgreSQL < 9 doesn't support bytea_output, so verify we are running
  // at least PostgreSQL 9.
  $database_connection = Database::getConnection();
  if (version_compare($database_connection
    ->version(), '9') >= 0) {
    if (!$this
      ->checkBinaryOutputSuccess()) {

      // First try to alter the database. If it fails, raise an error telling
      // the user to do it themselves.
      $connection_options = $database_connection
        ->getConnectionOptions();

      // It is safe to include the database name directly here, because this
      // code is only called when a connection to the database is already
      // established, thus the database name is guaranteed to be a correct
      // value.
      $query = "ALTER DATABASE \"" . $connection_options['database'] . "\" SET bytea_output = 'escape';";
      try {
        $database_connection
          ->query($query);
      } catch (\Exception $e) {

        // Ignore possible errors when the user doesn't have the necessary
        // privileges to ALTER the database.
      }

      // Close the database connection so that the configuration parameter
      // is applied to the current connection.
      Database::closeConnection();

      // Recheck, if it fails, finally just rely on the end user to do the
      // right thing.
      if (!$this
        ->checkBinaryOutputSuccess()) {
        $replacements = [
          '%setting' => 'bytea_output',
          '%current_value' => 'hex',
          '%needed_value' => 'escape',
          '@query' => $query,
        ];
        $this
          ->fail(t("The %setting setting is currently set to '%current_value', but needs to be '%needed_value'. Change this by running the following query: <code>@query</code>", $replacements));
      }
    }
  }
}