Skip to content

Commit 6c11310

Browse files
committed
Add index for cache
1 parent d16f8f2 commit 6c11310

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/repo/cache.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ use crate::error::Error;
66

77
pub fn initialize(cache: &Connection) -> Result<(), Error> {
88
cache.execute(
9-
"CREATE TABLE commits_cache (
10-
branch TEXT NOT NULL,
11-
commit_hash TEXT NOT NULL
12-
)",
9+
"CREATE TABLE IF NOT EXISTS commits_cache (
10+
branch TEXT NOT NULL,
11+
commit_hash TEXT NOT NULL
12+
)",
1313
[],
1414
)?;
1515
cache.execute(
16-
"CREATE TABLE branches (
17-
branch TEXT NOT NULL PRIMARY KEY,
18-
current_commit TEXT NOT NULL
19-
)",
16+
"CREATE TABLE IF NOT EXISTS branches (
17+
branch TEXT NOT NULL PRIMARY KEY,
18+
current_commit TEXT NOT NULL
19+
)",
20+
[],
21+
)?;
22+
cache.execute(
23+
"CREATE INDEX IF NOT EXISTS idx_commit_branches
24+
ON commits_cache (commit_hash)",
2025
[],
2126
)?;
2227

src/repo/resources.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ impl Resource<String> for RepoResources {
3232
// load repo
3333
let repo = Mutex::new(Repository::open(&paths.repo)?);
3434
// load cache
35-
let cache_exists = paths.cache.is_file();
3635
let cache_cfg = deadpool_sqlite::Config::new(&paths.cache);
3736
let cache = cache_cfg.create_pool(deadpool_sqlite::Runtime::Tokio1)?;
38-
if !cache_exists {
39-
log::debug!("initializing cache for {name}...");
40-
let conn = cache.get().await?;
41-
conn.interact(|c| cache::initialize(c))
42-
.await
43-
.map_err(|e| Error::DBInteract(Mutex::new(e)))??;
44-
}
37+
log::debug!("initializing cache for {name}...");
38+
let conn = cache.get().await?;
39+
conn.interact(|c| cache::initialize(c))
40+
.await
41+
.map_err(|e| Error::DBInteract(Mutex::new(e)))??;
4542
// load settings
4643
let settings = RwLock::new(read_json(&paths.settings)?);
4744

0 commit comments

Comments
 (0)