-
Notifications
You must be signed in to change notification settings - Fork 58
Default SQLite connections to WAL #405
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
Open
JanJakes
wants to merge
4
commits into
trunk
Choose a base branch
from
wal
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
054503a
Default SQLite connections to WAL
JanJakes 5b4c913
Fall back to the current journal mode when WAL is unavailable
JanJakes 9bfb3ac
Accept integer synchronous setting values
JanJakes 1898fe0
Pass journal mode and synchronous options through the PDO API
JanJakes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
packages/mysql-on-sqlite/tests/WP_SQLite_Connection_Tests.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| <?php | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Tests for the SQLite connection setup. | ||
| */ | ||
| class WP_SQLite_Connection_Tests extends TestCase { | ||
| /** | ||
| * Path to the temporary directory holding the SQLite database file. | ||
| * | ||
| * @var string|null | ||
| */ | ||
| private $db_dir; | ||
|
|
||
| /** | ||
| * Path to the temporary SQLite database file used in file-based tests. | ||
| * | ||
| * @var string|null | ||
| */ | ||
| private $db_path; | ||
|
|
||
| public function setUp(): void { | ||
| $this->db_dir = tempnam( sys_get_temp_dir(), 'wp_sqlite_' ); | ||
| unlink( $this->db_dir ); | ||
| mkdir( $this->db_dir ); | ||
| $this->db_path = $this->db_dir . '/database.sqlite'; | ||
| } | ||
|
|
||
| public function tearDown(): void { | ||
| chmod( $this->db_dir, 0755 ); // Restore permissions changed by read-only tests. | ||
| foreach ( array( | ||
| $this->db_path, | ||
| $this->db_path . '-wal', | ||
| $this->db_path . '-shm', | ||
| $this->db_path . '-journal', | ||
| ) as $path ) { | ||
| if ( is_string( $path ) && file_exists( $path ) ) { | ||
| unlink( $path ); | ||
| } | ||
| } | ||
| rmdir( $this->db_dir ); | ||
| $this->db_dir = null; | ||
| $this->db_path = null; | ||
| } | ||
|
|
||
| public function testDefaultJournalModeUsesWal(): void { | ||
| $connection = new WP_SQLite_Connection( array( 'path' => $this->db_path ) ); | ||
|
|
||
| $this->assertSame( 'wal', $this->get_journal_mode( $connection ) ); | ||
| $this->assertSame( '1', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testJournalModeCanBeOverridden(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'DELETE', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 'delete', $this->get_journal_mode( $connection ) ); | ||
| } | ||
|
|
||
| public function testSynchronousCanBeOverridden(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'synchronous' => 'FULL', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( '2', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testRollbackJournalModeKeepsDefaultSynchronous(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'DELETE', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( '2', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testInMemoryDatabaseKeepsDefaultSynchronous(): void { | ||
| $connection = new WP_SQLite_Connection( array( 'path' => ':memory:' ) ); | ||
|
|
||
| $this->assertSame( 'memory', $this->get_journal_mode( $connection ) ); | ||
| $this->assertSame( '2', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testJournalModeAndSynchronousAreCaseInsensitive(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'delete', | ||
| 'synchronous' => 'extra', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 'delete', $this->get_journal_mode( $connection ) ); | ||
| $this->assertSame( '3', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testInvalidJournalModeAndSynchronousAreIgnored(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'INVALID', | ||
| 'synchronous' => 'INVALID', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 'delete', $this->get_journal_mode( $connection ) ); | ||
| $this->assertSame( '2', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testSynchronousAcceptsIntegerValues(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'synchronous' => 3, | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( '3', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testSynchronousAcceptsIntegerZero(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'DELETE', | ||
| 'synchronous' => 0, | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( '0', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testDefaultJournalModeFallsBackWhenWalIsUnavailable(): void { | ||
| $this->make_database_directory_read_only(); | ||
|
|
||
| $connection = new WP_SQLite_Connection( array( 'path' => $this->db_path ) ); | ||
|
|
||
| $this->assertSame( 'delete', $this->get_journal_mode( $connection ) ); | ||
| $this->assertSame( '2', $this->get_synchronous( $connection ) ); | ||
| } | ||
|
|
||
| public function testExplicitJournalModeSurfacesFailureWhenWalIsUnavailable(): void { | ||
| $this->make_database_directory_read_only(); | ||
|
|
||
| $this->expectException( PDOException::class ); | ||
| new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'WAL', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Create the database file first, and then make its directory read-only, | ||
| * so that the WAL sidecar files ("-wal", "-shm") cannot be created. | ||
| */ | ||
| private function make_database_directory_read_only(): void { | ||
| $connection = new WP_SQLite_Connection( | ||
| array( | ||
| 'path' => $this->db_path, | ||
| 'journal_mode' => 'DELETE', | ||
| ) | ||
| ); | ||
| $connection->query( 'CREATE TABLE t ( id INTEGER )' ); | ||
| $connection = null; | ||
|
|
||
| chmod( $this->db_dir, 0555 ); | ||
| if ( is_writable( $this->db_dir ) ) { | ||
| $this->markTestSkipped( 'The test requires a non-writable database directory.' ); | ||
| } | ||
| } | ||
|
|
||
| private function get_journal_mode( WP_SQLite_Connection $connection ): string { | ||
| return strtolower( (string) $connection->query( 'PRAGMA journal_mode' )->fetchColumn() ); | ||
| } | ||
|
|
||
| private function get_synchronous( WP_SQLite_Connection $connection ): string { | ||
| return (string) $connection->query( 'PRAGMA synchronous' )->fetchColumn(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is super confusing. It's SYNCHRONOUS but it's NORMAL. Let's just stick to SQLite terminology and maybe even let go of constants:
Or even go super simple:
The comment above explains what we're doing, I assume it's because the logic is so opaque here. I'd love to, instead, understand why we're doing this. Document the NORMAL mode inline and explain why we're defaulting to it when WAL is enabled and not otherwise and what is SQLite default value?