Skip to content

Read the bundled tile index without writing beside it - #43

Merged
achubaty merged 1 commit into
developmentfrom
fix/sqlite-readonly-immutable-dev
Dec 4, 2026
Merged

achubaty merged 1 commit into
developmentfrom
fix/sqlite-readonly-immutable-dev

Conversation

@eliotmcintire

Copy link
Copy Markdown
Collaborator

Reading the package's built-in tile database wrote temporary SQLite files into the installed package folder. It now reads the database without writing anything there.

Affects: Every call to latestHistoricalYear() and the tile lookup. They work in read-only or shared installs and no longer create files in the package folder. Lookup results are unchanged.

Technical details

Defect: the shipped inst/extdata/ClimateNA_tiles.sqlite was in WAL mode. Even a SQLITE_RO connection (sqlite_connect_db(), R/ClimateNA_createTiles.R, called from R/ClimateNA_useTiles.R:59 and R/latestHistoricalYear.R:21) creates and writes -shm (and -wal) beside a WAL database, here in the install folder. That fails on read-only installs, is shared between hard-linked libraries, and writes into the install directory.

Fix:

  • Read-only connections open file:<path>?immutable=1 with flags = SQLITE_RO. RSQLite passes a file: URI to SQLite as is (RSQLite 3.53.3; no extra URI flag). New sqlite_immutable_uri() builds it: POSIX file:///home/..., Windows file:///C:/... (backslashes to /, drive colon kept), the rest percent-encoded. SQLite then takes no locks and writes nothing. The PRAGMAs (busy_timeout, synchronous, journal_mode) now run only for writing connections. readonly = TRUE assumes nobody is changing the file; that holds for the shipped index.
  • The committed database is converted with PRAGMA journal_mode = DELETE. Contents are the same (all 6 tables compared with identical(); only header bytes 19, 20, 28 and 96 change). md5 47de7e6a... becomes bc94e316...; size stays 48,427,008 bytes.
  • The maintainer step that ships the file (data-raw/06-archive_fingerprints.R) now runs PRAGMA journal_mode = DELETE after the checkpoint. New databases still start in WAL for concurrent building.
  • Data is not moved to tools::R_user_dir(); that is unnecessary once readers write nothing.

Tests (tests/testthat/test-sqliteReadOnlyNoWrites.R, shared dbCopy() in tests/testthat/helper-sqlite.R), failing first on development:

  • the shipped index is not in WAL mode;
  • on a copy of extdata, the readers create no -wal/-shm, and file list, size, mtime and md5 are unchanged;
  • with the folder read-only (mode 555), the readers still work and return the same rows (that last step is skipped where folder modes are ignored: Windows, root);
  • the URI is right for POSIX and Windows paths.

The old lock-wait test in test-sqliteConcurrency.R passed trivially, because immutable reads ignore locks. It now holds a write lock in another process, asserts that an immutable read neither waits nor fails, and that a second writing connection waits and then succeeds.

Full devtools::test(), NOT_CRAN=true, Linux: 123 pass, 2 fail, 1 skip. The 2 failures are in test-prepClimateLayers.R (Google Drive credentials unavailable in this environment; they fail the same on development). The skip is the Windows-only makeClimateDEM test. R CMD check --no-manual shows no WARNINGs; its only NOTE is the existing unused hms import.

Reuses: sqlite_connect_db(), ClimateNA_sql(), ClimateNA_tiles_sqlite(), utils::URLencode(), the existing dbCopy() test helper (moved to helper-sqlite.R), the checkpoint step in data-raw/06-archive_fingerprints.R; new: sqlite_immutable_uri() (nothing existing builds a SQLite file URI).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT

Read-only ClimateNA_sql() connections open the file with SQLite's immutable=1 URI, so no -wal/-shm
files are created. The shipped index is converted from WAL to rollback-journal mode, and the
maintainer step that ships it now switches the journal mode back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT
@eliotmcintire eliotmcintire added the bug Something isn't working label Nov 28, 2026
@achubaty
achubaty merged commit e1a7287 into development Dec 4, 2026
12 checks passed
@achubaty
achubaty deleted the fix/sqlite-readonly-immutable-dev branch December 4, 2026 01:38
achubaty added a commit that referenced this pull request Dec 4, 2026
# Conflicts:
#	NEWS.md
#	inst/extdata/ClimateNA_tiles.sqlite
#	tests/testthat/test-sqliteConcurrency.R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants