From 752c16253ca7945e68423e91aa0e8ecbeb4fa064 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 16 Oct 2024 04:56:50 -0400 Subject: [PATCH] Support project relative paths to sqlite databases regardless of webroot (#191) --- src/Database/Connectors/ConnectionFactory.php | 17 ++++++++ src/Database/Connectors/SQLiteConnector.php | 43 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/Database/Connectors/SQLiteConnector.php diff --git a/src/Database/Connectors/ConnectionFactory.php b/src/Database/Connectors/ConnectionFactory.php index 629630037..e87718b6b 100644 --- a/src/Database/Connectors/ConnectionFactory.php +++ b/src/Database/Connectors/ConnectionFactory.php @@ -38,6 +38,23 @@ protected function createPdoResolverWithHosts(array $config) }; } + /** + * Create a connector instance based on the configuration. + * + * @param array $config + * @return \Illuminate\Database\Connectors\ConnectorInterface + * + * @throws \InvalidArgumentException + */ + public function createConnector(array $config) + { + if (array_get($config, 'driver') === 'sqlite') { + return new SQLiteConnector; + } else { + return parent::createConnector($config); + } + } + /** * Create a new connection instance. * diff --git a/src/Database/Connectors/SQLiteConnector.php b/src/Database/Connectors/SQLiteConnector.php new file mode 100644 index 000000000..d9209ca1d --- /dev/null +++ b/src/Database/Connectors/SQLiteConnector.php @@ -0,0 +1,43 @@ +getOptions($config); + + // SQLite supports "in-memory" databases that only last as long as the owning + // connection does. These are useful for tests or for short lifetime store + // querying. In-memory databases may only have a single open connection. + if ($config['database'] === ':memory:') { + return $this->createConnection('sqlite::memory:', $config, $options); + } + + $path = realpath($config['database']); + if (!file_exists($path)) { + $path = realpath(base_path($config['database'])); + } + + // Here we'll verify that the SQLite database exists before going any further + // as the developer probably wants to know if the database exists and this + // SQLite driver will not throw any exception if it does not by default. + if ($path === false) { + throw new SQLiteDatabaseDoesNotExistException($config['database']); + } + + return $this->createConnection("sqlite:{$path}", $config, $options); + } +}