3 namespace Drupal\Core\Database\Driver\sqlite\Install;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Driver\sqlite\Connection;
7 use Drupal\Core\Database\DatabaseNotFoundException;
8 use Drupal\Core\Database\Install\Tasks as InstallTasks;
11 * Specifies installation tasks for SQLite databases.
13 class Tasks extends InstallTasks {
18 protected $pdoDriver = 'sqlite';
23 public function name() {
30 public function minimumVersion() {
37 public function getFormOptions(array $database) {
38 $form = parent::getFormOptions($database);
40 // Remove the options that only apply to client/server style databases.
41 unset($form['username'], $form['password'], $form['advanced_options']['host'], $form['advanced_options']['port']);
43 // Make the text more accurate for SQLite.
44 $form['database']['#title'] = t('Database file');
45 $form['database']['#description'] = t('The absolute path to the file where @drupal data will be stored. This must be writable by the web server and should exist outside of the web root.', ['@drupal' => drupal_install_profile_distribution_name()]);
46 $default_database = \Drupal::service('site.path') . '/files/.ht.sqlite';
47 $form['database']['#default_value'] = empty($database['database']) ? $default_database : $database['database'];
54 protected function connect() {
56 // This doesn't actually test the connection.
57 Database::setActiveConnection();
58 // Now actually do a check.
59 Database::getConnection();
60 $this->pass('Drupal can CONNECT to the database ok.');
62 catch (\Exception $e) {
63 // Attempt to create the database if it is not found.
64 if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
65 // Remove the database string from connection info.
66 $connection_info = Database::getConnectionInfo();
67 $database = $connection_info['default']['database'];
69 // We cannot use file_directory_temp() here because we haven't yet
70 // successfully connected to the database.
71 $connection_info['default']['database'] = drupal_tempnam(sys_get_temp_dir(), 'sqlite');
73 // In order to change the Database::$databaseInfo array, need to remove
74 // the active connection, then re-add it with the new info.
75 Database::removeConnection('default');
76 Database::addConnectionInfo('default', 'default', $connection_info['default']);
79 Database::getConnection()->createDatabase($database);
80 Database::closeConnection();
82 // Now, restore the database config.
83 Database::removeConnection('default');
84 $connection_info['default']['database'] = $database;
85 Database::addConnectionInfo('default', 'default', $connection_info['default']);
87 // Check the database connection.
88 Database::getConnection();
89 $this->pass('Drupal can CONNECT to the database ok.');
91 catch (DatabaseNotFoundException $e) {
92 // Still no dice; probably a permission issue. Raise the error to the
94 $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', ['%database' => $database, '%error' => $e->getMessage()]));
98 // Database connection failed for some other reason than the database
100 $this->fail(t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', ['%error' => $e->getMessage()]));