Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support project relative paths to sqlite databases regardless of webroot #191

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
43 changes: 43 additions & 0 deletions src/Database/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Winter\Storm\Database\Connectors;

use Illuminate\Database\Connectors\SQLiteConnector as BaseSQLiteConnector;
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;

class SQLiteConnector extends BaseSQLiteConnector
{
/**
* Establish a database connection.
*
* @param array $config
* @return \PDO
*
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
*/
public function connect(array $config)
{
$options = $this->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);
}
}