fix: prevent snapshot hang caused by u8 overflow in progress calculation

When creating/restoring snapshots with 4+ tables, the progress percentage
calculation overflowed u8 (e.g. 4*80=320 > 255), causing a panic that left
the IPC call unresolved. Also deduplicate fetch_foreign_keys_raw call.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-21 17:45:58 +03:00
parent a3b05b0328
commit 34c80809f1

View File

@@ -41,8 +41,10 @@ pub async fn create_snapshot(
.map(|t| (t.schema.clone(), t.table.clone()))
.collect();
// Fetch FK info once — used for both dependency expansion and topological sort
let fk_rows = fetch_foreign_keys_raw(&pool).await?;
if params.include_dependencies {
let fk_rows = fetch_foreign_keys_raw(&pool).await?;
for fk in &fk_rows {
for (schema, table) in &params.tables.iter().map(|t| (t.schema.clone(), t.table.clone())).collect::<Vec<_>>() {
if &fk.schema == schema && &fk.table == table {
@@ -56,7 +58,6 @@ pub async fn create_snapshot(
}
// FK-based topological sort
let fk_rows = fetch_foreign_keys_raw(&pool).await?;
let fk_edges: Vec<(String, String, String, String)> = fk_rows
.iter()
.map(|fk| (fk.schema.clone(), fk.table.clone(), fk.ref_schema.clone(), fk.ref_table.clone()))
@@ -75,7 +76,7 @@ pub async fn create_snapshot(
let mut total_rows: u64 = 0;
for (i, (schema, table)) in sorted_tables.iter().enumerate() {
let percent = 10 + ((i as u8) * 80 / total_tables.max(1) as u8);
let percent = (10 + (i * 80 / total_tables.max(1))).min(90) as u8;
let _ = app.emit(
"snapshot-progress",
SnapshotProgress {
@@ -249,7 +250,7 @@ pub async fn restore_snapshot(
continue;
}
let percent = 20 + ((i as u8) * 75 / total_tables.max(1) as u8);
let percent = (20 + (i * 75 / total_tables.max(1))).min(95) as u8;
let _ = app.emit(
"snapshot-progress",
SnapshotProgress {