From 8bd588070e3acfa2d895e40db90a32f4e8f62869 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 21 Aug 2022 00:01:28 +0300 Subject: [PATCH] bin: add migration of finished fs tasks --- src/bin/fs_to_sqlite.rs | 63 ++++++++++++++++++++++++++++++++--------- src/cli/add.rs | 1 + src/cli/priority.rs | 1 + src/repo.rs | 1 + src/repo/fs.rs | 6 ++-- src/repo/sqlite.rs | 5 ++-- 6 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/bin/fs_to_sqlite.rs b/src/bin/fs_to_sqlite.rs index 9f509dc..2c478f0 100644 --- a/src/bin/fs_to_sqlite.rs +++ b/src/bin/fs_to_sqlite.rs @@ -4,23 +4,58 @@ use xdg::BaseDirectories; fn main() { let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); let fs_repo = repo::fs::FsRepo::new(xdg_dirs.clone()); + let sqlite_repo = repo::sqlite::SqliteRepo::new(xdg_dirs.clone()).unwrap(); + + log::info!("active tasks"); + let fs_tasks = fs_repo.get_tasks(false).unwrap(); + if !fs_tasks.is_empty() { + for task in fs_tasks { + log::info!("task: {}", task.name); + log::info!(" inserting..."); - let sqlite_repo = repo::sqlite::SqliteRepo::new(xdg_dirs).unwrap(); - for task in fs_tasks { - log::info!("task: {}", task.name); - log::info!(" inserting..."); + sqlite_repo + .insert_task(repo::InsertTaskData { + name: task.name, + project: task.project, + link: task.link, + dir_path: task.dir_path, + index: None, + finished_at: None, + }) + .unwrap(); - sqlite_repo - .insert_task(repo::InsertTaskData { - name: task.name, - project: task.project, - link: task.link, - dir_path: task.dir_path, - index: None, - }) - .unwrap(); + log::info!(" inserted"); + } + } - log::info!(" inserted"); + log::info!("finished tasks"); + + let fs_tasks = fs_repo.get_tasks(true).unwrap(); + if !fs_tasks.is_empty() { + let meta = std::fs::metadata(xdg_dirs.get_data_file(repo::fs::FINISHED_DATA_FILE)).unwrap(); + let finished_at = meta + .modified() + .map(time::OffsetDateTime::from) + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()); + + for task in fs_tasks { + log::info!("task: {}", task.name); + log::info!(" inserting..."); + + sqlite_repo + .insert_task(repo::InsertTaskData { + name: task.name, + project: task.project, + link: task.link, + dir_path: task.dir_path, + index: None, + finished_at: Some(finished_at), + }) + // TODO: think of a better solution than idx + .ok(); + + log::info!(" inserted"); + } } } diff --git a/src/cli/add.rs b/src/cli/add.rs index bd3373b..94434ca 100644 --- a/src/cli/add.rs +++ b/src/cli/add.rs @@ -43,6 +43,7 @@ pub fn execute(repo: impl Repository, args: Args) { .transpose() .unwrap(), index: None, + finished_at: None, }); match res { diff --git a/src/cli/priority.rs b/src/cli/priority.rs index 5b360a8..2dc795b 100644 --- a/src/cli/priority.rs +++ b/src/cli/priority.rs @@ -77,6 +77,7 @@ pub fn execute(repo: impl Repository, args: Args) { project: target.project, link: target.link, dir_path: target.dir_path, + finished_at: None, }); match res { Ok(task) => { diff --git a/src/repo.rs b/src/repo.rs index 119da4d..d1bbd8e 100755 --- a/src/repo.rs +++ b/src/repo.rs @@ -55,6 +55,7 @@ pub struct InsertTaskData { pub link: Option, pub dir_path: Option, pub index: Option, + pub finished_at: Option, } pub struct UpdateTaskData { diff --git a/src/repo/fs.rs b/src/repo/fs.rs index 9ec94b8..1ff14cd 100644 --- a/src/repo/fs.rs +++ b/src/repo/fs.rs @@ -60,9 +60,9 @@ impl From for domain::CurrentTaskInfo { } } -const CURRENT_TASK_FILE: &str = "current.json"; -const DATA_FILE: &str = "data.json"; -const FINISHED_DATA_FILE: &str = "finished_data.json"; +pub const CURRENT_TASK_FILE: &str = "current.json"; +pub const DATA_FILE: &str = "data.json"; +pub const FINISHED_DATA_FILE: &str = "finished_data.json"; pub struct FsRepo { xdg_dirs: BaseDirectories, diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index 5e09e4a..3f3bc30 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -121,8 +121,8 @@ impl Repository for SqliteRepo { let mut stmt = self .conn .prepare( - "INSERT INTO tasks (name, project, link, dir_path) - VALUES (?1, ?2, ?3, ?4)", + "INSERT INTO tasks (name, project, link, dir_path, created_at, finished_at) + VALUES (?1, ?2, ?3, ?4, ?5, ?5)", ) .map_err(|_| Error::PrepareQuery)?; @@ -134,6 +134,7 @@ impl Repository for SqliteRepo { &insert_data .dir_path .and_then(|p| p.into_os_string().into_string().ok()), + &insert_data.finished_at, )) .map_err(|_| Error::InsertData)?;