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

feat: add SQLite3 config synchronous #9202

Open
wants to merge 2 commits into
base: 4.6
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions app/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Database extends Config
// 'failover' => [],
// 'foreignKeys' => true,
// 'busyTimeout' => 1000,
// 'synchronous' => null,
// 'dateFormat' => [
// 'date' => 'Y-m-d',
// 'datetime' => 'Y-m-d H:i:s',
Expand Down
17 changes: 17 additions & 0 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\TableName;
use Exception;
use InvalidArgumentException;
use SQLite3;
use SQLite3Result;
use stdClass;
Expand Down Expand Up @@ -56,6 +57,15 @@ class Connection extends BaseConnection
*/
protected $busyTimeout;

/**
* The setting of the "synchronous" flag
*
* @var int|null flag
*
* @see https://www.sqlite.org/pragma.html#pragma_synchronous
*/
protected $synchronous;

/**
* @return void
*/
Expand All @@ -70,6 +80,13 @@ public function initialize()
if (is_int($this->busyTimeout)) {
$this->connID->busyTimeout($this->busyTimeout);
}

if (is_int($this->synchronous)) {
Copy link
Contributor

@maniaba maniaba Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to add validation or throw a database exception in this section. Since we are directly passing $this->synchronous into the PRAGMA statement, without validating whether the value is one of the expected integers (0, 1, 2, 3), we risk unexpected behavior or incorrect PRAGMA settings if an invalid value is provided. Adding validation would ensure we are handling only valid inputs and would prevent potential silent failures.

Additionally, it might be worth considering the use of an enum to define the valid values for synchronous. This would provide a clearer and more structured way to enforce the allowed options and make the code easier to maintain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added synchronous value validation and test.

I think using an enum here would mess with simplicity a bit - but if more people say we should go this way I can add it. Although I don't think we have a single enum in the project at the moment (I might have missed something).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using an enum is necessary in this case. The reason enums weren't used in the framework was due to older PHP versions, but I believe we should use enums where possible.

In general, I'm a fan of enums as they improve code readability, but I don't see a strong need for them here.

if (! in_array($this->synchronous, [0, 1, 2, 3], true)) {
throw new InvalidArgumentException('Invalid synchronous value.');
}
$this->connID->exec('PRAGMA synchronous = ' . $this->synchronous);
}
}

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/system/Database/Live/SQLite3/ConnectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Live\SQLite3;

use CodeIgniter\Test\CIUnitTestCase;
use Config\Database;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('DatabaseLive')]
final class ConnectTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->db = Database::connect($this->DBGroup);

if ($this->db->DBDriver !== 'SQLite3') {
$this->markTestSkipped('This test is only for SQLite3.');
}
}

public function testShowErrorMessageWhenSettingInvalidSynchronous(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid synchronous value.');

$config = config('Database');
$group = $config->tests;
// Sets invalid synchronous.
$group['synchronous'] = 123;
$db = Database::connect($group);

// Actually connect to DB.
$db->initialize();
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ Others
- Added a new configuration ``foundRows`` for MySQLi to use ``MYSQLI_CLIENT_FOUND_ROWS``.
- Added the ``BaseConnection::resetTransStatus()`` method to reset the transaction
status. See :ref:`transactions-resetting-transaction-status` for details.
- SQLite3 has a new Config item ``synchronous`` to adjust how strict SQLite is at flushing
to disk during transactions. Modifying this can be useful if we use journal mode set to ``WAL``.

Model
=====
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/database/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ Description of Values
See `SQLite documentation <https://www.sqlite.org/pragma.html#pragma_foreign_keys>`_.
To enforce Foreign Key constraint, set this config item to true.
**busyTimeout** (``SQLite3`` only) milliseconds (int) - Sleeps for a specified amount of time when a table is locked.
**synchronous** (``SQLite3`` only) flag (int) - How strict SQLite will be at flushing to disk during transactions.
Use `null` to stay with the default setting. This can be used since v4.6.0.
**numberNative** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE.
**foundRows** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_CLIENT_FOUND_ROWS.
**dateFormat** The default date/time formats as PHP's `DateTime format`_.
Expand Down
Loading