coven_database/readable_objects.rs
1//! Every object of coven's that app SQL may read, made on a connection
2//! outside any store, for an app's test that runs its own schema there
3//! (api.md E4).
4
5use coven_foundation::id_source::{DeviceId, FileId};
6use sqlive::rusqlite::Connection;
7
8/// Make on `connection`, a connection an app's test opened itself outside
9/// any store, every object of coven's that app SQL may read, as every store
10/// makes it: the tables the app's local triggers may follow,
11/// `coven_sync_status` and `coven_retry_effect` with their one row, and
12/// coven's views. The function also makes the tables of coven's own that
13/// the views read or that the outside-store writes fill, all empty
14/// (`readable_schema`). The function registers coven's SQL functions, with
15/// `device` as this device's id and `sync_functions` as the computations of
16/// the sync state and of each upload's phase, which the coven crate passes
17/// from sync as its builder does. Nothing transfers files there and
18/// nothing watches the network, as on a read-only handle. The test runs
19/// this function before the app's schema, so the app's triggers and views
20/// apply and run as in a store. coven writes no rows on that connection.
21/// coven's triggers that keep `coven_pending` run there, and the test saves
22/// pending records with `save_outside_store`. The test writes the rows that
23/// coven would write in the other followed tables, and records a user's
24/// original with `record_user_file`.
25pub fn make_readable_objects(
26 connection: &Connection,
27 device: DeviceId,
28 sync_functions: &crate::SyncFunctions,
29) -> sqlive::rusqlite::Result<()> {
30 let policy = crate::app_policy::CovenPolicy::new(&[]);
31 sqlive::Setup::on_test_connection(connection, policy.clone(), |setup| {
32 crate::coven_functions::register(setup, &policy)?;
33 // Nothing starts a transfer or hears of the network here, so
34 // nothing tells this observer of a change.
35 let observer = crate::store_failure::StoreHandle::recording_all(
36 coven_foundation::task::HeardBugs::shared(),
37 );
38 crate::coven_functions::register_views(
39 setup,
40 device,
41 &crate::memory_facts::ProcessFacts::new(observer),
42 Some(sync_functions),
43 )
44 })?;
45 connection.execute_batch(&crate::internal_schema::readable_schema())
46}
47
48/// Record on `connection`, where `make_readable_objects` made coven's
49/// objects, the original at `path`, last modified at `modified_at` and
50/// holding `bytes`, as `file`'s original on this device, as staging it for
51/// the cloud records it: its size, modification time, content hash and
52/// chunk hashes (ยง17.3). `coven_user_files` then lists it.
53pub fn record_user_file(
54 connection: &Connection,
55 file: FileId,
56 path: &std::path::Path,
57 modified_at: std::time::SystemTime,
58 bytes: &[u8],
59) -> Result<(), crate::DbError> {
60 let mut hasher = crate::FileHasher::new();
61 hasher.update(bytes);
62 let original = crate::UserFile {
63 path: path.to_owned(),
64 size: u64::try_from(bytes.len()).expect("every slice's length fits in 64 bits"),
65 modified_at,
66 };
67 crate::user_file::record_outside_store(connection, file, &original, &hasher.finish())
68}
69
70#[cfg(test)]
71#[path = "readable_objects_tests.rs"]
72mod tests;