Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(storage): recover interrupted migration retries safely (#7109)

Ezra Ellette3c0ac07a24c3
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs b/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs
index 0932af73a..17da6769a 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/feedback_upload.rs
@@ -311,6 +311,10 @@ async fn collect_migration_diagnostics(app: &AppHandle) -> String {
let Some(root) = root else {
return "[Storage directory unavailable]".into();
};
+ collect_migration_diagnostics_from_root(root).await
+}
+
+async fn collect_migration_diagnostics_from_root(root: std::path::PathBuf) -> String {
match timeout(
DIAGNOSTIC_PROBE_TIMEOUT,
tokio::task::spawn_blocking(move || screenpipe_db::storage::diagnostics::recent(&root)),
@@ -738,6 +742,108 @@ mod tests {
}
}
+ #[tokio::test]
+ async fn migration_conflict_reaches_support_after_log_rotation_and_redaction() {
+ use screenpipe_db::{storage, DatabaseManager};
+ let root = tempfile::tempdir().unwrap();
+ let source = root.path().join("db.sqlite");
+ let db = DatabaseManager::new(source.to_str().unwrap(), Default::default())
+ .await
+ .unwrap();
+ db.execute_raw_sql_write(
+ "INSERT INTO frames(id,timestamp,full_text) VALUES(1,'2026-09-18','private history')",
+ )
+ .await
+ .unwrap();
+ db.close().await;
+ let report = storage::migrate(root.path(), Default::default(), Default::default())
+ .await
+ .unwrap();
+ let descriptor = storage::StorageDescriptor::read(root.path())
+ .unwrap()
+ .unwrap();
+ let extra = root.path().join("extra.sqlite");
+ let db = DatabaseManager::new(extra.to_str().unwrap(), Default::default())
+ .await
+ .unwrap();
+ db.execute_raw_sql_write("INSERT INTO frames(id,timestamp,full_text) VALUES(2,'2026-09-18','additional private history')").await.unwrap();
+ db.close().await;
+ std::fs::rename(extra, &source).unwrap();
+ std::fs::write(
+ root.path().join("storage-migration.json"),
+ serde_json::to_vec(&json!({
+ "format": 2, "phase": "paused", "descriptor": descriptor,
+ "source": report.tables, "snapshot": null, "report": null
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+ let error = storage::migrate(root.path(), Default::default(), Default::default())
+ .await
+ .unwrap_err();
+ assert!(error
+ .to_string()
+ .contains("source contains recorded history"));
+ timeout(Duration::from_secs(3), async {
+ loop {
+ if storage::diagnostics::recent(root.path())
+ .unwrap_or_default()
+ .iter()
+ .any(|snapshot| snapshot.status == "failed")
+ {
+ break;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap();
+ // A later support submission has no original rolling log in memory or on disk.
+ let diagnostics = collect_migration_diagnostics_from_root(root.path().to_owned()).await;
+ let raw =
+ format!("[no log files found]\n\n=== Storage Migration Diagnostics ===\n{diagnostics}");
+ let redacted = crate::feedback_redact::redact_pii_for_feedback(raw, "{}".into())
+ .await
+ .unwrap();
+ assert!(redacted.contains("source contains recorded history"));
+ assert!(redacted.contains("validating_migration_source"));
+ assert!(redacted.contains("\"source_exists\": true"));
+ assert!(redacted.contains("\"index_exists\": true"));
+ assert!(!redacted.contains("private history"));
+
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/api/logs"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"data": {
+ "signedUrl": format!("{}/upload/log", server.uri()), "path": "logs/report.log"
+ }})))
+ .mount(&server)
+ .await;
+ Mock::given(method("PUT"))
+ .and(path("/upload/log"))
+ .respond_with(ResponseTemplate::new(200))
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/api/logs/confirm"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"data": {"id": 42}})))
+ .mount(&server)
+ .await;
+ upload_report(
+ &Client::new(),
+ &server.uri(),
+ &request(),
+ redacted.clone(),
+ None,
+ None,
+ )
+ .await
+ .unwrap();
+ let requests = server.received_requests().await.unwrap();
+ let upload = requests.iter().find(|r| r.method == "PUT").unwrap();
+ assert_eq!(upload.body, redacted.as_bytes());
+ }
+
#[test]
fn validates_job_identity_and_video_source() {
let mut input = request();
diff --git a/crates/screenpipe-db/src/storage/diagnostics.rs b/crates/screenpipe-db/src/storage/diagnostics.rs
index 24a3e7fe9..0782cf6ff 100644
--- a/crates/screenpipe-db/src/storage/diagnostics.rs
+++ b/crates/screenpipe-db/src/storage/diagnostics.rs
@@ -26,6 +26,19 @@ tokio::task_local! {
}
/// Contains counters and operation names only, never captured text or SQL.
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct SourceState {
+ pub journal_phase: String,
+ pub source_exists: bool,
+ pub index_exists: bool,
+ pub active_descriptor_present: bool,
+ pub active_descriptor_matches: bool,
+ pub expected_source_bytes: Option<u64>,
+ pub actual_source_bytes: Option<u64>,
+ pub source_modified_matches: Option<bool>,
+ pub source_file_id_matches: Option<bool>,
+}
+
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Snapshot {
pub schema_version: u32,
@@ -51,6 +64,8 @@ pub struct Snapshot {
pub idle_seconds: u64,
pub failure_stage: Option<String>,
pub error: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub source_state: Option<SourceState>,
}
struct Run {
@@ -95,6 +110,15 @@ pub fn app_version(version: String) {
});
}
+pub(super) fn source_state(state: SourceState) {
+ let _ = ACTIVE.try_with(|run| {
+ run.lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .snapshot
+ .source_state = Some(state);
+ });
+}
+
pub(super) fn batch(
table: &str,
first: Option<i64>,
@@ -214,6 +238,7 @@ async fn observe_with_timing<T, E: Display>(
idle_seconds: 0,
failure_stage: None,
error: None,
+ source_state: None,
},
started,
advanced: started,
diff --git a/crates/screenpipe-db/src/storage/lifecycle.rs b/crates/screenpipe-db/src/storage/lifecycle.rs
index 6c5ce81ef..2e798dc79 100644
--- a/crates/screenpipe-db/src/storage/lifecycle.rs
+++ b/crates/screenpipe-db/src/storage/lifecycle.rs
@@ -153,6 +153,137 @@ fn read_journal(root: &Path) -> Result<Journal, sqlx::Error> {
Ok(journal)
}
+/// Inspect the journal's actual files before opening a manager, which may create
+/// a missing SQLite file. Never infer the source from descriptor publication:
+/// recovery can stop between its durable paused journal and storage.json.
+async fn reconcile_migration_source(root: &Path, journal: &mut Journal) -> Result<(), sqlx::Error> {
+ super::diagnostics::stage("validating_migration_source");
+ let source = source_identity(root)?;
+ let index = checked_path(root, &journal.descriptor.index)?;
+ let active = StorageDescriptor::read(root)?;
+ super::diagnostics::source_state(super::diagnostics::SourceState {
+ journal_phase: serde_json::to_value(&journal.phase)
+ .map_err(storage_error)?
+ .as_str()
+ .unwrap()
+ .to_owned(),
+ source_exists: source.is_some(),
+ index_exists: index.exists(),
+ active_descriptor_present: active.is_some(),
+ active_descriptor_matches: active.as_ref() == Some(&journal.descriptor),
+ expected_source_bytes: journal.snapshot.as_ref().map(|s| s.bytes),
+ actual_source_bytes: source.as_ref().map(|s| s.bytes),
+ source_modified_matches: source
+ .as_ref()
+ .zip(journal.snapshot.as_ref())
+ .map(|(a, b)| a.modified == b.modified),
+ source_file_id_matches: source
+ .as_ref()
+ .and_then(|s| s.file_id)
+ .zip(journal.snapshot.as_ref().and_then(|s| s.file_id))
+ .map(|(a, b)| a == b),
+ });
+ if active.as_ref().is_some_and(|d| d != &journal.descriptor) {
+ return Err(storage_error(
+ "active descriptor differs from migration index; both files have been kept",
+ ));
+ }
+ if !index.is_file() {
+ if active.is_some() || source.is_none() {
+ return Err(storage_error(
+ "migration index is missing; source files have been kept",
+ ));
+ }
+ return Ok(());
+ }
+ if source.is_none() {
+ return Ok(());
+ }
+
+ // Older explicit retries could create an empty legacy database while the
+ // real index was awaiting descriptor publication. Only retire that empty
+ // artifact, and only beside an index with this journal's stored identity.
+ crate::db::register_sqlite_extensions()?;
+ let _index_owner = screenpipe_sqlite_coordinator::acquire_sqlite_manager_lease(&index)
+ .map_err(storage_error)?;
+ let mut conn = sqlx::SqliteConnection::connect_with(
+ &sqlx::sqlite::SqliteConnectOptions::new()
+ .filename(&index)
+ .read_only(true)
+ .create_if_missing(false),
+ )
+ .await?;
+ let stored = sqlx::query_scalar::<_, String>(
+ "SELECT descriptor FROM storage_metadata WHERE singleton=1",
+ )
+ .fetch_one(&mut conn)
+ .await;
+ if let Err(error) = &stored {
+ super::diagnostics::failure(error);
+ }
+ conn.close().await?;
+ let stored: StorageDescriptor = serde_json::from_str(&stored?).map_err(storage_error)?;
+ if stored != journal.descriptor {
+ return Err(storage_error(
+ "migration index identity differs from journal; both files have been kept",
+ ));
+ }
+
+ let source_path = root.join("db.sqlite");
+ let _source_owner = screenpipe_sqlite_coordinator::acquire_sqlite_manager_lease(&source_path)
+ .map_err(storage_error)?;
+ let mut conn = sqlx::SqliteConnection::connect_with(
+ &sqlx::sqlite::SqliteConnectOptions::new()
+ .filename(&source_path)
+ .create_if_missing(false)
+ .pragma("locking_mode", "EXCLUSIVE")
+ .busy_timeout(std::time::Duration::from_secs(5)),
+ )
+ .await?;
+ let empty = async {
+ sqlx::query("BEGIN EXCLUSIVE").execute(&mut conn).await?;
+ // Shadow tables belong to their virtual table. Query every logical
+ // table, including unknown extensions, rather than just frames/audio.
+ // SQLite statistics/sequences and SQLx's ledger contain no history.
+ let tables: Vec<String> = sqlx::query_scalar("SELECT name FROM pragma_table_list WHERE schema='main' AND type!='shadow' AND name NOT GLOB 'sqlite_*' AND name!='_sqlx_migrations'")
+ .fetch_all(&mut conn).await?;
+ for table in tables {
+ let populated: bool = sqlx::query_scalar(sqlx::AssertSqlSafe(format!("SELECT EXISTS(SELECT 1 FROM {} LIMIT 1)", quote(&table))))
+ .fetch_one(&mut conn).await?;
+ if populated {
+ super::diagnostics::batch(&table, None, None, None, None);
+ return Err(storage_error(format!("migration source conflicts with existing index: source contains recorded history in {table}; both files have been kept")));
+ }
+ }
+ sqlx::query("COMMIT").execute(&mut conn).await?;
+ // Checkpoint before moving the closed file; never orphan a WAL.
+ let mode: String = sqlx::query_scalar("PRAGMA journal_mode=DELETE").fetch_one(&mut conn).await?;
+ if mode != "delete" {
+ return Err(storage_error("empty migration source is still in use; both files have been kept"));
+ }
+ Ok::<_, sqlx::Error>(())
+ }.await;
+ if let Err(error) = &empty {
+ super::diagnostics::failure(error);
+ }
+ conn.close().await?;
+ empty?;
+ // The old retry may have overwritten receipts with the empty DB's counts.
+ // Persist the need to refresh them before moving anything, so interruption
+ // after the rename cannot make a later retry trust those stale receipts.
+ journal.phase = Phase::Paused;
+ durable_json(&root.join("storage-migration.json"), journal)?;
+ let retained = index.parent().unwrap().join(format!(
+ "recovered-empty-source-{}.sqlite",
+ uuid::Uuid::new_v4()
+ ));
+ std::fs::rename(&source_path, &retained)?;
+ sync_directory(index.parent().unwrap())?;
+ sync_directory(root)?;
+ super::faults::checkpoint("migration_empty_source_retired");
+ Ok(())
+}
+
pub(super) fn migration_is_paused(root: &Path) -> Result<bool, sqlx::Error> {
let journal = read_journal(root)?;
Ok(journal.phase == Phase::Paused && checked_path(root, Path::new("db.sqlite"))?.is_file())
@@ -234,6 +365,7 @@ async fn recover_observed(
fs2::FileExt::try_lock_exclusive(&lock)
.map_err(|_| storage_error("storage lifecycle is already owned"))?;
let mut journal = read_journal(&root)?;
+ reconcile_migration_source(&root, &mut journal).await?;
let index = checked_path(&root, &journal.descriptor.index)?;
if root.join("db.sqlite").is_file() {
if index.exists() || StorageDescriptor::read(&root)?.is_some() {
@@ -493,20 +625,38 @@ async fn migrate_observed(
journal
};
super::diagnostics::tables(&journal.source);
+ reconcile_migration_source(&root, &mut journal).await?;
+ let index = checked_path(&root, &journal.descriptor.index)?;
+ let recheck_unrenamed_source = source_path.is_file()
+ && !index.exists()
+ && journal.phase == Phase::Building
+ && source_identity(&root)? != journal.snapshot;
+ if recheck_unrenamed_source {
+ // A timestamp/checkpoint change alone must not strand valid history.
+ // Verify its saved logical receipts before accepting a new identity.
+ journal.phase = Phase::Paused;
+ }
if journal.phase == Phase::Paused {
progress(MigrationProgress::phase("checking history before retry"));
+ if index.is_file() {
+ super::in_place::recover_recording(
+ HybridStorage::new(root.clone(), journal.descriptor.clone())?,
+ &progress,
+ )
+ .await?;
+ }
super::diagnostics::stage("opening_retry_history");
// Recording may have appended, edited, or retained history since the
// failure. The explicit retry verifies that current logical dataset,
// keeping all already committed archive files and their generations.
- let active = StorageDescriptor::read(&root)?;
- let path = if active.is_some() {
- root.join(&journal.descriptor.index)
+ let path = if index.is_file() {
+ index.clone()
} else {
source_path.clone()
};
- let storage = active
- .map(|d| HybridStorage::new(root.clone(), d))
+ let storage = index
+ .is_file()
+ .then(|| HybridStorage::new(root.clone(), journal.descriptor.clone()))
.transpose()?;
let db = DatabaseManager::new_with_storage(
path.to_str().unwrap(),
@@ -518,7 +668,13 @@ async fn migrate_observed(
.await?;
let refreshed = async {
super::diagnostics::stage("retry_parity");
+ verify_integrity(&db.pool).await?;
let receipts = table_receipts(&db, Some(&journal.source)).await?;
+ if recheck_unrenamed_source && receipts != journal.source {
+ return Err(storage_error(
+ "migration source content changed before rename; source has been kept",
+ ));
+ }
let mut searches = Vec::new();
for (term, _) in &journal.search_receipts {
let ids = sqlx::query_scalar(MIGRATION_SEARCH)
@@ -551,7 +707,7 @@ async fn migrate_observed(
));
}
}
- let index = checked_path(&root, &journal.descriptor.index)?;
+ super::diagnostics::stage("validating_source_before_rename");
if source_path.exists() {
if index.exists() || source_identity(&root)? != journal.snapshot {
return Err(storage_error(
diff --git a/crates/screenpipe-db/tests/migration_source_recovery.rs b/crates/screenpipe-db/tests/migration_source_recovery.rs
new file mode 100644
index 000000000..d56c1ef1e
--- /dev/null
+++ b/crates/screenpipe-db/tests/migration_source_recovery.rs
@@ -0,0 +1,276 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+#![cfg(feature = "storage-fault-injection")]
+
+use screenpipe_db::{
+ storage::{migrate, recover_interrupted_migration, Projection},
+ DatabaseManager,
+};
+use std::path::Path;
+
+async fn seed(path: &Path, text: Option<&str>) {
+ let db = DatabaseManager::new(path.to_str().unwrap(), Default::default())
+ .await
+ .unwrap();
+ if let Some(text) = text {
+ db.execute_raw_sql_write(&format!(
+ "INSERT INTO frames(id,timestamp,full_text) VALUES(1,'2026-09-18','{text}')"
+ ))
+ .await
+ .unwrap();
+ }
+ db.close().await;
+}
+
+fn crash_migration(root: &Path, point: &str) {
+ let result = std::process::Command::new(env!("CARGO_BIN_EXE_screenpipe-storage"))
+ .arg("migrate")
+ .arg(root)
+ .env("SCREENPIPE_STORAGE_CRASH_AT", point)
+ .output()
+ .unwrap();
+ assert_eq!(
+ result.status.code(),
+ Some(86),
+ "{}",
+ String::from_utf8_lossy(&result.stderr)
+ );
+}
+
+#[tokio::test]
+async fn recovery_child() {
+ if let Ok(root) = std::env::var("SCREENPIPE_TEST_SOURCE_RECOVERY_ROOT") {
+ recover_interrupted_migration(Path::new(&root), Default::default())
+ .await
+ .unwrap();
+ }
+}
+
+async fn paused_before_activation() -> tempfile::TempDir {
+ let root = tempfile::tempdir().unwrap();
+ seed(&root.path().join("db.sqlite"), Some("original history")).await;
+ crash_migration(root.path(), "migration_after_rename");
+ let result = std::process::Command::new(std::env::current_exe().unwrap())
+ .args(["--exact", "recovery_child", "--nocapture"])
+ .env("SCREENPIPE_TEST_SOURCE_RECOVERY_ROOT", root.path())
+ .env("SCREENPIPE_STORAGE_CRASH_AT", "migration_recovery_ready")
+ .output()
+ .unwrap();
+ assert_eq!(
+ result.status.code(),
+ Some(86),
+ "{}",
+ String::from_utf8_lossy(&result.stderr)
+ );
+ assert!(!root.path().join("storage.json").exists());
+ assert!(!root.path().join("db.sqlite").exists());
+ root
+}
+
+async fn finish_and_verify(root: &Path) {
+ let report = migrate(root, Default::default(), Default::default())
+ .await
+ .unwrap();
+ assert_eq!(report.frames, 1);
+ assert!(root.join("storage-migration-complete.json").is_file());
+ assert!(!root.join("storage-migration.json").exists());
+ assert!(!root.join("db.sqlite").exists());
+ for pass in 0..2 {
+ let db = DatabaseManager::new(root.join("db.sqlite").to_str().unwrap(), Default::default())
+ .await
+ .unwrap();
+ assert_eq!(
+ db.frame_payloads(&[1], Projection::Search).await.unwrap()[&1].text(),
+ "original history"
+ );
+ if pass == 0 {
+ db.execute_raw_sql_write(
+ "INSERT INTO frames(id,timestamp,full_text) VALUES(2,'2026-09-18','new recording')",
+ )
+ .await
+ .unwrap();
+ }
+ assert_eq!(
+ db.frame_payloads(&[2], Projection::Search).await.unwrap()[&2].text(),
+ "new recording"
+ );
+ db.verify_storage().await.unwrap();
+ db.close().await;
+ }
+}
+
+#[tokio::test]
+async fn explicit_retry_after_interrupted_recovery_uses_the_existing_index() {
+ let root = paused_before_activation().await;
+ finish_and_verify(root.path()).await;
+}
+
+async fn recreate_old_retry(root: &Path) {
+ // The old retry opened the missing legacy path with create-if-missing.
+ let empty = root.join("recreated.sqlite");
+ seed(&empty, None).await;
+ std::fs::rename(empty, root.join("db.sqlite")).unwrap();
+ // The old retry then saved receipts from the empty DB and changed paused
+ // back to building before hitting the conflicting-files guard.
+ let journal_path = root.join("storage-migration.json");
+ let mut journal: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(&journal_path).unwrap()).unwrap();
+ journal["phase"] = "building".into();
+ for table in journal["source"].as_array_mut().unwrap() {
+ if table["table"] == "frames" {
+ table["rows"] = 0.into();
+ table["sha256"] = "old-empty-source-receipt".into();
+ }
+ }
+ std::fs::write(&journal_path, serde_json::to_vec(&journal).unwrap()).unwrap();
+}
+
+#[tokio::test]
+async fn recovery_and_retry_preserve_history_when_old_retry_recreated_an_empty_source() {
+ let root = paused_before_activation().await;
+ recreate_old_retry(root.path()).await;
+ recover_interrupted_migration(root.path(), Default::default())
+ .await
+ .unwrap();
+ // Recovery must keep conversion paused and permit durable recording.
+ assert!(root.path().join("storage-migration.json").is_file());
+ assert!(!root.path().join("storage-migration-complete.json").exists());
+ let descriptor = screenpipe_db::storage::StorageDescriptor::read(root.path())
+ .unwrap()
+ .unwrap();
+ let retained: Vec<_> = std::fs::read_dir(root.path().join(descriptor.index).parent().unwrap())
+ .unwrap()
+ .filter_map(Result::ok)
+ .filter(|entry| {
+ entry
+ .file_name()
+ .to_string_lossy()
+ .starts_with("recovered-empty-source-")
+ })
+ .collect();
+ assert_eq!(retained.len(), 1);
+ recover_interrupted_migration(root.path(), Default::default())
+ .await
+ .unwrap();
+ finish_and_verify(root.path()).await;
+}
+
+#[tokio::test]
+async fn conflicting_nonempty_source_is_preserved_and_diagnosed() {
+ let root = paused_before_activation().await;
+ let extra = root.path().join("recreated.sqlite");
+ seed(&extra, Some("additional history")).await;
+ let source = root.path().join("db.sqlite");
+ std::fs::rename(extra, &source).unwrap();
+ let before = std::fs::read(&source).unwrap();
+ let journal = std::fs::read(root.path().join("storage-migration.json")).unwrap();
+ let error = migrate(root.path(), Default::default(), Default::default())
+ .await
+ .unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("source contains recorded history"),
+ "{error}"
+ );
+ assert_eq!(std::fs::read(&source).unwrap(), before);
+ assert_eq!(
+ std::fs::read(root.path().join("storage-migration.json")).unwrap(),
+ journal
+ );
+ let snapshots = tokio::time::timeout(std::time::Duration::from_secs(3), async {
+ loop {
+ let snapshots =
+ screenpipe_db::storage::diagnostics::recent(root.path()).unwrap_or_default();
+ if snapshots.iter().any(|s| s.status == "failed") {
+ break snapshots;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap();
+ let failed = snapshots.iter().find(|s| s.status == "failed").unwrap();
+ assert_eq!(
+ failed.failure_stage.as_deref(),
+ Some("validating_migration_source")
+ );
+ let state = failed.source_state.as_ref().unwrap();
+ assert!(state.source_exists && state.index_exists);
+ assert!(!state.active_descriptor_present);
+ assert_eq!(state.journal_phase, "paused");
+}
+
+#[tokio::test]
+async fn retry_resumes_if_interrupted_after_retaining_the_empty_source() {
+ let root = paused_before_activation().await;
+ recreate_old_retry(root.path()).await;
+ crash_migration(root.path(), "migration_empty_source_retired");
+ assert!(!root.path().join("db.sqlite").exists());
+ // An explicit retry must also work without another startup recovery first.
+ finish_and_verify(root.path()).await;
+}
+
+#[tokio::test]
+async fn explicit_retry_refreshes_a_source_checkpoint_before_it_was_renamed() {
+ let root = tempfile::tempdir().unwrap();
+ let path = root.path().join("db.sqlite");
+ seed(&path, Some("original history")).await;
+ crash_migration(root.path(), "migration_before_rename");
+ std::fs::File::options()
+ .write(true)
+ .open(&path)
+ .unwrap()
+ .set_modified(std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1))
+ .unwrap();
+ finish_and_verify(root.path()).await;
+}
+
+#[tokio::test]
+async fn changed_source_contents_before_rename_are_preserved() {
+ use sqlx::Connection;
+ let root = tempfile::tempdir().unwrap();
+ let path = root.path().join("db.sqlite");
+ seed(&path, Some("original history")).await;
+ crash_migration(root.path(), "migration_before_rename");
+ let journal = std::fs::read(root.path().join("storage-migration.json")).unwrap();
+ let mut conn = sqlx::SqliteConnection::connect_with(
+ &sqlx::sqlite::SqliteConnectOptions::new().filename(&path),
+ )
+ .await
+ .unwrap();
+ sqlx::query("UPDATE frames SET full_text='changed history' WHERE id=1")
+ .execute(&mut conn)
+ .await
+ .unwrap();
+ conn.close().await.unwrap();
+ let error = migrate(root.path(), Default::default(), Default::default())
+ .await
+ .unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("source content changed before rename"),
+ "{error}"
+ );
+ assert!(path.is_file());
+ assert!(!root.path().join("storage.json").exists());
+ assert_eq!(
+ std::fs::read(root.path().join("storage-migration.json")).unwrap(),
+ journal
+ );
+ let mut conn = sqlx::SqliteConnection::connect_with(
+ &sqlx::sqlite::SqliteConnectOptions::new()
+ .filename(&path)
+ .read_only(true),
+ )
+ .await
+ .unwrap();
+ let text: String = sqlx::query_scalar("SELECT full_text FROM frames WHERE id=1")
+ .fetch_one(&mut conn)
+ .await
+ .unwrap();
+ assert_eq!(text, "changed history");
+ conn.close().await.unwrap();
+}