Skip to content

Commit a53a275

Browse files
committed
Bugfix: skip write pragmas on read-only conns
- `apply_pragmas` ran `auto_vacuum` and `journal_mode` on read-only SQLite connections, causing "attempt to write a readonly database" panic in `scan_subtree_only` (and any subtree scan in production) - Added `readonly` parameter to `apply_pragmas`; read-only connections now only get `synchronous` and `cache_size`
1 parent c0a63f5 commit a53a275

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

  • apps/desktop/src-tauri/src/indexing

apps/desktop/src-tauri/src/indexing/store.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,15 @@ fn ensure_root_sentinel(conn: &Connection) -> Result<(), IndexStoreError> {
278278
}
279279

280280
/// Apply WAL-mode pragmas for performance.
281-
fn apply_pragmas(conn: &Connection) -> Result<(), IndexStoreError> {
281+
fn apply_pragmas(conn: &Connection, readonly: bool) -> Result<(), IndexStoreError> {
282+
if !readonly {
283+
conn.execute_batch(
284+
"PRAGMA auto_vacuum = INCREMENTAL;
285+
PRAGMA journal_mode = WAL;",
286+
)?;
287+
}
282288
conn.execute_batch(
283-
"PRAGMA auto_vacuum = INCREMENTAL;
284-
PRAGMA journal_mode = WAL;
285-
PRAGMA synchronous = NORMAL;
289+
"PRAGMA synchronous = NORMAL;
286290
PRAGMA cache_size = -16384;",
287291
)?;
288292
Ok(())
@@ -400,7 +404,7 @@ impl IndexStore {
400404
fn try_open(db_path: &Path) -> Result<Self, IndexStoreError> {
401405
let conn = Connection::open(db_path)?;
402406
register_platform_case_collation(&conn)?;
403-
apply_pragmas(&conn)?;
407+
apply_pragmas(&conn, false)?;
404408
create_tables(&conn)?;
405409

406410
// Check schema version
@@ -445,7 +449,7 @@ impl IndexStore {
445449

446450
let conn = Connection::open(db_path)?;
447451
register_platform_case_collation(&conn)?;
448-
apply_pragmas(&conn)?;
452+
apply_pragmas(&conn, false)?;
449453
create_tables(&conn)?;
450454
conn.execute(
451455
"INSERT OR REPLACE INTO meta (key, value) VALUES (?1, ?2)",
@@ -463,17 +467,17 @@ impl IndexStore {
463467
pub fn open_write_connection(db_path: &Path) -> Result<Connection, IndexStoreError> {
464468
let conn = Connection::open(db_path)?;
465469
register_platform_case_collation(&conn)?;
466-
apply_pragmas(&conn)?;
470+
apply_pragmas(&conn, false)?;
467471
Ok(conn)
468472
}
469473

470-
/// Open a read-only connection with WAL pragmas and `platform_case` collation.
474+
/// Open a read-only connection with per-connection pragmas and `platform_case` collation.
471475
///
472476
/// Never contends with the writer thread's write lock.
473477
pub fn open_read_connection(db_path: &Path) -> Result<Connection, IndexStoreError> {
474478
let conn = Connection::open_with_flags(db_path, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)?;
475479
register_platform_case_collation(&conn)?;
476-
apply_pragmas(&conn)?;
480+
apply_pragmas(&conn, true)?;
477481
Ok(conn)
478482
}
479483

0 commit comments

Comments
 (0)