db: add migration
This commit is contained in:
parent
11557e1de2
commit
e2ac408c35
5 changed files with 46 additions and 10 deletions
9
database/migrations/202208201623.sql
Normal file
9
database/migrations/202208201623.sql
Normal file
|
@ -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
|
||||
;
|
|
@ -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
|
||||
;
|
||||
|
|
|
@ -86,4 +86,6 @@ pub trait Repository {
|
|||
fn stop_task(&self) -> Result<domain::Task, Error>;
|
||||
|
||||
fn finish_task(&self) -> Result<domain::Task, Error>;
|
||||
|
||||
fn get_finished_tasks(&self) -> Result<domain::Task, Error>;
|
||||
}
|
||||
|
|
|
@ -203,6 +203,10 @@ impl Repository for FsRepo {
|
|||
|
||||
Ok(task.into())
|
||||
}
|
||||
|
||||
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl FsRepo {
|
||||
|
|
|
@ -211,6 +211,10 @@ impl Repository for SqliteRepo {
|
|||
|
||||
Ok(db_task.into())
|
||||
}
|
||||
|
||||
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
|
||||
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(());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: execute full schema if version is none
|
||||
if version.is_none() {
|
||||
run_migration!(self, version = 202208162308);
|
||||
}
|
||||
|
||||
self.conn
|
||||
|
|
Loading…
Reference in a new issue