Skip to content

Commit a15ecdb

Browse files
committed
Inline journal/synchronous defaults and explain NORMAL
Drop the single-use DEFAULT_SQLITE_JOURNAL_MODE and DEFAULT_SQLITE_WAL_SYNCHRONOUS constants in favor of inline literals, and replace the synchronous comment with one that explains why we default to NORMAL under WAL: SQLite's default is FULL (an fsync per commit), while NORMAL avoids that cost and stays corruption-safe in WAL mode, a guarantee that does not hold for rollback journal modes.
1 parent 1898fe0 commit a15ecdb

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-connection.php

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ class WP_SQLite_Connection {
1919
*/
2020
const DEFAULT_SQLITE_TIMEOUT = 10;
2121

22-
/**
23-
* The default SQLite journal mode.
24-
*/
25-
const DEFAULT_SQLITE_JOURNAL_MODE = 'WAL';
26-
27-
/**
28-
* The default SQLite synchronous setting for WAL mode.
29-
*/
30-
const DEFAULT_SQLITE_WAL_SYNCHRONOUS = 'NORMAL';
31-
3222
/**
3323
* The supported SQLite journal modes.
3424
*
@@ -117,9 +107,9 @@ public function __construct( array $options ) {
117107
}
118108
$this->pdo->setAttribute( PDO::ATTR_TIMEOUT, $timeout );
119109

120-
// Configure SQLite journal mode.
110+
// Configure SQLite journal mode. Default to WAL for best throughput.
121111
$effective_journal_mode = null;
122-
$journal_mode = $options['journal_mode'] ?? self::DEFAULT_SQLITE_JOURNAL_MODE;
112+
$journal_mode = $options['journal_mode'] ?? 'WAL';
123113
if ( is_string( $journal_mode ) ) {
124114
$journal_mode = strtoupper( $journal_mode );
125115
}
@@ -138,17 +128,38 @@ public function __construct( array $options ) {
138128
}
139129
}
140130

141-
// Configure SQLite synchronous setting. In WAL mode, default to NORMAL.
142-
// Otherwise, use SQLite's default value.
131+
/*
132+
* Configure SQLite synchronous setting. Default to NORMAL for WAL mode.
133+
*
134+
* WAL improves read/write concurrency and "synchronous = NORMAL" avoids
135+
* frequent sync to the main database, which could become a bottleneck.
136+
* In WAL mode, NORMAL is safe and recommended. From the SQLite docs:
137+
*
138+
* The synchronous=NORMAL setting provides the best balance between
139+
* performance and safety for most applications running in WAL mode.
140+
* You lose durability across power lose with synchronous NORMAL in WAL
141+
* mode, but that is not important for most applications. Transactions
142+
* are still atomic, consistent, and isolated, which are the most
143+
* important characteristics in most use cases.
144+
*
145+
* SQLite defaults to "synchronous = FULL" to avoid data corruption with
146+
* other journal modes. With WAL, this is not necessary.
147+
*
148+
* See: https://sqlite.org/pragma.html#pragma_synchronous
149+
*/
143150
$synchronous = $options['synchronous'] ?? null;
144-
if ( null === $synchronous && 'WAL' === $effective_journal_mode ) {
145-
$synchronous = self::DEFAULT_SQLITE_WAL_SYNCHRONOUS;
146-
} elseif ( is_int( $synchronous ) && isset( self::SQLITE_SYNCHRONOUS_SETTINGS[ $synchronous ] ) ) {
147-
$synchronous = self::SQLITE_SYNCHRONOUS_SETTINGS[ $synchronous ];
148-
} elseif ( is_string( $synchronous ) ) {
149-
$synchronous = strtoupper( $synchronous );
151+
if ( isset( $synchronous ) ) {
152+
// Validate and normalize explicitly provided synchronous value.
153+
if ( is_int( $synchronous ) && isset( self::SQLITE_SYNCHRONOUS_SETTINGS[ $synchronous ] ) ) {
154+
$synchronous = self::SQLITE_SYNCHRONOUS_SETTINGS[ $synchronous ];
155+
} elseif ( is_string( $synchronous ) ) {
156+
$synchronous = strtoupper( $synchronous );
157+
}
158+
} elseif ( 'WAL' === $effective_journal_mode ) {
159+
// Default to NORMAL for WAL mode.
160+
$synchronous = 'NORMAL';
150161
}
151-
if ( $synchronous && in_array( $synchronous, self::SQLITE_SYNCHRONOUS_SETTINGS, true ) ) {
162+
if ( in_array( $synchronous, self::SQLITE_SYNCHRONOUS_SETTINGS, true ) ) {
152163
$this->query( 'PRAGMA synchronous = ' . $synchronous );
153164
}
154165
}

0 commit comments

Comments
 (0)