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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
chore: use migrations instead of hardcoded sql
Louis Beaumont50013167d34f
diff --git a/screenpipe-server/Cargo.toml b/screenpipe-server/Cargo.tomlindex a966ed000..c5ccbf263 100644--- a/screenpipe-server/Cargo.toml+++ b/screenpipe-server/Cargo.toml@@ -30,7 +30,7 @@ num_cpus = "1.0"chrono = { version = "0.4.31", features = ["serde"] }# Database-sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio-native-tls", "chrono"] }+sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio-native-tls", "chrono", "migrate"] }# FFmpeg bindingsffmpeg-next = "7.0.2"diff --git a/screenpipe-server/src/db.rs b/screenpipe-server/src/db.rsindex 5e4a797e4..56497bb9f 100644--- a/screenpipe-server/src/db.rs+++ b/screenpipe-server/src/db.rs@@ -1,5 +1,6 @@use chrono::{DateTime, Utc};use serde::{Deserialize, Serialize};+use sqlx::migrate::MigrateDatabase;use sqlx::{sqlite::{SqlitePool, SqlitePoolOptions},FromRow,@@ -45,17 +46,29 @@ pub struct DatabaseManager {impl DatabaseManager {pub async fn new(database_path: &str) -> Result<Self, sqlx::Error> {- let connection_string = format!("{}?mode=rwc", database_path);+ let connection_string = format!("sqlite:{}", database_path);++ // Create the database if it doesn't exist+ if !sqlx::Sqlite::database_exists(&connection_string).await? {+ sqlx::Sqlite::create_database(&connection_string).await?;+ }+let pool = SqlitePoolOptions::new().max_connections(5).acquire_timeout(Duration::from_secs(3)).connect(&connection_string).await?;+let db_manager = DatabaseManager { pool };- db_manager.create_tables().await?;+ db_manager.run_migrations().await?;Ok(db_manager)}+ async fn run_migrations(&self) -> Result<(), sqlx::Error> {+ sqlx::migrate!("./src/migrations").run(&self.pool).await?;+ Ok(())+ }+pub async fn insert_audio_chunk(&self, file_path: &str) -> Result<i64, sqlx::Error> {let mut tx = self.pool.begin().await?;let id = sqlx::query("INSERT INTO audio_chunks (file_path) VALUES (?1)")@@ -87,115 +100,6 @@ impl DatabaseManager {Ok(())}- async fn create_tables(&self) -> Result<(), sqlx::Error> {- let mut tx = self.pool.begin().await?;-- sqlx::query(- "CREATE TABLE IF NOT EXISTS video_chunks (- id INTEGER PRIMARY KEY AUTOINCREMENT,- file_path TEXT NOT NULL- )",- )- .execute(&mut *tx)- .await?;-- sqlx::query(- "CREATE TABLE IF NOT EXISTS frames (- id INTEGER PRIMARY KEY AUTOINCREMENT,- video_chunk_id INTEGER NOT NULL,- offset_index INTEGER NOT NULL,- timestamp TIMESTAMP NOT NULL- )",- )- .execute(&mut *tx)- .await?;-- sqlx::query(- "CREATE TABLE IF NOT EXISTS ocr_text (- frame_id INTEGER NOT NULL,- text TEXT NOT NULL- )",- )- .execute(&mut *tx)- .await?;-- sqlx::query(- "CREATE TABLE IF NOT EXISTS audio_chunks (- id INTEGER PRIMARY KEY AUTOINCREMENT,- file_path TEXT NOT NULL- )",- )- .execute(&mut *tx)- .await?;-- sqlx::query(- "CREATE TABLE IF NOT EXISTS audio_transcriptions (- id INTEGER PRIMARY KEY AUTOINCREMENT,- audio_chunk_id INTEGER NOT NULL,- offset_index INTEGER NOT NULL,- timestamp TIMESTAMP NOT NULL,- transcription TEXT NOT NULL,- FOREIGN KEY (audio_chunk_id) REFERENCES audio_chunks(id)- )",- )- .execute(&mut *tx)- .await?;-- tx.commit().await?;-- self.create_indices().await?;-- Ok(())- }-- pub async fn purge(&mut self) -> Result<(), sqlx::Error> {- let mut tx = self.pool.begin().await?;-- sqlx::query("DROP TABLE IF EXISTS video_chunks")- .execute(&mut *tx)- .await?;- sqlx::query("DROP TABLE IF EXISTS frames")- .execute(&mut *tx)- .await?;- sqlx::query("DROP TABLE IF EXISTS ocr_text")- .execute(&mut *tx)- .await?;- sqlx::query("DROP TABLE IF EXISTS audio_chunks")- .execute(&mut *tx)- .await?;- sqlx::query("DROP TABLE IF EXISTS audio_transcriptions")- .execute(&mut *tx)- .await?;-- tx.commit().await?;-- self.create_tables().await?;- Ok(())- }-- async fn create_indices(&self) -> Result<(), sqlx::Error> {- let mut tx = self.pool.begin().await?;-- sqlx::query(- "CREATE INDEX IF NOT EXISTS idx_video_chunk_id_id ON frames ( video_chunk_id, id)",- )- .execute(&mut *tx)- .await?;-- sqlx::query("CREATE INDEX IF NOT EXISTS idx_frame_id ON ocr_text (frame_id)")- .execute(&mut *tx)- .await?;-- sqlx::query(- "CREATE INDEX IF NOT EXISTS idx_audio_chunk_id ON audio_transcriptions (audio_chunk_id)",- )- .execute(&mut *tx)- .await?;-- tx.commit().await?;- Ok(())- }-pub async fn insert_video_chunk(&self, file_path: &str) -> Result<i64, sqlx::Error> {let mut tx = self.pool.begin().await?;let id = sqlx::query("INSERT INTO video_chunks (file_path) VALUES (?1)")@@ -534,82 +438,3 @@ impl Clone for DatabaseManager {}}-#[cfg(test)]-mod tests {- use super::*;-- async fn setup_test_db() -> DatabaseManager {- DatabaseManager::new("sqlite::memory:").await.unwrap()- }-- #[tokio::test]- async fn test_insert_and_search_ocr() {- let db = setup_test_db().await;- let video_chunk_id = db.insert_video_chunk("test_video.mp4").await.unwrap();- let frame_id = db.insert_frame().await.unwrap();- db.insert_ocr_text(frame_id, "Hello, world!").await.unwrap();-- let results = db.search("Hello", ContentType::OCR, 100, 0).await.unwrap();- assert_eq!(results.len(), 1);- if let SearchResult::OCR(ocr_result) = &results[0] {- assert_eq!(ocr_result.ocr_text, "Hello, world!");- assert_eq!(ocr_result.file_path, "test_video.mp4");- } else {- panic!("Expected OCR result");- }- }-- #[tokio::test]- async fn test_insert_and_search_audio() {- let db = setup_test_db().await;- let audio_chunk_id = db.insert_audio_chunk("test_audio.mp3").await.unwrap();- db.insert_audio_transcription(audio_chunk_id, "Hello from audio", 0)- .await- .unwrap();-- let results = db- .search("audio", ContentType::Audio, 100, 0)- .await- .unwrap();- assert_eq!(results.len(), 1);- if let SearchResult::Audio(audio_result) = &results[0] {- assert_eq!(audio_result.transcription, "Hello from audio");- assert_eq!(audio_result.file_path, "test_audio.mp3");- } else {- panic!("Expected Audio result");- }- }-- #[tokio::test]- async fn test_search_all() {- let db = setup_test_db().await;-- // Insert OCR data- let video_chunk_id = db.insert_video_chunk("test_video.mp4").await.unwrap();- let frame_id = db.insert_frame().await.unwrap();- db.insert_ocr_text(frame_id, "Hello from OCR")- .await- .unwrap();-- // Insert Audio data- let audio_chunk_id = db.insert_audio_chunk("test_audio.mp3").await.unwrap();- db.insert_audio_transcription(audio_chunk_id, "Hello from audio", 0)- .await- .unwrap();-- let results = db.search("Hello", ContentType::All, 100, 0).await.unwrap();- assert_eq!(results.len(), 2);-- let ocr_count = results- .iter()- .filter(|r| matches!(r, SearchResult::OCR(_)))- .count();- let audio_count = results- .iter()- .filter(|r| matches!(r, SearchResult::Audio(_)))- .count();-- assert_eq!(ocr_count, 1);- assert_eq!(audio_count, 1);- }-}diff --git a/screenpipe-server/src/migrations/20240703111257_screenpipe.sql b/screenpipe-server/src/migrations/20240703111257_screenpipe.sqlindex 5a5fb1554..77583881e 100644--- a/screenpipe-server/src/migrations/20240703111257_screenpipe.sql+++ b/screenpipe-server/src/migrations/20240703111257_screenpipe.sql@@ -1,4 +1,3 @@--- Not used yet TODO-- Create video_chunks tableCREATE TABLE IF NOT EXISTS video_chunks (@@ -15,8 +14,8 @@ CREATE TABLE IF NOT EXISTS frames (FOREIGN KEY (video_chunk_id) REFERENCES video_chunks(id));--- Create ocr_text virtual table-CREATE VIRTUAL TABLE IF NOT EXISTS ocr_text USING fts4(+-- Create ocr_text table+CREATE TABLE IF NOT EXISTS ocr_text (frame_id INTEGER NOT NULL,text TEXT NOT NULL);@@ -39,4 +38,6 @@ CREATE TABLE IF NOT EXISTS audio_transcriptions (-- Create indices (you may want to add specific indices based on your query patterns)CREATE INDEX IF NOT EXISTS idx_frames_video_chunk_id ON frames(video_chunk_id);-CREATE INDEX IF NOT EXISTS idx_audio_transcriptions_audio_chunk_id ON audio_transcriptions(audio_chunk_id);\ No newline at end of file+CREATE INDEX IF NOT EXISTS idx_audio_transcriptions_audio_chunk_id ON audio_transcriptions(audio_chunk_id);+CREATE INDEX IF NOT EXISTS idx_ocr_text_frame_id ON ocr_text(frame_id);+diff --git a/screenpipe-server/tests/db.rs b/screenpipe-server/tests/db.rsnew file mode 100644index 000000000..2f864e35f--- /dev/null+++ b/screenpipe-server/tests/db.rs@@ -0,0 +1,79 @@+#[cfg(test)]+mod tests {+ use screenpipe_server::{ContentType, DatabaseManager, SearchResult};++ async fn setup_test_db() -> DatabaseManager {+ DatabaseManager::new("sqlite::memory:").await.unwrap()+ }++ #[tokio::test]+ async fn test_insert_and_search_ocr() {+ let db = setup_test_db().await;+ let _ = db.insert_video_chunk("test_video.mp4").await.unwrap();+ let frame_id = db.insert_frame().await.unwrap();+ db.insert_ocr_text(frame_id, "Hello, world!").await.unwrap();++ let results = db.search("Hello", ContentType::OCR, 100, 0).await.unwrap();+ assert_eq!(results.len(), 1);+ if let SearchResult::OCR(ocr_result) = &results[0] {+ assert_eq!(ocr_result.ocr_text, "Hello, world!");+ assert_eq!(ocr_result.file_path, "test_video.mp4");+ } else {+ panic!("Expected OCR result");+ }+ }++ #[tokio::test]+ async fn test_insert_and_search_audio() {+ let db = setup_test_db().await;+ let audio_chunk_id = db.insert_audio_chunk("test_audio.mp3").await.unwrap();+ db.insert_audio_transcription(audio_chunk_id, "Hello from audio", 0)+ .await+ .unwrap();++ let results = db+ .search("audio", ContentType::Audio, 100, 0)+ .await+ .unwrap();+ assert_eq!(results.len(), 1);+ if let SearchResult::Audio(audio_result) = &results[0] {+ assert_eq!(audio_result.transcription, "Hello from audio");+ assert_eq!(audio_result.file_path, "test_audio.mp3");+ } else {+ panic!("Expected Audio result");+ }+ }++ #[tokio::test]+ async fn test_search_all() {+ let db = setup_test_db().await;++ // Insert OCR data+ let _ = db.insert_video_chunk("test_video.mp4").await.unwrap();+ let frame_id = db.insert_frame().await.unwrap();+ db.insert_ocr_text(frame_id, "Hello from OCR")+ .await+ .unwrap();++ // Insert Audio data+ let audio_chunk_id = db.insert_audio_chunk("test_audio.mp3").await.unwrap();+ db.insert_audio_transcription(audio_chunk_id, "Hello from audio", 0)+ .await+ .unwrap();++ let results = db.search("Hello", ContentType::All, 100, 0).await.unwrap();+ assert_eq!(results.len(), 2);++ let ocr_count = results+ .iter()+ .filter(|r| matches!(r, SearchResult::OCR(_)))+ .count();+ let audio_count = results+ .iter()+ .filter(|r| matches!(r, SearchResult::Audio(_)))+ .count();++ assert_eq!(ocr_count, 1);+ assert_eq!(audio_count, 1);+ }+}