aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/sync.rs
blob: cb74309785846ad5b66f7ba2eea7b07a6b11d4af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use eyre::{Context, Result};

use crate::atuin_client::{
    database::Database, history::store::HistoryStore, record::sqlite_store::SqliteStore,
    settings::Settings,
};
use crate::atuin_common::record::RecordId;

// This is the only crate that ties together all other crates.
// Therefore, it's the only crate where functions tying together all stores can live

/// Rebuild all stores after a sync
/// Note: for history, this only does an _incremental_ sync. Hence the need to specify downloaded
/// records.
pub(crate) async fn build(
    settings: &Settings,
    store: &SqliteStore,
    db: &dyn Database,
    downloaded: Option<&[RecordId]>,
) -> Result<()> {
    let encryption_key: [u8; 32] = crate::atuin_client::encryption::load_key(settings)
        .context("could not load encryption key")?
        .into();

    let host_id = Settings::host_id().await?;

    let downloaded = downloaded.unwrap_or(&[]);

    let history_store = HistoryStore::new(store.clone(), host_id, encryption_key);

    history_store.incremental_build(db, downloaded).await?;

    Ok(())
}