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

[12.x] Support for Attaching Databases as Schemas in SQLite #54380

Closed
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
4 changes: 4 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
'driver' => 'sqlite',
'url' => env('DB_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'schemas' => [
// 'secondary' => env('DB_SECONDARY_DATABASE', database_path('secondary.sqlite')),
// 'tertiary' => env('DB_TERTIARY_DATABASE', ':memory:'),
],
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'busy_timeout' => null,
Expand Down
125 changes: 115 additions & 10 deletions src/Illuminate/Database/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,137 @@ class SQLiteConnector extends Connector implements ConnectorInterface
*
* @param array $config
* @return \PDO
*
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
*/
public function connect(array $config)
{
$options = $this->getOptions($config);

$path = $this->parseDatabasePath($config['database']);

$connection = $this->createConnection("sqlite:{$path}", $config, $options);

$this->configureForeignKeyConstraints($connection, $config);
$this->configureBusyTimeout($connection, $config);
$this->configureJournalMode($connection, $config);
$this->configureSynchronous($connection, $config);
$this->configureAttachedDatabases($connection, $config);

return $connection;
}

/**
* Get the absolute database path.
*
* @param string $path
* @return string
*
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
*/
protected function parseDatabasePath(string $path): string
{
// 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 shall be anonymous (:memory:) or named.
if ($config['database'] === ':memory:' ||
str_contains($config['database'], '?mode=memory') ||
str_contains($config['database'], '&mode=memory')
if ($path === ':memory:' ||
str_contains($path, '?mode=memory') ||
str_contains($path, '&mode=memory')
) {
return $this->createConnection('sqlite:'.$config['database'], $config, $options);
return $path;
}

$path = realpath($config['database']);
$realPath = realpath($path);

// 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']);
if ($realPath === false) {
throw new SQLiteDatabaseDoesNotExistException($path);
}

return $realPath;
}

/**
* Enable or disable foreign key constraints if configured.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureForeignKeyConstraints($connection, array $config): void
{
if (! isset($config['foreign_key_constraints'])) {
return;
}

$foreignKeys = $config['foreign_key_constraints'] ? 1 : 0;

$connection->prepare("pragma foreign_keys = {$foreignKeys}")->execute();
}

/**
* Set the busy timeout if configured.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureBusyTimeout($connection, array $config): void
{
if (! isset($config['busy_timeout'])) {
return;
}

$connection->prepare("pragma busy_timeout = {$config['busy_timeout']}")->execute();
}

/**
* Set the journal mode if configured.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureJournalMode($connection, array $config): void
{
if (! isset($config['journal_mode'])) {
return;
}

$connection->prepare("pragma journal_mode = {$config['journal_mode']}")->execute();
}

/**
* Set the synchronous mode if configured.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureSynchronous($connection, array $config): void
{
if (! isset($config['synchronous'])) {
return;
}

return $this->createConnection("sqlite:{$path}", $config, $options);
$connection->prepare("pragma synchronous = {$config['synchronous']}")->execute();
}

/**
* Attach databases as schemas if configured.
*
* @param \PDO $connection
* @param array $config
* @return void
*/
protected function configureAttachedDatabases($connection, array $config): void
{
if (! isset($config['schemas'])) {
return;
}

foreach ($config['schemas'] as $schema => $path) {
$connection->prepare("attach database '{$this->parseDatabasePath($path)}' as '{$schema}'")->execute();
}
}
}
111 changes: 0 additions & 111 deletions src/Illuminate/Database/SQLiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@

class SQLiteConnection extends Connection
{
/**
* Create a new database connection instance.
*
* @param \PDO|\Closure $pdo
* @param string $database
* @param string $tablePrefix
* @param array $config
* @return void
*/
public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
{
parent::__construct($pdo, $database, $tablePrefix, $config);

$this->configureForeignKeyConstraints();
$this->configureBusyTimeout();
$this->configureJournalMode();
$this->configureSynchronous();
}

/**
* {@inheritdoc}
*/
Expand All @@ -39,98 +20,6 @@ public function getDriverTitle()
return 'SQLite';
}

/**
* Enable or disable foreign key constraints if configured.
*
* @return void
*/
protected function configureForeignKeyConstraints(): void
{
$enableForeignKeyConstraints = $this->getConfig('foreign_key_constraints');

if ($enableForeignKeyConstraints === null) {
return;
}

$schemaBuilder = $this->getSchemaBuilder();

try {
$enableForeignKeyConstraints
? $schemaBuilder->enableForeignKeyConstraints()
: $schemaBuilder->disableForeignKeyConstraints();
} catch (QueryException $e) {
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
throw $e;
}
}
}

/**
* Set the busy timeout if configured.
*
* @return void
*/
protected function configureBusyTimeout(): void
{
$milliseconds = $this->getConfig('busy_timeout');

if ($milliseconds === null) {
return;
}

try {
$this->getSchemaBuilder()->setBusyTimeout($milliseconds);
} catch (QueryException $e) {
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
throw $e;
}
}
}

/**
* Set the journal mode if configured.
*
* @return void
*/
protected function configureJournalMode(): void
{
$mode = $this->getConfig('journal_mode');

if ($mode === null) {
return;
}

try {
$this->getSchemaBuilder()->setJournalMode($mode);
} catch (QueryException $e) {
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
throw $e;
}
}
}

/**
* Set the synchronous mode if configured.
*
* @return void
*/
protected function configureSynchronous(): void
{
$mode = $this->getConfig('synchronous');

if ($mode === null) {
return;
}

try {
$this->getSchemaBuilder()->setSynchronous($mode);
} catch (QueryException $e) {
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
throw $e;
}
}
}

/**
* Escape a binary value for safe SQL embedding.
*
Expand Down
Loading