bin: add migration of finished fs tasks

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-21 00:01:28 +03:00
parent 73ab922c77
commit 8bd588070e
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 58 additions and 19 deletions

View file

@ -4,23 +4,58 @@ use xdg::BaseDirectories;
fn main() { fn main() {
let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap();
let fs_repo = repo::fs::FsRepo::new(xdg_dirs.clone()); 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(); 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(); sqlite_repo
for task in fs_tasks { .insert_task(repo::InsertTaskData {
log::info!("task: {}", task.name); name: task.name,
log::info!(" inserting..."); project: task.project,
link: task.link,
dir_path: task.dir_path,
index: None,
finished_at: None,
})
.unwrap();
sqlite_repo log::info!(" inserted");
.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!("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");
}
} }
} }

View file

@ -43,6 +43,7 @@ pub fn execute(repo: impl Repository, args: Args) {
.transpose() .transpose()
.unwrap(), .unwrap(),
index: None, index: None,
finished_at: None,
}); });
match res { match res {

View file

@ -77,6 +77,7 @@ pub fn execute(repo: impl Repository, args: Args) {
project: target.project, project: target.project,
link: target.link, link: target.link,
dir_path: target.dir_path, dir_path: target.dir_path,
finished_at: None,
}); });
match res { match res {
Ok(task) => { Ok(task) => {

View file

@ -55,6 +55,7 @@ pub struct InsertTaskData {
pub link: Option<String>, pub link: Option<String>,
pub dir_path: Option<PathBuf>, pub dir_path: Option<PathBuf>,
pub index: Option<usize>, pub index: Option<usize>,
pub finished_at: Option<time::OffsetDateTime>,
} }
pub struct UpdateTaskData { pub struct UpdateTaskData {

View file

@ -60,9 +60,9 @@ impl From<CurrentTaskInfo> for domain::CurrentTaskInfo {
} }
} }
const CURRENT_TASK_FILE: &str = "current.json"; pub const CURRENT_TASK_FILE: &str = "current.json";
const DATA_FILE: &str = "data.json"; pub const DATA_FILE: &str = "data.json";
const FINISHED_DATA_FILE: &str = "finished_data.json"; pub const FINISHED_DATA_FILE: &str = "finished_data.json";
pub struct FsRepo { pub struct FsRepo {
xdg_dirs: BaseDirectories, xdg_dirs: BaseDirectories,

View file

@ -121,8 +121,8 @@ impl Repository for SqliteRepo {
let mut stmt = self let mut stmt = self
.conn .conn
.prepare( .prepare(
"INSERT INTO tasks (name, project, link, dir_path) "INSERT INTO tasks (name, project, link, dir_path, created_at, finished_at)
VALUES (?1, ?2, ?3, ?4)", VALUES (?1, ?2, ?3, ?4, ?5, ?5)",
) )
.map_err(|_| Error::PrepareQuery)?; .map_err(|_| Error::PrepareQuery)?;
@ -134,6 +134,7 @@ impl Repository for SqliteRepo {
&insert_data &insert_data
.dir_path .dir_path
.and_then(|p| p.into_os_string().into_string().ok()), .and_then(|p| p.into_os_string().into_string().ok()),
&insert_data.finished_at,
)) ))
.map_err(|_| Error::InsertData)?; .map_err(|_| Error::InsertData)?;