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:
@@ -41,8 +41,10 @@ pub async fn create_snapshot(
|
|||||||
.map(|t| (t.schema.clone(), t.table.clone()))
|
.map(|t| (t.schema.clone(), t.table.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if params.include_dependencies {
|
// Fetch FK info once — used for both dependency expansion and topological sort
|
||||||
let fk_rows = fetch_foreign_keys_raw(&pool).await?;
|
let fk_rows = fetch_foreign_keys_raw(&pool).await?;
|
||||||
|
|
||||||
|
if params.include_dependencies {
|
||||||
for fk in &fk_rows {
|
for fk in &fk_rows {
|
||||||
for (schema, table) in ¶ms.tables.iter().map(|t| (t.schema.clone(), t.table.clone())).collect::<Vec<_>>() {
|
for (schema, table) in ¶ms.tables.iter().map(|t| (t.schema.clone(), t.table.clone())).collect::<Vec<_>>() {
|
||||||
if &fk.schema == schema && &fk.table == table {
|
if &fk.schema == schema && &fk.table == table {
|
||||||
@@ -56,7 +58,6 @@ pub async fn create_snapshot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FK-based topological sort
|
// FK-based topological sort
|
||||||
let fk_rows = fetch_foreign_keys_raw(&pool).await?;
|
|
||||||
let fk_edges: Vec<(String, String, String, String)> = fk_rows
|
let fk_edges: Vec<(String, String, String, String)> = fk_rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|fk| (fk.schema.clone(), fk.table.clone(), fk.ref_schema.clone(), fk.ref_table.clone()))
|
.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;
|
let mut total_rows: u64 = 0;
|
||||||
|
|
||||||
for (i, (schema, table)) in sorted_tables.iter().enumerate() {
|
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(
|
let _ = app.emit(
|
||||||
"snapshot-progress",
|
"snapshot-progress",
|
||||||
SnapshotProgress {
|
SnapshotProgress {
|
||||||
@@ -249,7 +250,7 @@ pub async fn restore_snapshot(
|
|||||||
continue;
|
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(
|
let _ = app.emit(
|
||||||
"snapshot-progress",
|
"snapshot-progress",
|
||||||
SnapshotProgress {
|
SnapshotProgress {
|
||||||
|
|||||||
Reference in New Issue
Block a user