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(frames_fts): filter backfill + log migration start

TerryChengTW1f11216655cf
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index 9235f4f11..c2ab5b7b0 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -43,6 +43,7 @@ const DEDUP_TIME_WINDOW_SECS: i64 = 45;
/// Similarity threshold for cross-device deduplication (0.0 to 1.0).
/// Higher = stricter matching, lower = more aggressive deduplication.
const DEDUP_SIMILARITY_THRESHOLD: f64 = 0.85;
+const FRAMES_FTS_EXTERNAL_CONTENT_MIGRATION_VERSION: i64 = 20260415000000;
pub struct DeleteTimeRangeResult {
pub frames_deleted: u64,
@@ -268,6 +269,7 @@ impl DatabaseManager {
async fn run_migrations(pool: &SqlitePool) -> Result<(), sqlx::Error> {
let mut migrator = sqlx::migrate!("./src/migrations");
migrator.set_ignore_missing(true);
+ Self::log_pending_search_index_migration(pool, &migrator).await;
match migrator.run(pool).await {
Ok(_) => {}
Err(e) => {
@@ -299,6 +301,63 @@ impl DatabaseManager {
Ok(())
}
+ async fn log_pending_search_index_migration(
+ pool: &SqlitePool,
+ migrator: &sqlx::migrate::Migrator,
+ ) {
+ if !migrator
+ .iter()
+ .any(|migration| migration.version == FRAMES_FTS_EXTERNAL_CONTENT_MIGRATION_VERSION)
+ {
+ return;
+ }
+
+ let migration_table_exists = match sqlx::query_scalar::<_, i64>(
+ "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = '_sqlx_migrations'",
+ )
+ .fetch_one(pool)
+ .await
+ {
+ Ok(count) => count > 0,
+ Err(e) => {
+ debug!("could not inspect _sqlx_migrations before migrate: {}", e);
+ return;
+ }
+ };
+
+ let migration_pending = if migration_table_exists {
+ match sqlx::query_scalar::<_, i64>(
+ "SELECT COUNT(*) FROM _sqlx_migrations WHERE version = ?",
+ )
+ .bind(FRAMES_FTS_EXTERNAL_CONTENT_MIGRATION_VERSION)
+ .fetch_one(pool)
+ .await
+ {
+ Ok(count) => count == 0,
+ Err(e) => {
+ debug!("could not inspect applied migrations before migrate: {}", e);
+ return;
+ }
+ }
+ } else {
+ match sqlx::query_scalar::<_, i64>("SELECT 1 FROM frames LIMIT 1")
+ .fetch_optional(pool)
+ .await
+ {
+ Ok(Some(_)) => true,
+ Ok(None) => false,
+ Err(e) => {
+ debug!("could not inspect existing frames before migrate: {}", e);
+ return;
+ }
+ }
+ };
+
+ if migration_pending {
+ info!("migrating frames_fts search index, this may take a few minutes on large databases...");
+ }
+ }
+
/// Fix checksum mismatches by updating stored checksums to match current migration files.
/// This is needed when a migration file was modified after being applied to the DB
/// (which happened with the fps migration between v0.3.130 and v0.3.131).
diff --git a/crates/screenpipe-db/src/migrations/20260415000000_frames_fts_external_content.sql b/crates/screenpipe-db/src/migrations/20260415000000_frames_fts_external_content.sql
index c1678a7e0..cead8f995 100644
--- a/crates/screenpipe-db/src/migrations/20260415000000_frames_fts_external_content.sql
+++ b/crates/screenpipe-db/src/migrations/20260415000000_frames_fts_external_content.sql
@@ -52,8 +52,18 @@ CREATE VIRTUAL TABLE frames_fts USING fts5(
tokenize='unicode61'
);
--- 3. Rebuild the inverted index from frames. This is the slow step.
-INSERT INTO frames_fts(frames_fts) VALUES('rebuild');
+-- 3. Backfill the inverted index from frames rows that should actually be
+-- searchable. This preserves the trigger semantics below: metadata-only rows
+-- without OCR text are not indexed.
+INSERT INTO frames_fts(rowid, full_text, app_name, window_name, browser_url)
+SELECT
+ id,
+ full_text,
+ COALESCE(app_name, ''),
+ COALESCE(window_name, ''),
+ COALESCE(browser_url, '')
+FROM frames
+WHERE full_text IS NOT NULL AND full_text != '';
-- 4. Recreate triggers for external content mode.
-- External content requires explicit INSERT/DELETE on the FTS table to