@ -1,5 +1,6 @@
use sqlx ::sqlite ::SqliteConnectOptions ;
use sqlx ::sqlite ::SqlitePool ;
use sqlx ::Connection ;
use sqlx ::Row ;
use std ::{ env , str ::FromStr } ;
use tokio ::process ::Command ;
@ -33,14 +34,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
. unwrap ( ) ;
let url = format! ( "sqlite://{}" , db_path . as_path ( ) . display ( ) ) ;
let pool =
SqlitePool ::connect_with ( SqliteConnectOptions ::from_str ( & url ) ? . create_if_missing ( true ) )
. await ? ;
sqlx ::migrate ! ( "./migrations" ) . run ( & pool ) . await ? ;
let mut dbconn = sqlx ::SqliteConnection ::connect_with (
& SqliteConnectOptions ::from_str ( & url ) ?
. create_if_missing ( true )
// Use the normal journal mode to avoid those pesky shm and wal files
. journal_mode ( sqlx ::sqlite ::SqliteJournalMode ::Delete ) ,
)
. await ? ;
// let pool =
// SqlitePool::connect_with(SqliteConnectOptions::from_str(&url)?.create_if_missing(true))
// .await?;
sqlx ::migrate ! ( "./migrations" ) . run ( & mut dbconn ) . await ? ;
let profiles_created : u64 = sqlx ::query ( r#"INSERT OR IGNORE INTO profile (name) VALUES (?)"# )
. bind ( & profile )
. execute ( & pool )
. execute ( & mut dbconn )
. await ?
. rows_affected ( ) ;
if profiles_created ! = 0 {
@ -58,7 +66,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let already_watched_count : i64 = sqlx ::query ( r#"SELECT count(*) FROM watched WHERE path = ? AND profile = (SELECT id FROM PROFILE WHERE name = ?)"# )
. bind ( f . path ( ) . as_os_str ( ) . to_str ( ) )
. bind ( & profile )
. fetch_one ( & pool ) . await ? . try_get ( "count(*)" ) ? ;
. fetch_one ( & mut dbconn ) . await ? . try_get ( "count(*)" ) ? ;
println! ( "Already watched count: {}" , already_watched_count ) ;
if already_watched_count > 0 {
@ -78,7 +86,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let insert : u64 = sqlx ::query ( r#"INSERT INTO watched (profile, path, watched_at) SELECT (SELECT id FROM PROFILE WHERE name = ?) profile, ? path, DATETIME('now') watched_at"# )
. bind ( & profile )
. bind ( f . path ( ) . as_os_str ( ) . to_str ( ) )
. execute ( & pool ) . await ? . rows_affected ( ) ;
. execute ( & mut dbconn ) . await ? . rows_affected ( ) ;
println! ( "Insert: {:?}" , insert ) ;
assert! ( insert = = 1 ) ;
continue ;
@ -89,6 +97,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
break ;
}
dbconn . close ( ) . await ? ;
Ok ( ( ) )
}