From cdd6803a60daaf61f41316a9168bb78ac375690b Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 27 Nov 2026 21:11:29 -0700 Subject: [PATCH 1/4] Read the bundled tile index without writing beside it 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 Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT --- NEWS.md | 4 ++ R/ClimateNA_createTiles.R | 26 +++++++-- data-raw/06-archive_fingerprints.R | 1 + inst/extdata/ClimateNA_tiles.sqlite | Bin 48427008 -> 48427008 bytes tests/testthat/test-sqliteReadOnlyNoWrites.R | 57 +++++++++++++++++++ 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-sqliteReadOnlyNoWrites.R diff --git a/NEWS.md b/NEWS.md index f2fc9a9..dd311a7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # climateData (development version) +- Reading the bundled tile index (`latestHistoricalYear()`, the tile lookup in `ClimateNA_useTiles()`) no longer + writes `-wal`/`-shm` files into the installed package folder. Read-only `ClimateNA_sql()` connections open the + file with SQLite's `immutable=1`, and the bundled index is shipped in rollback-journal mode instead of WAL. + - `latestHistoricalYear()` and the tile lookup in `ClimateNA_useTiles()` open the bundled tile index read-only and set the SQLite busy timeout before any other statement. Before, many processes opening it together (e.g. parallel hindcast jobs parsing fireSense modules on NFS) could fail with "database is locked". diff --git a/R/ClimateNA_createTiles.R b/R/ClimateNA_createTiles.R index 3e77095..c1e78e6 100644 --- a/R/ClimateNA_createTiles.R +++ b/R/ClimateNA_createTiles.R @@ -92,9 +92,23 @@ sqlite_connect_db <- function(dbfile, readonly = FALSE) { ## builds a library containing only this package's Depends/Imports/LinkingTo. ## climateData therefore declares hms in its own Imports: the requirement is ## real and RSQLite does not state it correctly. + ## A read-only connection opens the file with SQLite's `immutable=1` URI parameter: SQLite then takes no + ## locks and creates no `-wal`/`-shm` files, so reading never writes beside the database (e.g. into the + ## installed package folder, which may be read-only or shared). Use it only for a file nobody is changing. + ## RSQLite hands a `file:` URI straight to SQLite, so no extra flag is needed. + if (isTRUE(readonly)) { + dbname <- paste0( + "file:", + gsub("%2F", "/", utils::URLencode(normalizePath(dbfile, winslash = "/"), reserved = TRUE), fixed = TRUE), + "?immutable=1" + ) + } else { + dbname <- dbfile + } + db <- RSQLite::dbConnect( drv = RSQLite::SQLite(), - dbname = dbfile, + dbname = dbname, ## `synchronous = NULL`: dbConnect would otherwise run `PRAGMA synchronous` before any busy timeout ## is set, which fails at once with "database is locked" when other processes have the file open. synchronous = NULL, @@ -102,13 +116,15 @@ sqlite_connect_db <- function(dbfile, readonly = FALSE) { extended_types = TRUE ## for DATETIME -- see note above re: hms ) - ## must come first: every statement below waits (ms) for a lock instead of failing at once - DBI::dbExecute(db, "PRAGMA busy_timeout = 60000") - if (!isTRUE(readonly)) { + ## must come first: every statement below waits (ms) for a lock instead of failing at once + DBI::dbExecute(db, "PRAGMA busy_timeout = 60000") DBI::dbExecute(db, "PRAGMA synchronous = normal") if (isTRUE(newDb)) { - DBI::dbExecute(db, "PRAGMA journal_mode = WAL") ## using WAL allows concurrency + ## WAL allows concurrent writers while building. Switch back to `journal_mode = DELETE` before the + ## file is shipped in inst/extdata (see data-raw/06-archive_fingerprints.R): a WAL file creates + ## `-wal`/`-shm` files beside it even when only read. + DBI::dbExecute(db, "PRAGMA journal_mode = WAL") } } diff --git a/data-raw/06-archive_fingerprints.R b/data-raw/06-archive_fingerprints.R index 4ec114d..2b3edc5 100644 --- a/data-raw/06-archive_fingerprints.R +++ b/data-raw/06-archive_fingerprints.R @@ -35,6 +35,7 @@ db <- sqlite_connect_db(wrkngDBfile) n <- .archivesUpsert(db, fingerprints) message(n, " fingerprints written; ", sum(is.na(fingerprints$verified)), " zips not verified") DBI::dbExecute(db, "PRAGMA wal_checkpoint(TRUNCATE)") +DBI::dbExecute(db, "PRAGMA journal_mode = DELETE") ## ship in rollback-journal mode: reading a WAL file writes -wal/-shm beside it DBI::dbDisconnect(db) stopifnot(!file.exists(paste0(wrkngDBfile, "-wal")) || file.size(paste0(wrkngDBfile, "-wal")) == 0) diff --git a/inst/extdata/ClimateNA_tiles.sqlite b/inst/extdata/ClimateNA_tiles.sqlite index 48e98cfb10405198d40472219220f64bf4adda5e..e177d061d2aa441962052c3033ed73247c6d7e85 100755 GIT binary patch delta 2741 zcmWmDW1A2N07c=M$+o9yvTfV8>t=hhCfsVWZQHhO+qGMZYPXiPyJtVVf8adN34cB* zSvZ30*R9bdtV3wWUVVGC2@B015I-O=FrZ9mXh1+f$c*6ffgz)01zJH?BrCEN#foZ0 zv!Yuute93TE4CHKifhHQ;#&!wXE7!9jmTY&#G@Vuo_y8tj1OotEtt@ zYHqc#T3W5F))sx*SZ%F#R(q?1)zRu?b+)=#U9E0bcdLih)9PjQw)$9ot$tRR)!!Om z4YUSXgRLRfP-~bq+!|qxw8E`X)@W;tHP*5<&Khq`up+F9)+B4PHN~20O|zz3Gpw1` zENiwk$C_)+v*ue1tcBJhYq7P&T52t`mRl>VmDVb2wYA1tYpt`^TN|v6)+TGSwZ+#%jiI%*xWj$0?Jlh!Hgv~|WhYn`*sTNkX0 z)+Ot*b;Y`BU9+xRH>{i1E$g;*$GU6Xv+i3DtcTVk>#_C3dTKqho?9=hm)0xmwe`k& zYkg;ZZ@shLTR&JoT0dDoTOX{C)+g&1>sRZu^~L(l`rZ1&`qTQ$`rGZ#Sju9A%aE!uejKNqK<1ii*5P^x9 zgvpqKshEc8n1Pv?h1r;cxtNFfSb&9CgvD5brC5gLSb>#Th1FPtwOEJs*no}Lgw5E3 zt=NX`*nyqch27YLz1WBSIDmsVgu^(3qd11+IDwNmh0{2Lvp9$IxPXhegv+>stGI^i zxPhCvh1kLSx01L4Qi?IYt zu?)+x0xPi!tFZ=au@3980UNOio3RC3u?^d?13R$`yRip*u@C!k00(ghhj9c)aSX?C z0w-|_r*Q^naSrEk0T*!zmvIGGaShjT12=ICw{Zt|aS!+L01xp9kMRUg@eI%LBIt-- I1}*>aA7cO)9RL6T delta 2741 zcmWmDV|y3|07c;>we6%$ZQHi(_SSZ4rMPWV+qP}nwmlSEw{qUjKD>Y6JkJS#J}Fr^ zg6r3<(Iu=yXvbcCd$b7)%^wgyASfuHOlW98KtRZh;PQbXqhtkIK~^LyvK7UOYDKf6 zTQRJdRxB&F6~~He#k1mD39N)xA}g_##7b%`Gm9R=$rL59c z8LO;S&MI$Juqs-WtjbmutEyGas&3V=YFf3d+EyK_u2s*fZ#A$QT8*s6RuikK)y!&c zwXj-Rt*q7-ecD)Ut#(#>tAo|i>ST4cx>#MUZdP}zht<>SW%aiDSbeR2R+!b_8ek2y z23doxA=Xf9m^IuQVU4uHtx?u!Ym7D4vNg^cZ%wcwtclhnYqB-Pnrcn6rdud1##(Ewv({T1tc}(tYqPb*+G=gH zwp%-_oz^aEx3$OGYwff4TL-Lz)*#}vlx@ujsu3I;(o7OGswspt4Yu&T%TMw*<)+6h&^~8E=J+q!$FRYi=E9$CO6`f7c%ezShJ{;>YE{<8kI{;~dzP~a2= zA_$QX8Bq`w(GVRm5EHQw8*va9@em&gkPwNG7)g*6$&ef=kP@j7j1Z(o8iXP((jh%E zAR{s%GqNBnvLQQiASZGmH}W7a@*zJ8pdbpNFp8ikilI14pd?D6G|HeX%Aq_epdu=v zGOC~|s-Ze+peAaeHtL`*>Y+XwpdlKeF`A$$nxQ#bpe0(NHU97425r#}?a=`p(FvW= z1zph%-O&R*(F?uN2Yt~GVd#$m7>Gd_j3F3`VHl1P7>RI;hy6H!gE)l4ID(@%hT}MalQ@ObID@k|hx53Ai@1c#xPq&=hU>V2 zo4AGBxP!a6hx>Sdhj@g?c!H;RhUa*Jmw1KO_zvIW2mFXPc#C&KAwCiyArc`m zk{~IPAvsbYB~l?6AxMoh2t`_?LwaODMr1-}WIt^ z6hToGLvfTqNt8lqltEdPLwQs{MN~p%R6$i#Lv_?ZP1Hhd)InX;Lwz(rLo`BTG(l4| zLvyr1OSD33{NKL~+M*rWqXRmk6FQ>{x}qDpqX&AT7kZ-)`l27g&>sUZ5Q8unLogJ> zFdQQ=65$wy(HMiVFvej#CLjV6F$t3~1yeB%(=h`xF$=RX2XiqG^RWO6u?UN?1WU0D z%drA0u?nlP25Yen>#+eFu?d^81zWKV+pz;Xu?xGg2Yay(`*8pVaR`TT1V?cU$8iED zaSEq#24`^&=WziSaS4}k1y^wm*Kq?kaSOL`2X}D~_wfJ^@d%Ic1W)k{&+#JYh+YOQ G|L`AU78e}= diff --git a/tests/testthat/test-sqliteReadOnlyNoWrites.R b/tests/testthat/test-sqliteReadOnlyNoWrites.R new file mode 100644 index 0000000..05d214e --- /dev/null +++ b/tests/testthat/test-sqliteReadOnlyNoWrites.R @@ -0,0 +1,57 @@ +## Reading the shipped tile index must not write anything beside it (no -wal/-shm files, no change to the +## database file), also when the folder is read-only (read-only installs, hard-linked or shared libraries). + +skip_on_os("windows") + +extdataCopy <- function(env = parent.frame()) { + d <- tempfile("extdata") + dir.create(d) + f <- file.path(d, "ClimateNA_tiles.sqlite") + file.copy(ClimateNA_tiles_sqlite(), f) + withr::defer({ + Sys.chmod(d, "755") + unlink(d, recursive = TRUE) + }, envir = env) + f +} + +dbState <- function(f) { + list(files = list.files(dirname(f), all.files = TRUE, no.. = TRUE), + size = file.size(f), mtime = file.mtime(f), md5 = unname(tools::md5sum(f))) +} + +lookup <- function() { + dbdf <- ClimateNA_sql(ClimateNA_tiles_sqlite(), "historical", readonly = TRUE) + on.exit(DBI::dbDisconnect(dbdf[["db"]]), add = TRUE) + dplyr::collect(dplyr::arrange(dplyr::filter(dbdf[["df"]], tileid <= 20L), rowid)) +} + +test_that("the shipped tile index is not in WAL mode", { + con <- DBI::dbConnect(RSQLite::SQLite(), extdataCopy(), flags = RSQLite::SQLITE_RO) + on.exit(DBI::dbDisconnect(con)) + expect_identical(DBI::dbGetQuery(con, "PRAGMA journal_mode")[[1]], "delete") +}) + +test_that("read-only lookups write nothing beside the database", { + f <- extdataCopy() + testthat::local_mocked_bindings(ClimateNA_tiles_sqlite = function() f) + before <- dbState(f) + res <- lookup() + yr <- latestHistoricalYear() + expect_identical(dbState(f), before) + expect_gt(nrow(res), 0L) + expect_true(is.numeric(yr) || is.integer(yr)) +}) + +test_that("read-only lookups work in a read-only folder and give the same rows", { + f <- extdataCopy() + testthat::local_mocked_bindings(ClimateNA_tiles_sqlite = function() f) + expected <- lookup() + expectedYr <- latestHistoricalYear() + Sys.chmod(dirname(f), "555") + skip_if(file.access(dirname(f), 2) == 0, "cannot make a folder read-only here (running as root?)") + before <- dbState(f) + expect_identical(lookup(), expected) + expect_identical(latestHistoricalYear(), expectedYr) + expect_identical(dbState(f), before) +}) From 09af7e3d254dcecee52630140dd37d2b6ac2012e Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 27 Nov 2026 21:13:32 -0700 Subject: [PATCH 2/4] Run CI on modsForFireSense; test write locks, not immutable reads The lock-wait test now uses two writing connections and asserts an immutable read ignores the lock. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT --- .github/workflows/R-CMD-check.yaml | 4 ++-- .github/workflows/docker-climatena.yaml | 2 +- tests/testthat/test-sqliteConcurrency.R | 27 +++++++++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 5f5d776..b16718b 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -10,9 +10,9 @@ name: R-CMD-check on: push: - branches: [main, master, development] + branches: [main, master, development, modsForFireSense] pull_request: - branches: [main, master, development] + branches: [main, master, development, modsForFireSense] jobs: R-CMD-check: diff --git a/.github/workflows/docker-climatena.yaml b/.github/workflows/docker-climatena.yaml index 4d75266..121da84 100644 --- a/.github/workflows/docker-climatena.yaml +++ b/.github/workflows/docker-climatena.yaml @@ -9,7 +9,7 @@ on: branches: [main, development] paths: ["docker/climatena/**", ".github/workflows/docker-climatena.yaml"] pull_request: - branches: [main, development] + branches: [main, development, modsForFireSense] paths: ["docker/climatena/**", ".github/workflows/docker-climatena.yaml"] workflow_dispatch: diff --git a/tests/testthat/test-sqliteConcurrency.R b/tests/testthat/test-sqliteConcurrency.R index bc553cf..f0a8a29 100644 --- a/tests/testthat/test-sqliteConcurrency.R +++ b/tests/testthat/test-sqliteConcurrency.R @@ -50,23 +50,42 @@ test_that("a read-only lookup neither writes nor creates tables", { expect_false(DBI::dbExistsTable(con, "future_normals")) }) -test_that("a lookup waits for a lock held by another process instead of failing", { +test_that("writing connections wait for each other's lock; an immutable read does not", { f <- dbCopy() on.exit(unlink(paste0(f, c("", "-wal", "-shm"))), add = TRUE) - ## rollback-journal mode, where a writer's lock blocks readers + ## rollback-journal mode, as shipped, where a writer's lock blocks other connections con <- DBI::dbConnect(RSQLite::SQLite(), f) DBI::dbExecute(con, "PRAGMA journal_mode = DELETE") DBI::dbDisconnect(con) testthat::local_mocked_bindings(ClimateNA_tiles_sqlite = function() f) + nRows <- function() { + d <- ClimateNA_sql(f, "historical", readonly = TRUE) + on.exit(DBI::dbDisconnect(d[["db"]])) + nrow(dplyr::collect(d[["df"]])) + } + nBefore <- nRows() holder <- parallel::mcparallel({ con <- DBI::dbConnect(RSQLite::SQLite(), f) DBI::dbExecute(con, "BEGIN EXCLUSIVE") - Sys.sleep(2) + Sys.sleep(3) DBI::dbExecute(con, "COMMIT") DBI::dbDisconnect(con) }) Sys.sleep(0.5) ## let the holder take its lock - expect_no_error(latestHistoricalYear()) + + ## the immutable read ignores the lock: it neither waits nor fails + t0 <- Sys.time() + expect_no_error(yr <- latestHistoricalYear()) + expect_lt(as.numeric(difftime(Sys.time(), t0, units = "secs")), 2) + + ## a second writing connection waits for the lock (busy_timeout) instead of failing, then writes + t0 <- Sys.time() + w <- ClimateNA_sql(f, "historical") + expect_no_error(DBI::dbExecute(w[["db"]], "DELETE FROM historical WHERE rowid = (SELECT max(rowid) FROM historical)")) + DBI::dbDisconnect(w[["db"]]) + expect_gt(as.numeric(difftime(Sys.time(), t0, units = "secs")), 1) parallel::mccollect(holder) + + expect_identical(nRows(), nBefore - 1L) }) From edf41dd444f9697ae5db7523061ce7abe602e542 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 27 Nov 2026 21:20:36 -0700 Subject: [PATCH 3/4] Leave the docker-climatena workflow on main/development It pushes the :latest image on push events and its Wine smoke test fails on this runner. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT --- .github/workflows/docker-climatena.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-climatena.yaml b/.github/workflows/docker-climatena.yaml index 121da84..4d75266 100644 --- a/.github/workflows/docker-climatena.yaml +++ b/.github/workflows/docker-climatena.yaml @@ -9,7 +9,7 @@ on: branches: [main, development] paths: ["docker/climatena/**", ".github/workflows/docker-climatena.yaml"] pull_request: - branches: [main, development, modsForFireSense] + branches: [main, development] paths: ["docker/climatena/**", ".github/workflows/docker-climatena.yaml"] workflow_dispatch: From 9631e2cc3fcc36409e10eb54c1ee51b2159b896e Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 27 Nov 2026 21:38:23 -0700 Subject: [PATCH 4/4] Immutable URI for Windows paths; shared dbCopy test helper; run the read-only tests on Windows Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT --- R/ClimateNA_createTiles.R | 26 ++++++++++------- tests/testthat/helper-sqlite.R | 13 +++++++++ tests/testthat/test-sqliteConcurrency.R | 6 ---- tests/testthat/test-sqliteReadOnlyNoWrites.R | 30 ++++++++------------ 4 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 tests/testthat/helper-sqlite.R diff --git a/R/ClimateNA_createTiles.R b/R/ClimateNA_createTiles.R index c1e78e6..620f708 100644 --- a/R/ClimateNA_createTiles.R +++ b/R/ClimateNA_createTiles.R @@ -75,6 +75,20 @@ ClimateNA_path <- function(dataPath, tile = NULL, type = NULL, msy = NULL, gcm = })() } +#' SQLite `file:` URI that opens `dbfile` immutably +#' +#' POSIX paths give `file:///home/...`; Windows paths give `file:///C:/...` (backslashes become `/`, the +#' drive colon stays). The rest of the path is percent-encoded. +#' +#' @keywords internal +sqlite_immutable_uri <- function(dbfile) { + path <- gsub("\\", "/", normalizePath(dbfile, winslash = "/", mustWork = FALSE), fixed = TRUE) + drive <- if (grepl("^[A-Za-z]:", path)) substr(path, 1L, 2L) else "" + rest <- substring(path, nchar(drive) + 1L) + rest <- gsub("%2F", "/", utils::URLencode(rest, reserved = TRUE), fixed = TRUE) + paste0("file://", if (nzchar(drive)) "/", drive, rest, "?immutable=1") +} + #' @keywords internal #' @importFrom DBI dbExecute #' @importFrom RSQLite dbConnect SQLite @@ -95,16 +109,8 @@ sqlite_connect_db <- function(dbfile, readonly = FALSE) { ## A read-only connection opens the file with SQLite's `immutable=1` URI parameter: SQLite then takes no ## locks and creates no `-wal`/`-shm` files, so reading never writes beside the database (e.g. into the ## installed package folder, which may be read-only or shared). Use it only for a file nobody is changing. - ## RSQLite hands a `file:` URI straight to SQLite, so no extra flag is needed. - if (isTRUE(readonly)) { - dbname <- paste0( - "file:", - gsub("%2F", "/", utils::URLencode(normalizePath(dbfile, winslash = "/"), reserved = TRUE), fixed = TRUE), - "?immutable=1" - ) - } else { - dbname <- dbfile - } + ## RSQLite hands a `file:` URI straight to SQLite, so no extra flag is needed beyond `SQLITE_RO`. + dbname <- if (isTRUE(readonly)) sqlite_immutable_uri(dbfile) else dbfile db <- RSQLite::dbConnect( drv = RSQLite::SQLite(), diff --git a/tests/testthat/helper-sqlite.R b/tests/testthat/helper-sqlite.R new file mode 100644 index 0000000..12f8573 --- /dev/null +++ b/tests/testthat/helper-sqlite.R @@ -0,0 +1,13 @@ +## A copy of the shipped tile index in its own temporary folder (removed when the calling test ends), +## so a test can tell whether anything else appears beside it. +dbCopy <- function(env = parent.frame()) { + d <- tempfile("extdata") + dir.create(d) + f <- file.path(d, "ClimateNA_tiles.sqlite") + file.copy(ClimateNA_tiles_sqlite(), f) + withr::defer({ + Sys.chmod(d, "755") + unlink(d, recursive = TRUE) + }, envir = env) + f +} diff --git a/tests/testthat/test-sqliteConcurrency.R b/tests/testthat/test-sqliteConcurrency.R index f0a8a29..87c6635 100644 --- a/tests/testthat/test-sqliteConcurrency.R +++ b/tests/testthat/test-sqliteConcurrency.R @@ -4,12 +4,6 @@ skip_on_os("windows") ## uses forked workers -dbCopy <- function() { - f <- tempfile(fileext = ".sqlite") - file.copy(ClimateNA_tiles_sqlite(), f) - f -} - test_that("parallel latestHistoricalYear() calls on one database do not fail", { f <- dbCopy() on.exit(unlink(paste0(f, c("", "-wal", "-shm"))), add = TRUE) diff --git a/tests/testthat/test-sqliteReadOnlyNoWrites.R b/tests/testthat/test-sqliteReadOnlyNoWrites.R index 05d214e..43bdae7 100644 --- a/tests/testthat/test-sqliteReadOnlyNoWrites.R +++ b/tests/testthat/test-sqliteReadOnlyNoWrites.R @@ -1,20 +1,6 @@ ## Reading the shipped tile index must not write anything beside it (no -wal/-shm files, no change to the ## database file), also when the folder is read-only (read-only installs, hard-linked or shared libraries). -skip_on_os("windows") - -extdataCopy <- function(env = parent.frame()) { - d <- tempfile("extdata") - dir.create(d) - f <- file.path(d, "ClimateNA_tiles.sqlite") - file.copy(ClimateNA_tiles_sqlite(), f) - withr::defer({ - Sys.chmod(d, "755") - unlink(d, recursive = TRUE) - }, envir = env) - f -} - dbState <- function(f) { list(files = list.files(dirname(f), all.files = TRUE, no.. = TRUE), size = file.size(f), mtime = file.mtime(f), md5 = unname(tools::md5sum(f))) @@ -27,13 +13,13 @@ lookup <- function() { } test_that("the shipped tile index is not in WAL mode", { - con <- DBI::dbConnect(RSQLite::SQLite(), extdataCopy(), flags = RSQLite::SQLITE_RO) + con <- DBI::dbConnect(RSQLite::SQLite(), dbCopy(), flags = RSQLite::SQLITE_RO) on.exit(DBI::dbDisconnect(con)) expect_identical(DBI::dbGetQuery(con, "PRAGMA journal_mode")[[1]], "delete") }) test_that("read-only lookups write nothing beside the database", { - f <- extdataCopy() + f <- dbCopy() testthat::local_mocked_bindings(ClimateNA_tiles_sqlite = function() f) before <- dbState(f) res <- lookup() @@ -44,14 +30,22 @@ test_that("read-only lookups write nothing beside the database", { }) test_that("read-only lookups work in a read-only folder and give the same rows", { - f <- extdataCopy() + f <- dbCopy() testthat::local_mocked_bindings(ClimateNA_tiles_sqlite = function() f) expected <- lookup() expectedYr <- latestHistoricalYear() Sys.chmod(dirname(f), "555") - skip_if(file.access(dirname(f), 2) == 0, "cannot make a folder read-only here (running as root?)") before <- dbState(f) expect_identical(lookup(), expected) expect_identical(latestHistoricalYear(), expectedYr) expect_identical(dbState(f), before) + + ## Windows ignores folder modes, and root can write anywhere: then only the assertions above ran + skip_if(file.access(dirname(f), 2) == 0, "cannot make a folder read-only here (Windows or root)") +}) + +test_that("the immutable URI is built for POSIX and Windows paths", { + expect_identical(sqlite_immutable_uri("C:\\Users\\a b\\x.sqlite"), "file:///C:/Users/a%20b/x.sqlite?immutable=1") + skip_on_os("windows") ## normalizePath() adds a drive letter to a POSIX path + expect_identical(sqlite_immutable_uri("/home/a b/x#.sqlite"), "file:///home/a%20b/x%23.sqlite?immutable=1") })