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
|
WHERE t.finished_at IS NULL
|
||||||
ORDER BY t.created_at
|
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 stop_task(&self) -> Result<domain::Task, Error>;
|
||||||
|
|
||||||
fn finish_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())
|
Ok(task.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FsRepo {
|
impl FsRepo {
|
||||||
|
|
|
@ -211,6 +211,10 @@ impl Repository for SqliteRepo {
|
||||||
|
|
||||||
Ok(db_task.into())
|
Ok(db_task.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_finished_tasks(&self) -> Result<domain::Task, Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqliteRepo {
|
impl SqliteRepo {
|
||||||
|
@ -251,12 +255,16 @@ impl SqliteRepo {
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! run_migration {
|
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
|
$this
|
||||||
.conn
|
.conn
|
||||||
.execute_batch(&format!(
|
.execute_batch(&format!(
|
||||||
"BEGIN; {} COMMIT;",
|
"BEGIN; {} COMMIT;",
|
||||||
include_str!(concat!("../../database/migrations/", $version, ".sql"))
|
include_str!(concat!("../../database/", $sql_name, ".sql"))
|
||||||
))
|
))
|
||||||
.map_err(|_| MigrationError::Upgrade)?;
|
.map_err(|_| MigrationError::Upgrade)?;
|
||||||
|
|
||||||
|
@ -288,18 +296,21 @@ impl std::fmt::Display for MigrationError {
|
||||||
|
|
||||||
impl std::error::Error for MigrationError {}
|
impl std::error::Error for MigrationError {}
|
||||||
|
|
||||||
const LATEST_VERSION: i64 = 202208162308;
|
const LATEST_VERSION: i64 = 202208201623;
|
||||||
|
|
||||||
impl SqliteRepo {
|
impl SqliteRepo {
|
||||||
pub fn upgrade(&self) -> Result<(), MigrationError> {
|
pub fn upgrade(&self) -> Result<(), MigrationError> {
|
||||||
let mut version = self.version();
|
let mut version = self.version();
|
||||||
if version == Some(LATEST_VERSION) {
|
match version {
|
||||||
return Ok(());
|
Some(LATEST_VERSION) => return Ok(()),
|
||||||
}
|
None => {
|
||||||
|
run_migration!(self, version = LATEST_VERSION => "schema");
|
||||||
// TODO: execute full schema if version is none
|
}
|
||||||
if version.is_none() {
|
Some(v) => {
|
||||||
run_migration!(self, version = 202208162308);
|
if v == 202208162308 {
|
||||||
|
run_migration!(self, version = 202208201623);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.conn
|
self.conn
|
||||||
|
|
Loading…
Reference in a new issue