BigPizzaV3/CodexPlusPlus · error

restore conflict: row already exists

Error message

restore conflict: {table} row already exists

What it means

detect_restore_conflicts pre-flights every row of a restore payload against the live database; if restore_row_conflicts finds an existing row with the same key (and conflicting content), it bails before any restore is applied. Restore is intentionally non-destructive: it refuses to overwrite existing rows instead of silently clobbering live data.

Solutions

  1. Delete or dedupe the conflicting rows from the target DB first, or verify the row content matches and skip it in the payload.
  2. Don't run the same restore/undo twice; track completed tokens in your undo history.
  3. Filter the backup's tables/rows to exclude ids that already exist in the live DB before calling restore_backups.
  4. If the intent is overwrite, remove the existing rows (or truncate the table) explicitly within a transaction, then restore.

Example fix

// before
storage.restore_backups(&backup)?; // thread id "abc" already exists in threads table
// after
let existing: Vec<String> = load_existing_thread_ids(&db_path)?;
let backup = filter_backup_rows(&backup, |table, row| {
    let id = row["id"].as_str().unwrap_or_default();
    !(table == "threads" && existing.iter().any(|e| e == id))
});
storage.restore_backups(&backup)?;
Defensive patterns

Strategy: validation

Validate before calling

for (table, rows) in backup["tables"].as_object().unwrap() {
    for row in rows.as_array().unwrap_or(&vec![]) {
        if row_exists(&db_path, table, row)? {
            anyhow::bail!("pre-check: {table} row already exists; dedupe or remove before restore");
        }
    }
}

Type guard

fn restore_is_clean(db: &Connection, backup: &Value) -> bool {
    detect_restore_conflicts(db, backup["tables"].as_object().unwrap_or(&Map::new())).is_ok()
}

Try / catch

match storage.restore_backups(&backup) {
    Ok(()) => {}
    Err(e) if e.to_string().contains("row already exists") => {
        eprintln!("{e}; skipping rows already present (restore is non-destructive)");
        let filtered = remove_conflicting_rows(&backup, &db_path)?;
        storage.restore_backups(&filtered)?;
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling restore_backups when the target DB already contains a row matching a backup row's primary key (e.g. same thread/session id) — the row already exists, so restoring would duplicate or overwrite it.

Common situations: Running undo/restore twice (second run sees rows restored by the first); the deleted thread still exists in the DB so undoing its deletion collides; other threads/databases sharing rows across grouped restores (preflight detects the conflict in a later DB).

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/e814f371d047fab7. Report an issue: GitHub.

Appendix: source

Thrown at crates/codex-plus-data/src/storage.rs:1115

        }
    }
    Ok(())
}

fn detect_restore_conflicts(db: &Connection, tables: &Map<String, Value>) -> anyhow::Result<()> {
    for (table, rows) in tables {
        if table.starts_with("__") {
            continue;
        }
        let Some(rows) = rows.as_array() else {
            continue;
        };
        for row in rows {
            let Some(row) = row.as_object() else {
                continue;
            };
            if restore_row_conflicts(db, table, row)? {
                anyhow::bail!("restore conflict: {table} row already exists");
            }
        }
    }
    Ok(())
}

fn restore_row_conflicts(
    db: &Connection,
    table: &str,
    row: &Map<String, Value>,
) -> anyhow::Result<bool> {
    let key_columns = restore_conflict_key_columns(table, row);
    if key_columns.is_empty() || !has_table(db, table)? {
        return Ok(false);
    }
    let where_clause = key_columns
        .iter()
        .enumerate()

View on GitHub (pinned to b1ed92e5e4)