Fix report deletion

Resolves #12
This commit is contained in:
Luke Street
2025-09-12 23:26:08 -06:00
parent 1a006770ad
commit 76d390f073
5 changed files with 33 additions and 21 deletions
+2 -16
View File
@@ -806,7 +806,6 @@ impl Database {
pub async fn cleanup_report_units(&self) -> Result<()> {
let mut conn = self.pool.acquire().await?;
conn.execute("PRAGMA foreign_keys = OFF").await?;
let mut tx = conn.begin().await?;
let deleted_reports = sqlx::query!(
r#"
@@ -817,15 +816,6 @@ impl Database {
.execute(&mut *tx)
.await?
.rows_affected();
let deleted_report_report_units = sqlx::query!(
r#"
DELETE FROM report_report_units
WHERE report_id NOT IN (SELECT id FROM reports)
"#,
)
.execute(&mut *tx)
.await?
.rows_affected();
let deleted_report_units = sqlx::query!(
r#"
DELETE FROM report_units
@@ -836,13 +826,11 @@ impl Database {
.await?
.rows_affected();
tx.commit().await?;
conn.execute("PRAGMA foreign_keys = ON").await?;
if deleted_reports > 0 || deleted_report_units > 0 || deleted_report_report_units > 0 {
if deleted_reports > 0 || deleted_report_units > 0 {
tracing::info!(
"Deleted {} orphaned reports, {} orphaned report units and {} orphaned mappings",
"Deleted {} orphaned reports and {} orphaned report units",
deleted_reports,
deleted_report_units,
deleted_report_report_units,
);
}
Ok(())
@@ -1112,7 +1100,6 @@ impl Database {
pub async fn cleanup_images(&self) -> Result<()> {
let mut conn = self.pool.acquire().await?;
conn.execute("PRAGMA foreign_keys = OFF").await?;
let mut tx = conn.begin().await?;
let deleted_images = sqlx::query!(
r#"
@@ -1127,7 +1114,6 @@ impl Database {
.await?
.rows_affected();
tx.commit().await?;
conn.execute("PRAGMA foreign_keys = ON").await?;
if deleted_images > 0 {
tracing::info!("Deleted {} orphaned images", deleted_images);
}
+1 -1
View File
@@ -232,7 +232,7 @@ fn output_line(line: &ChangeLine, out: &mut String) {
fn generate_changes_list(changes: Vec<ChangeLine>, out: &mut String) {
let mut changes_by_kind = BTreeMap::new();
for change in changes {
changes_by_kind.entry(change.kind.clone()).or_insert(vec![]).push(change);
changes_by_kind.entry(change.kind).or_insert(vec![]).push(change);
}
for (change_kind, mut changes) in changes_by_kind {
let total_changes = changes.len();
+1 -1
View File
@@ -131,7 +131,7 @@ pub async fn get_image(
fn extract_extension(params: ImageParams) -> (ImageParams, Option<String>) {
if let Some((id, ext)) = params.id.rsplit_once('.') {
return (ImageParams { id: id.to_string(), ..params }, Some(ext.to_string()));
return (ImageParams { id: id.to_string() }, Some(ext.to_string()));
}
(params, None)
}
+1 -3
View File
@@ -694,9 +694,7 @@ pub async fn delete_commit(
session: Session,
Form(form): Form<DeleteCommitForm>,
) -> Result<Response, AppError> {
let Some(info) =
state.db.get_project_info(&params.owner, &params.repo, None).await?
else {
let Some(info) = state.db.get_project_info(&params.owner, &params.repo, None).await? else {
return Err(AppError::Status(StatusCode::NOT_FOUND));
};
if !current_user.can_manage_repo(info.project.id) {
+28
View File
@@ -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) ON DELETE CASCADE,
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;