Skip to main content

tuwunel_service/migrations/
split_conduit_highlight_counts.rs

1use tuwunel_core::{Result, info, utils::TryReadyExt};
2use tuwunel_database::Map;
3
4use super::tally::{Outcome, Tally};
5use crate::{
6	Services,
7	integrity::userroomid_highlightcount::{Finding, scan},
8};
9
10/// Splits a Conduit database's conflated highlight-count column.
11///
12/// Conduit opens `roomuserid_lastnotificationread` against the
13/// `userroomid_highlightcount` tree, a copy-paste in its schema, so one column
14/// holds both stores. tuwunel keeps them apart under the same byte layouts, so
15/// each room-keyed row moves verbatim into its own column and the user-keyed
16/// highlight counts stay. A native database has no room-keyed row to move, so
17/// it pays one row of the walk and takes no cork.
18pub(super) async fn split_conduit_highlight_counts(services: &Services) -> Result {
19	let db = &services.db;
20	let highlight = &db["userroomid_highlightcount"];
21	let lastread = &db["roomuserid_lastnotificationread"];
22
23	// The cork is taken at the first finding and held to the end of the walk.
24	let (tally, cork) = scan(services)
25		.ready_try_fold((Tally::default(), None), |(tally, cork), finding| {
26			let cork = cork.or_else(|| Some(db.cork_and_sync()));
27
28			Ok((tally.record(repair(highlight, lastread, &finding)), cork))
29		})
30		.await?;
31
32	drop(cork);
33
34	if tally.repaired > 0 {
35		info!(
36			moved = %tally.repaired,
37			"Split Conduit last-notification-read rows out of the highlight-count column"
38		);
39	}
40
41	Ok(())
42}
43
44/// Moves the row a finding names into the last-read column.
45///
46/// The key alone decides, and the value moved is the one the walk read. The
47/// insert comes first, so a stop between the two writes leaves the row to move
48/// again on the next boot.
49fn repair(highlight: &Map, lastread: &Map, Finding { key, value }: &Finding) -> Outcome {
50	lastread.insert(key, value);
51	highlight.remove(key);
52
53	Outcome::Repaired
54}