forge
Explore
Log in

Don't you dare go Hollow

a demotion to viewer takes the assignments with it: the role write and the sweep that owes every card a true chip are one IMMEDIATE transaction — a row left behind draws an assignee the mail audience silently skips, the card saying four people while two get the letter — and reminders are re-derived inside the same write so a pending warning names nobody just removed; a boot heal retires the rows a database from before the sweep still carries, idempotent the way the repairs around it are, and the announcements carry each touched task and queue so every open board re-renders the honest list

Browse files at this commit

2f7e959baa2723edeba6c7374039706e409fb09dAhmet Kaan GümüşParent: 0585aa5
crates/iz-core/src/store/turso_store.rsmodified+126 -8
@@ -137,6 +137,7 @@ impl TursoStore {
137 };137 };
138 store.migrate(path).await?;138 store.migrate(path).await?;
139 store.retire_actor_only_decisions().await?;139 store.retire_actor_only_decisions().await?;
140+ store.retire_viewer_assignments().await?;
140 store.encrypt_plaintext_passwords().await?;141 store.encrypt_plaintext_passwords().await?;
141 store.resniff_generic_attachments().await?;142 store.resniff_generic_attachments().await?;
142 store.sweep_orphan_files().await?;143 store.sweep_orphan_files().await?;
@@ -207,6 +208,50 @@ impl TursoStore {
207 Ok(())208 Ok(())
208 }209 }
209 210
211+ /// Assignment rows a Viewer still carries — written before a demotion
212+ /// swept them, or by a hand at the table — are a chip the mail audience
213+ /// refuses: the card says four people, two get the letter. Like the two
214+ /// repairs around it, this runs once per boot, idempotently: a database
215+ /// the pass has healed has no row left to match, and one that never
216+ /// carried any reads only the empty set. Reminders are re-derived to
217+ /// match, so a pending warning names nobody the sweep just removed.
218+ async fn retire_viewer_assignments(&self) -> Result<()> {
219+ let mut conn = self.tx_conn().await?;
220+ let tx = conn
221+ .transaction_with_behavior(TransactionBehavior::Immediate)
222+ .await
223+ .map_err(backend)?;
224+ let mut rows = tx
225+ .query(
226+ "SELECT DISTINCT a.task_id FROM task_assignee a \
227+ JOIN user u ON u.id = a.user_id WHERE u.role = ?1",
228+ params![Role::Viewer.as_str()],
229+ )
230+ .await
231+ .map_err(backend)?;
232+ let mut tasks = Vec::new();
233+ while let Some(row) = rows.next().await.map_err(backend)? {
234+ tasks.push(text(&row, 0)?);
235+ }
236+ drop(rows);
237+ if tasks.is_empty() {
238+ return Ok(());
239+ }
240+ tx.execute(
241+ "DELETE FROM task_assignee WHERE user_id IN \
242+ (SELECT id FROM user WHERE role = ?1)",
243+ params![Role::Viewer.as_str()],
244+ )
245+ .await
246+ .map_err(backend)?;
247+ let now = OffsetDateTime::now_utc();
248+ for task_id in &tasks {
249+ self.sync_task_reminders(&tx, task_id, now).await?;
250+ }
251+ tx.commit().await.map_err(backend)?;
252+ Ok(())
253+ }
254+
210 /// Re-decides the stored type of attachments whose `mime_type` is one of255 /// Re-decides the stored type of attachments whose `mime_type` is one of
211 /// [`sniff::GENERIC_MIME_TYPES`] — rows written before the sniffer could256 /// [`sniff::GENERIC_MIME_TYPES`] — rows written before the sniffer could
212 /// name OOXML files, which a file chip then renders as a plain download257 /// name OOXML files, which a file chip then renders as a plain download
@@ -682,6 +727,49 @@ impl TursoStore {
682 727
683 Ok(abandoned > 0 || minted > 0)728 Ok(abandoned > 0 || minted > 0)
684 }729 }
730+
731+ /// Every task `user_id` stops being assigned to here, with whether the
732+ /// reminder re-derivation that followed touched the queue — the pairs a
733+ /// demotion owes its readers. Deletes the rows inside the caller's
734+ /// transaction and re-derives each task's reminders to match, the same
735+ /// write `unassign_task` makes: a person leaving every card at once is
736+ /// many unassignments, not a new shape.
737+ async fn retire_assignments(
738+ &self,
739+ tx: &Transaction<'_>,
740+ user_id: &str,
741+ now: OffsetDateTime,
742+ ) -> Result<Vec<(String, bool)>> {
743+ let mut rows = tx
744+ .query(
745+ "SELECT task_id FROM task_assignee WHERE user_id = ?1",
746+ params![user_id],
747+ )
748+ .await
749+ .map_err(backend)?;
750+ let mut tasks = Vec::new();
751+ while let Some(row) = rows.next().await.map_err(backend)? {
752+ tasks.push(text(&row, 0)?);
753+ }
754+ drop(rows);
755+ if tasks.is_empty() {
756+ return Ok(Vec::new());
757+ }
758+ // Before the resync: it reads the assignee list, and the person being
759+ // retired must not be minted a reminder on their way out.
760+ tx.execute(
761+ "DELETE FROM task_assignee WHERE user_id = ?1",
762+ params![user_id],
763+ )
764+ .await
765+ .map_err(backend)?;
766+ let mut touched = Vec::with_capacity(tasks.len());
767+ for task_id in tasks {
768+ let queue = self.sync_task_reminders(tx, &task_id, now).await?;
769+ touched.push((task_id, queue));
770+ }
771+ Ok(touched)
772+ }
685 }773 }
686 774
687 const RULE_COLUMNS: &str = "id, board_id, trigger_kind, trigger_column, subject, audience, \775 const RULE_COLUMNS: &str = "id, board_id, trigger_kind, trigger_column, subject, audience, \
@@ -1871,10 +1959,16 @@ impl Store for TursoStore {
1871 Ok(())1959 Ok(())
1872 }1960 }
1873 }1961 }
1874-
1875 async fn set_role(&self, user_id: &str, role: Role) -> Result<()> {1962 async fn set_role(&self, user_id: &str, role: Role) -> Result<()> {
1876- let conn = self.conn.lock().await;1963+ // IMMEDIATE: a demotion's role write and the assignment sweep it owes
1877- let n = conn1964+ // are one write set — a card that renders between the two would show
1965+ // a chip the mail audience already refuses.
1966+ let mut conn = self.tx_conn().await?;
1967+ let tx = conn
1968+ .transaction_with_behavior(TransactionBehavior::Immediate)
1969+ .await
1970+ .map_err(backend)?;
1971+ let n = tx
1878 .execute(1972 .execute(
1879 "UPDATE user SET role = ?1 WHERE id = ?2",1973 "UPDATE user SET role = ?1 WHERE id = ?2",
1880 params![role.as_str(), user_id],1974 params![role.as_str(), user_id],
@@ -1882,12 +1976,36 @@ impl Store for TursoStore {
1882 .await1976 .await
1883 .map_err(backend)?;1977 .map_err(backend)?;
1884 if n == 0 {1978 if n == 0 {
1885- Err(StoreError::NotFound)1979+ return Err(StoreError::NotFound);
1886- } else {1980+ }
1887- drop(conn);1981+ let mut topics = vec![Topic::Members];
1888- self.announce([Topic::Members]);1982+ // A Viewer cannot be assigned, so a demotion to one is also every
1889- Ok(())1983+ // unassignment: a row left behind draws a chip the mail audience
1984+ // silently skips — the card saying four people while two get the
1985+ // letter.
1986+ if role == Role::Viewer {
1987+ match self
1988+ .retire_assignments(&tx, user_id, OffsetDateTime::now_utc())
1989+ .await
1990+ {
1991+ Ok(touched) => {
1992+ for (task_id, queue) in touched {
1993+ topics.push(Topic::Task(task_id));
1994+ if queue {
1995+ topics.push(Topic::Queue);
1996+ }
1997+ }
1998+ }
1999+ Err(e) => {
2000+ let _ = tx.rollback().await;
2001+ return Err(e);
2002+ }
2003+ }
1890 }2004 }
2005+ tx.commit().await.map_err(backend)?;
2006+ drop(conn);
2007+ self.announce(topics);
2008+ Ok(())
1891 }2009 }
1892 2010
1893 async fn set_user_disabled(&self, user_id: &str, disabled: bool) -> Result<()> {2011 async fn set_user_disabled(&self, user_id: &str, disabled: bool) -> Result<()> {
crates/iz-core/tests/store.rsmodified+70 -0
@@ -8257,6 +8257,76 @@ async fn losing_the_admin_flag_drops_an_admin_to_member_and_nothing_else() {
8257 .unwrap();8257 .unwrap();
8258 assert_eq!(still.role, Role::Viewer);8258 assert_eq!(still.role, Role::Viewer);
8259 }8259 }
8260+
8261+/// A demotion to Viewer is every unassignment at once: the role cannot be
8262+/// assigned, and a row left behind is a chip on the card that the mail
8263+/// audience silently skips — the card says four people, two get the letter.
8264+#[tokio::test]
8265+async fn demoting_a_member_to_viewer_takes_their_assignments_with_them() {
8266+ let (dir, store, workspace, admin) = shared().await;
8267+ let kept = member(&store, &workspace, "emre@iz.sh", "Emre").await;
8268+ let demoted = member(&store, &workspace, "grace@iz.sh", "Grace").await;
8269+ let first = add_task(&store, &workspace, "Backlog", "First", None, &admin).await;
8270+ let second = add_task(&store, &workspace, "Backlog", "Second", None, &admin).await;
8271+ for task in [&first, &second] {
8272+ store.assign_task(task, &kept).await.unwrap();
8273+ store.assign_task(task, &demoted).await.unwrap();
8274+ }
8275+
8276+ store.set_role(&demoted, Role::Viewer).await.unwrap();
8277+
8278+ for task in [&first, &second] {
8279+ let chips = store.assignees_for_task(task).await.unwrap();
8280+ assert_eq!(
8281+ chips.iter().map(|p| p.display_name.as_str()).collect::<Vec<_>>(),
8282+ ["Emre"],
8283+ "the demoted person is still a chip on {task}"
8284+ );
8285+ let mailed = store.recipients_for_task(task).await.unwrap();
8286+ assert_eq!(
8287+ mailed.iter().map(|p| p.email.as_str()).collect::<Vec<_>>(),
8288+ ["emre@iz.sh"],
8289+ "task {task} reads two audiences"
8290+ );
8291+ }
8292+ let _ = std::fs::remove_dir_all(&dir);
8293+}
8294+
8295+/// A database from before demotions swept assignments — or a hand at the
8296+/// table — can carry a Viewer's rows. The boot heal takes them, so the two
8297+/// reads agree from the first render after the deploy.
8298+#[tokio::test]
8299+async fn a_boot_leaves_no_viewer_still_assigned() {
8300+ let dir = std::env::temp_dir().join(format!("iz-test-{}", Ulid::new()));
8301+ std::fs::create_dir_all(&dir).unwrap();
8302+ let path = dir.join("iz.db").to_string_lossy().into_owned();
8303+ let store = TursoStore::open(&path, &dir.join("storage")).await.unwrap();
8304+ let (workspace, admin) = claim(&store).await;
8305+ let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await;
8306+ let task = add_task(&store, &workspace, "Backlog", "Ship it", None, &admin).await;
8307+ store.assign_task(&task, &mate).await.unwrap();
8308+ drop(store);
8309+
8310+ // The broken state itself: a role write that swept nothing, the shape a
8311+ // database written before the sweep carries.
8312+ let db = turso::Builder::new_local(&path).build().await.unwrap();
8313+ let conn = db.connect().unwrap();
8314+ conn.execute("PRAGMA foreign_keys = ON", ()).await.unwrap();
8315+ conn.execute(
8316+ "UPDATE user SET role = 'viewer' WHERE id = ?1",
8317+ turso::params![mate],
8318+ )
8319+ .await
8320+ .unwrap();
8321+
8322+ let reopened = TursoStore::open(&path, &dir.join("storage")).await.unwrap();
8323+ assert!(
8324+ reopened.assignees_for_task(&task).await.unwrap().is_empty(),
8325+ "the boot kept a chip the mail audience refuses"
8326+ );
8327+ drop(reopened);
8328+ let _ = std::fs::remove_dir_all(&dir);
8329+}
8260 #[tokio::test]8330 #[tokio::test]
8261 async fn a_provision_announces_only_when_the_row_changes() {8331 async fn a_provision_announces_only_when_the_row_changes() {
8262 let scratch = Scratch::open().await;8332 let scratch = Scratch::open().await;