//! Deleting a CI run reads that run's jobs, not every job in the file.//!//! `cotton_repo_jobs.run_id` is a foreign key to `cotton_repo_runs`, and//! SQLite checks a parent's delete by looking for children through an index//! that leads with the key's column. Until schema 94 the only index on the//! jobs led with `workspace_id`, so every deleted run read every job — and the//! prune erases runs by the thousand.use cotton_store::fixtures::{file_at_version, seed_workspace_row, NOW, WORKSPACE};use cotton_store::schema::ddl::SCHEMA_VERSION;use cotton_store::Driver;use rusqlite::types::ValueasSqlValue;const REPO: &str = "00000000-0000-4000-8000-00000000r001";/// A file with one repository and `runs` finished runs of three jobs each.fn file_with(runs: usize) -> Driver {let db = file_at_version(SCHEMA_VERSION); seed_workspace_row(&db);let text = |s: &str| SqlValue::Text(s.to_string()); db.run("INSERT INTO cotton_repos (id, workspace_id, key, host, owner, name, kind, web_url, held, created_at, updated_at) \ VALUES (?, ?, 'cotton:example/repo', 'cotton', 'example', 'repo', 'cotton', 'https://code.example.test/example/repo', 1, ?, ?)", &[text(REPO), text(WORKSPACE), text(NOW), text(NOW)], ) .expect("a repository"); db.transaction(|db| {for n in0..runs {let run = format!("run-{n:06}"); db.run("INSERT INTO cotton_repo_runs (id, workspace_id, repo_id, index_in_repo, workflow, event, ref, sha, status, conclusion, created_at, updated_at) \ VALUES (?, ?, ?, ?, 'ci.yml', 'push', 'refs/heads/main', ?, 'completed', 'success', ?, ?)", &[text(&run), text(WORKSPACE), text(REPO), SqlValue::Integer(n as i64 + 1), text(&"c".repeat(40)), text(NOW), text(NOW)], )?;for job in0..3 { db.run("INSERT INTO cotton_repo_jobs (id, workspace_id, run_id, index_in_run, task_id, name, runs_on, status, conclusion, created_at, updated_at) \ VALUES (?, ?, ?, ?, ?, 'check', 'macos', 'completed', 'success', ?, ?)", &[text(&format!("job-{n:06}-{job}")), text(WORKSPACE), text(&run), SqlValue::Integer(job + 1), SqlValue::Integer(n as i64 * 3 + job + 1), text(NOW), text(NOW)], )?; } }Ok(()) }) .expect("the runs"); db}/// The steps one run's delete reads, its own jobs deleted first (as an erase/// takes them: children before the parent).fn steps_to_delete_a_run(db: &Driver) -> u64 {let run = [SqlValue::Text("run-000007".to_string())]; assert_eq!(db.run("DELETE FROM cotton_repo_jobs WHERE run_id = ?", &run).expect("its jobs"), 3);let before = db.steps(); assert_eq!(db.run("DELETE FROM cotton_repo_runs WHERE id = ?", &run).expect("the run"), 1); db.steps() - before}/// RULE: Deleting a cotton_repo_runs row reads its own jobs through an index that leads with run_id, so it costs the same in a file of 30,000 jobs as in one of 300 — never a read of every job./// Measured 11 Oct 2026 on this fixture, in SQLite's own steps (`Driver::steps`): 45 with the index among 300 jobs and 45 among 30,000; 90,035 among 30,000 without it (three steps a job, every job). On the owner's serving Mac (22,151 jobs) that was about 66,000 steps for each of the thousands of runs a prune erases.#[test]fn deleting_a_run_reads_its_own_jobs_and_never_the_table() {let small = steps_to_delete_a_run(&file_with(100));let large = steps_to_delete_a_run(&file_with(10_000)); assert!( large <= small + small / 2,"a run's delete read {large} steps among 30,000 jobs and {small} among 300" );// And the measure sees the read it guards against: without the index the// same delete reads every job.let unindexed = file_with(10_000); unindexed.exec("DROP INDEX idx_cotton_repo_jobs_run_id").expect("the index is there to drop");let scanned = steps_to_delete_a_run(&unindexed); assert!(scanned > large * 100, "without the index the delete read {scanned} steps, with it {large}");}