function Tasks::initializeDatabase

Same name in other branches
  1. 9 core/modules/pgsql/src/Driver/Database/pgsql/Install/Tasks.php \Drupal\pgsql\Driver\Database\pgsql\Install\Tasks::initializeDatabase()
  2. 10 core/modules/pgsql/src/Driver/Database/pgsql/Install/Tasks.php \Drupal\pgsql\Driver\Database\pgsql\Install\Tasks::initializeDatabase()
  3. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Install/Tasks.php \Drupal\pgsql\Driver\Database\pgsql\Install\Tasks::initializeDatabase()

Make PostgreSQL Drupal friendly.

File

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

Class

Tasks
Specifies installation tasks for PostgreSQL databases.

Namespace

Drupal\Core\Database\Driver\pgsql\Install

Code

public function initializeDatabase() {
    // We create some functions using global names instead of prefixing them
    // like we do with table names. This is so that we don't double up if more
    // than one instance of Drupal is running on a single database. We therefore
    // avoid trying to create them again in that case.
    // At the same time checking for the existence of the function fixes
    // concurrency issues, when both try to update at the same time.
    try {
        $connection = Database::getConnection();
        // When testing, two installs might try to run the CREATE FUNCTION queries
        // at the same time. Do not let that happen.
        $connection->query('SELECT pg_advisory_lock(1)');
        // Don't use {} around pg_proc table.
        if (!$connection->query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'")
            ->fetchField()) {
            $connection->query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
          \'SELECT random();\'
          LANGUAGE \'sql\'', [], [
                'allow_delimiter_in_query' => TRUE,
            ]);
        }
        if (!$connection->query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'substring_index'")
            ->fetchField()) {
            $connection->query('CREATE OR REPLACE FUNCTION "substring_index"(text, text, integer) RETURNS text AS
          \'SELECT array_to_string((string_to_array($1, $2)) [1:$3], $2);\'
          LANGUAGE \'sql\'', [], [
                'allow_delimiter_in_query' => TRUE,
            ]);
        }
        $connection->query('SELECT pg_advisory_unlock(1)');
        $this->pass(t('PostgreSQL has initialized itself.'));
    } catch (\Exception $e) {
        $this->fail(t('Drupal could not be correctly setup with the existing database due to the following error: @error.', [
            '@error' => $e->getMessage(),
        ]));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.