Merge 'Fix off-by-one error in max_frame after WAL load' from Jussi Saurio

🤦

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>
Reviewed-by: Preston Thorpe (@PThorpe92)

Closes #1572
This commit is contained in:
Jussi Saurio
2025-05-25 20:25:50 +03:00
2 changed files with 33 additions and 1 deletions

View File

@@ -485,4 +485,34 @@ mod tests {
Ok(())
}
#[tokio::test]
async fn test_database_persistence_write_one_frame_many_times() -> Result<()> {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path().to_str().unwrap();
for i in 0..100 {
{
let db = Builder::new_local(db_path).build().await?;
let conn = db.connect()?;
conn.execute("CREATE TABLE IF NOT EXISTS test_persistence (id INTEGER PRIMARY KEY, name TEXT NOT NULL);", ()).await?;
conn.execute("INSERT INTO test_persistence (name) VALUES ('Alice');", ())
.await?;
}
{
let db = Builder::new_local(db_path).build().await?;
let conn = db.connect()?;
let mut rows_iter = conn
.query("SELECT count(*) FROM test_persistence;", ())
.await?;
let rows = rows_iter.next().await?.unwrap();
assert_eq!(rows.get_value(0)?, Value::Integer(i as i64 + 1));
assert!(rows_iter.next().await?.is_none());
}
}
Ok(())
}
}

View File

@@ -1512,7 +1512,9 @@ pub fn read_entire_wal_dumb(file: &Arc<dyn File>) -> Result<Arc<UnsafeCell<WalFi
current_offset += WAL_FRAME_HEADER_SIZE + page_size;
}
wfs_data.max_frame.store(frame_idx, Ordering::SeqCst);
wfs_data
.max_frame
.store(frame_idx.saturating_sub(1), Ordering::SeqCst);
wfs_data.last_checksum = cumulative_checksum;
wfs_data.loaded.store(true, Ordering::SeqCst);
});