From a4102ee810a2c1f3638974a148ee003636db0c5e Mon Sep 17 00:00:00 2001 From: Luke Street Date: Mon, 30 Sep 2024 20:08:12 -0600 Subject: [PATCH] Allow duplicate units within a report --- migrations/10_report_report_units_index.sql | 28 +++++++++++++++++++++ src/db/mod.rs | 4 +-- src/handlers/project.rs | 10 ++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 migrations/10_report_report_units_index.sql diff --git a/migrations/10_report_report_units_index.sql b/migrations/10_report_report_units_index.sql new file mode 100644 index 0000000..c41562f --- /dev/null +++ b/migrations/10_report_report_units_index.sql @@ -0,0 +1,28 @@ +PRAGMA foreign_keys = off; + +ALTER TABLE report_report_units + RENAME TO report_report_units_old; + +CREATE TABLE report_report_units +( + report_id INTEGER NOT NULL, + report_unit_id BLOB NOT NULL, + unit_index INTEGER NOT NULL, -- Index of the report unit in the report + PRIMARY KEY (report_id, report_unit_id, unit_index), + FOREIGN KEY (report_id) REFERENCES reports (id), + FOREIGN KEY (report_unit_id) REFERENCES report_units (id) +); + +DROP INDEX report_report_units_report_id_index; +DROP INDEX report_report_units_report_unit_id_index; + +CREATE INDEX report_report_units_report_id_index ON report_report_units (report_id); +CREATE INDEX report_report_units_report_unit_id_index ON report_report_units (report_unit_id); + +INSERT INTO report_report_units +SELECT * +FROM report_report_units_old; + +DROP TABLE report_report_units_old; + +PRAGMA foreign_keys = on; \ No newline at end of file diff --git a/src/db/mod.rs b/src/db/mod.rs index ac64285..a11be2c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -223,13 +223,13 @@ impl Database { { let idx = row.unit_index as usize; if idx != report.units.len() { - bail!("Report unit index mismatch"); + bail!("Report unit index mismatch: {} but expected {}", idx, report.units.len()); } let key: UnitKey = row.id.as_slice().try_into()?; let data = decompress(&row.data).context("Failed to decompress report unit data")?; let hash: UnitKey = blake3::hash(data.as_ref()).into(); if hash != key { - bail!("Report unit data hash mismatch"); + bail!("Report unit data hash mismatch for unit {}", idx); } let unit = ReportUnit::decode(data.as_ref()).context("Failed to decode report unit")?; report.units.push(unit); diff --git a/src/handlers/project.rs b/src/handlers/project.rs index 78d0cb1..5a1255a 100644 --- a/src/handlers/project.rs +++ b/src/handlers/project.rs @@ -1,6 +1,6 @@ use std::{sync::Arc, time::Instant}; -use anyhow::anyhow; +use anyhow::{anyhow, Context}; use axum::{ extract::{Query, State}, http::StatusCode, @@ -88,7 +88,13 @@ pub async fn get_projects( let report = state .db .get_report(&info.project.owner, &info.project.repo, &info.commit.sha, version) - .await; + .await + .with_context(|| { + format!( + "Failed to fetch report for {}/{} sha {} version {}", + info.project.owner, info.project.repo, info.commit.sha, version + ) + }); (info, report) }); }