diff --git a/database/migrations/202208201623.sql b/database/migrations/202208201623.sql new file mode 100644 index 0000000..c5a6790 --- /dev/null +++ b/database/migrations/202208201623.sql @@ -0,0 +1,9 @@ +CREATE VIEW finished_tasks +AS +SELECT + t.*, + row_number() OVER (ORDER BY t.finished_at DESC) AS idx +FROM tasks AS t +WHERE t.finished_at IS NOT NULL +ORDER BY t.finished_at DESC +; diff --git a/database/schema.sql b/database/schema.sql index 981c7bb..a5e919f 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -24,3 +24,13 @@ FROM tasks AS t WHERE t.finished_at IS NULL ORDER BY t.created_at ; + +CREATE VIEW finished_tasks +AS +SELECT + t.*, + row_number() OVER (ORDER BY t.finished_at DESC) AS idx +FROM tasks AS t +WHERE t.finished_at IS NOT NULL +ORDER BY t.finished_at DESC +; diff --git a/src/repo.rs b/src/repo.rs index 50f66cc..70dd069 100755 --- a/src/repo.rs +++ b/src/repo.rs @@ -86,4 +86,6 @@ pub trait Repository { fn stop_task(&self) -> Result; fn finish_task(&self) -> Result; + + fn get_finished_tasks(&self) -> Result; } diff --git a/src/repo/fs.rs b/src/repo/fs.rs index 55f83fc..9cb4f98 100644 --- a/src/repo/fs.rs +++ b/src/repo/fs.rs @@ -203,6 +203,10 @@ impl Repository for FsRepo { Ok(task.into()) } + + fn get_finished_tasks(&self) -> Result { + todo!() + } } impl FsRepo { diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index 77d47bd..7239cb9 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -211,6 +211,10 @@ impl Repository for SqliteRepo { Ok(db_task.into()) } + + fn get_finished_tasks(&self) -> Result { + todo!() + } } impl SqliteRepo { @@ -251,12 +255,16 @@ impl SqliteRepo { } macro_rules! run_migration { - ($this:ident, $ver:ident = $version:literal) => { + ($this:ident, $ver:ident = $version:expr) => { + run_migration!($this, $ver = $version => concat!("migrations/", $version)); + }; + + ($this:ident, $ver:ident = $version:expr => $sql_name:expr) => { $this .conn .execute_batch(&format!( "BEGIN; {} COMMIT;", - include_str!(concat!("../../database/migrations/", $version, ".sql")) + include_str!(concat!("../../database/", $sql_name, ".sql")) )) .map_err(|_| MigrationError::Upgrade)?; @@ -288,18 +296,21 @@ impl std::fmt::Display for MigrationError { impl std::error::Error for MigrationError {} -const LATEST_VERSION: i64 = 202208162308; +const LATEST_VERSION: i64 = 202208201623; impl SqliteRepo { pub fn upgrade(&self) -> Result<(), MigrationError> { let mut version = self.version(); - if version == Some(LATEST_VERSION) { - return Ok(()); - } - - // TODO: execute full schema if version is none - if version.is_none() { - run_migration!(self, version = 202208162308); + match version { + Some(LATEST_VERSION) => return Ok(()), + None => { + run_migration!(self, version = LATEST_VERSION => "schema"); + } + Some(v) => { + if v == 202208162308 { + run_migration!(self, version = 202208201623); + } + } } self.conn