diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-05-29 14:38:18 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-29 18:38:18 +0000 |
commit | 94f040ac2867706d261e2fe1ec8bc2c4263eb6ab (patch) | |
tree | 5eaed4d41efd8d25da839bc29b8d32554e1a0fca /cli/cache/code_cache.rs | |
parent | fada25b0dd593efee496dabb48ed9cb7a9cb6647 (diff) |
fix: bump cache sqlite dbs to v2 for WAL journal mode change (#24030)
In https://github.com/denoland/deno/pull/23955 we changed the sqlite db
journal mode to WAL. This causes issues when someone is running an old
version of Deno using TRUNCATE and a new version because the two fight
against each other.
Diffstat (limited to 'cli/cache/code_cache.rs')
-rw-r--r-- | cli/cache/code_cache.rs | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/cli/cache/code_cache.rs b/cli/cache/code_cache.rs index 8d96bf3a1..abcd0d46a 100644 --- a/cli/cache/code_cache.rs +++ b/cli/cache/code_cache.rs @@ -7,16 +7,19 @@ use deno_runtime::deno_webstorage::rusqlite::params; use super::cache_db::CacheDB; use super::cache_db::CacheDBConfiguration; +use super::cache_db::CacheDBHash; use super::cache_db::CacheFailure; pub static CODE_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration { - table_initializer: "CREATE TABLE IF NOT EXISTS codecache ( - specifier TEXT NOT NULL, - type TEXT NOT NULL, - source_hash TEXT NOT NULL, - data BLOB NOT NULL, - PRIMARY KEY (specifier, type) - );", + table_initializer: concat!( + "CREATE TABLE IF NOT EXISTS codecache (", + "specifier TEXT NOT NULL,", + "type INTEGER NOT NULL,", + "source_hash INTEGER NOT NULL,", + "data BLOB NOT NULL,", + "PRIMARY KEY (specifier, type)", + ");" + ), on_version_change: "DELETE FROM codecache;", preheat_queries: &[], on_failure: CacheFailure::Blackhole, @@ -59,7 +62,7 @@ impl CodeCache { Self::ensure_ok(self.inner.get_sync( specifier.as_str(), code_cache_type, - &source_hash.to_string(), + CacheDBHash::new(source_hash), )) } @@ -73,7 +76,7 @@ impl CodeCache { Self::ensure_ok(self.inner.set_sync( specifier.as_str(), code_cache_type, - &source_hash.to_string(), + CacheDBHash::new(source_hash), data, )); } @@ -113,7 +116,7 @@ impl CodeCacheInner { &self, specifier: &str, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, + source_hash: CacheDBHash, ) -> Result<Option<Vec<u8>>, AnyError> { let query = " SELECT @@ -123,7 +126,11 @@ impl CodeCacheInner { WHERE specifier=?1 AND type=?2 AND source_hash=?3 LIMIT 1"; - let params = params![specifier, code_cache_type.as_str(), source_hash,]; + let params = params![ + specifier, + serialize_code_cache_type(code_cache_type), + source_hash, + ]; self.conn.query_row(query, params, |row| { let value: Vec<u8> = row.get(0)?; Ok(value) @@ -134,7 +141,7 @@ impl CodeCacheInner { &self, specifier: &str, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, // use string because sqlite doesn't have a u64 type + source_hash: CacheDBHash, data: &[u8], ) -> Result<(), AnyError> { let sql = " @@ -142,13 +149,26 @@ impl CodeCacheInner { codecache (specifier, type, source_hash, data) VALUES (?1, ?2, ?3, ?4)"; - let params = - params![specifier, code_cache_type.as_str(), source_hash, data]; + let params = params![ + specifier, + serialize_code_cache_type(code_cache_type), + source_hash, + data + ]; self.conn.execute(sql, params)?; Ok(()) } } +fn serialize_code_cache_type( + code_cache_type: code_cache::CodeCacheType, +) -> i64 { + match code_cache_type { + code_cache::CodeCacheType::Script => 0, + code_cache::CodeCacheType::EsModule => 1, + } +} + #[cfg(test)] mod test { use super::*; @@ -162,7 +182,7 @@ mod test { .get_sync( "file:///foo/bar.js", code_cache::CodeCacheType::EsModule, - "hash", + CacheDBHash::new(1), ) .unwrap() .is_none()); @@ -171,7 +191,7 @@ mod test { .set_sync( "file:///foo/bar.js", code_cache::CodeCacheType::EsModule, - "hash", + CacheDBHash::new(1), &data_esm, ) .unwrap(); @@ -180,7 +200,7 @@ mod test { .get_sync( "file:///foo/bar.js", code_cache::CodeCacheType::EsModule, - "hash", + CacheDBHash::new(1), ) .unwrap() .unwrap(), @@ -191,7 +211,7 @@ mod test { .get_sync( "file:///foo/bar.js", code_cache::CodeCacheType::Script, - "hash", + CacheDBHash::new(1), ) .unwrap() .is_none()); @@ -200,7 +220,7 @@ mod test { .set_sync( "file:///foo/bar.js", code_cache::CodeCacheType::Script, - "hash", + CacheDBHash::new(1), &data_script, ) .unwrap(); @@ -209,7 +229,7 @@ mod test { .get_sync( "file:///foo/bar.js", code_cache::CodeCacheType::Script, - "hash", + CacheDBHash::new(1), ) .unwrap() .unwrap(), @@ -220,7 +240,7 @@ mod test { .get_sync( "file:///foo/bar.js", code_cache::CodeCacheType::EsModule, - "hash", + CacheDBHash::new(1), ) .unwrap() .unwrap(), |