function DatabaseTablePrefixTestCase::testSchemaDotTablePrefixes

File

modules/simpletest/tests/database_test.test, line 4644

Class

DatabaseTablePrefixTestCase
Test table prefix handling.

Code

public function testSchemaDotTablePrefixes() {
    // Get a copy of the default connection options.
    $db = Database::getConnection('default', 'default');
    $connection_options = $db->getConnectionOptions();
    if ($connection_options['driver'] === 'sqlite') {
        // In SQLite simpletest's prefixed db tables exist in their own schema
        // (e.g. simpletest124904.system), so we cannot test the schema.table
        // prefix syntax here.
        $this->assert(TRUE, 'Skipping schema.table prefixed tables test for SQLite.');
        return;
    }
    $db_name = $connection_options['database'];
    if ($connection_options['driver'] === 'pgsql') {
        // database.scheme.table for PostgreSQL
        // @see https://www.postgresql.org/docs/14/ddl-schemas.html
        $db_name .= '.public';
    }
    // This prefix is usually something like simpletest12345
    $test_prefix = $connection_options['prefix']['default'];
    // Set up a new connection with table prefixes in the form "schema.table"
    $prefixed = $connection_options;
    $prefixed['prefix'] = array(
        'default' => $test_prefix,
        'users' => $db_name . '.' . $test_prefix,
        'role' => $db_name . '.' . $test_prefix,
    );
    Database::addConnectionInfo('default', 'prefixed', $prefixed);
    // Test that the prefixed database connection can query the prefixed tables.
    $num_users_prefixed = Database::getConnection('prefixed', 'default')->query('SELECT COUNT(1) FROM {users}')
        ->fetchField();
    $this->assertTrue((int) $num_users_prefixed > 0, 'Successfully queried the users table using a schema.table prefix');
    $num_users_default = Database::getConnection('default', 'default')->query('SELECT COUNT(1) FROM {users}')
        ->fetchField();
    $this->assertEqual($num_users_default, $num_users_prefixed, 'Verified results of query using a connection with schema.table prefixed tables');
}

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