Skip to main content

coven_database/store/store_session/test_support/
journal.rs

1use super::{DbError, StoreDatabase, StoreSession, WriteId};
2use coven_protocol::write::WriteStatus;
3
4impl StoreDatabase {
5    pub async fn store_write_journal_for_test(&self) -> Result<String, DbError> {
6        self.call_store(|session| session.store_write_journal_for_test())
7            .await
8    }
9
10    /// Read the original capture bytes independently of replacement preparation.
11    pub async fn store_write_capture_for_test(
12        &self,
13        write_id: WriteId,
14    ) -> Result<(String, String, String), DbError> {
15        self.call_store(move |session| session.store_write_capture_for_test(&write_id))
16            .await
17    }
18
19    pub async fn store_write_status_count_for_test(
20        &self,
21        status: WriteStatus,
22    ) -> Result<i64, DbError> {
23        self.call_store(move |session| session.store_write_status_count_for_test(&status))
24            .await
25    }
26
27    pub async fn has_rebased_store_writes_for_test(&self) -> Result<bool, DbError> {
28        self.call_store(|session| session.has_rebased_store_writes_for_test())
29            .await
30    }
31}
32
33impl StoreSession<'_> {
34    fn store_write_journal_for_test(&self) -> Result<String, DbError> {
35        self.conn
36            .query_row(
37                "SELECT json_group_array(json_array(ordinal, write_id, status, affected_rows,
38                    changeset_hash, base, blob_facts, rebased, prepared))
39                 FROM (SELECT * FROM store_writes ORDER BY ordinal)",
40                [],
41                |row| row.get(0),
42            )
43            .map_err(DbError::from)
44    }
45
46    fn store_write_capture_for_test(
47        &self,
48        write_id: &WriteId,
49    ) -> Result<(String, String, String), DbError> {
50        self.conn
51            .query_row(
52                "SELECT base, changeset_hash, blob_facts FROM store_writes WHERE write_id = ?1",
53                [write_id.as_str()],
54                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
55            )
56            .map_err(DbError::from)
57    }
58
59    fn store_write_status_count_for_test(&self, status: &WriteStatus) -> Result<i64, DbError> {
60        let status = serde_json::to_string(status)?;
61        self.conn
62            .query_row(
63                "SELECT COUNT(*) FROM store_writes WHERE status = ?1",
64                [status],
65                |row| row.get(0),
66            )
67            .map_err(DbError::from)
68    }
69
70    fn has_rebased_store_writes_for_test(&self) -> Result<bool, DbError> {
71        self.conn
72            .query_row(
73                "SELECT EXISTS(SELECT 1 FROM store_writes WHERE rebased IS NOT NULL)",
74                [],
75                |row| row.get(0),
76            )
77            .map_err(DbError::from)
78    }
79}