function Connection::__destruct

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::__destruct()
Same name and namespace in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::__destruct()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::__destruct()
  3. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::__destruct()
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::__destruct()
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::__destruct()
  6. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::__destruct()
  7. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::__destruct()
  8. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::__destruct()

Destructor for the SQLite connection.

We prune empty databases on destruct, but only if tables have been dropped. This is especially needed when running the test suite, which creates and destroy databases several times in a row.

Overrides Connection::__destruct

File

core/modules/sqlite/src/Driver/Database/sqlite/Connection.php, line 166

Class

Connection
SQLite implementation of <a href="/api/drupal/core%21lib%21Drupal%21Core%21Database%21Connection.php/class/Connection/11.x" title="Base Database API class." class="local">\Drupal\Core\Database\Connection</a>.

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

public function __destruct() {
    if ($this->tableDropped && !empty($this->attachedDatabases)) {
        foreach ($this->attachedDatabases as $prefix) {
            // Check if the database is now empty, ignore the internal SQLite tables.
            try {
                $count = $this->query('SELECT COUNT(*) FROM ' . $prefix . '.sqlite_master WHERE type = :type AND name NOT LIKE :pattern', [
                    ':type' => 'table',
                    ':pattern' => 'sqlite_%',
                ])
                    ->fetchField();
                // We can prune the database file if it doesn't have any tables.
                if ($count == 0 && $this->connectionOptions['database'] != ':memory:' && file_exists($this->connectionOptions['database'] . '-' . $prefix)) {
                    // Detach the database.
                    $this->query('DETACH DATABASE :schema', [
                        ':schema' => $prefix,
                    ]);
                    // Destroy the database file.
                    unlink($this->connectionOptions['database'] . '-' . $prefix);
                }
            } catch (\Exception $e) {
                // Ignore the exception and continue. There is nothing we can do here
                // to report the error or fail safe.
            }
        }
    }
    parent::__destruct();
}

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