-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: track version history in database (#409)
* refactor: create CeramicService and make processing undelivered events a fn call * feat: create a version table and update it when starting the daemon This can be used in the future to prevent downgrades that go across incompatible schema/data migrations
- Loading branch information
Showing
19 changed files
with
224 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- Add down migration script here | ||
DROP TABLE IF EXISTS "ceramic_one_version"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- Add up migration script here | ||
CREATE TABLE IF NOT EXISTS "ceramic_one_version" ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
"version" TEXT NOT NULL UNIQUE, | ||
"installed_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"last_started_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
mod error; | ||
mod event; | ||
mod interest; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
use std::sync::Arc; | ||
|
||
use ceramic_store::{CeramicOneVersion, SqlitePool}; | ||
pub use error::Error; | ||
pub use event::{BlockStore, CeramicEventService}; | ||
pub use interest::CeramicInterestService; | ||
|
||
pub(crate) type Result<T> = std::result::Result<T, Error>; | ||
|
||
/// The ceramic service holds the logic needed by the other components (e.g. api, recon) to access the store and process events | ||
/// in a way that makes sense to the ceramic protocol, and not just as raw bytes. | ||
#[derive(Debug)] | ||
pub struct CeramicService { | ||
pub(crate) interest: Arc<CeramicInterestService>, | ||
pub(crate) event: Arc<CeramicEventService>, | ||
} | ||
|
||
impl CeramicService { | ||
/// Create a new CeramicService and process undelivered events if requested | ||
pub async fn try_new(pool: SqlitePool) -> Result<Self> { | ||
// In the future, we may need to check the previous version to make sure we're not downgrading and risking data loss | ||
CeramicOneVersion::insert_current(&pool).await?; | ||
let interest = Arc::new(CeramicInterestService::new(pool.clone())); | ||
let event = Arc::new(CeramicEventService::new(pool).await?); | ||
Ok(Self { interest, event }) | ||
} | ||
|
||
/// Get the interest service | ||
pub fn interest_service(&self) -> &Arc<CeramicInterestService> { | ||
&self.interest | ||
} | ||
|
||
/// Get the event service | ||
pub fn event_service(&self) -> &Arc<CeramicEventService> { | ||
&self.event | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::str::FromStr; | ||
|
||
use anyhow::anyhow; | ||
|
||
use crate::{ | ||
sql::{entities::VersionRow, SqlitePool}, | ||
Error, Result, | ||
}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
/// It's kind of pointless to roundtrip CARGO_PKG_VERSION through this struct, | ||
/// but it makes it clear how we expect to format our versions in the database. | ||
struct SemVer { | ||
major: u64, | ||
minor: u64, | ||
patch: u64, | ||
} | ||
|
||
impl std::fmt::Display for SemVer { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}.{}.{}", self.major, self.minor, self.patch) | ||
} | ||
} | ||
|
||
impl std::str::FromStr for SemVer { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
let parts: Vec<&str> = s.split('.').collect(); | ||
if parts.len() != 3 { | ||
Err(Error::new_invalid_arg(anyhow!( | ||
"Invalid version. Must have 3 parts: {}", | ||
s.to_string() | ||
))) | ||
} else { | ||
let major = parts[0].parse().map_err(|_| { | ||
Error::new_invalid_arg(anyhow!( | ||
"Invalid version. Major did not parse: {}", | ||
s.to_string() | ||
)) | ||
})?; | ||
let minor = parts[1].parse().map_err(|_| { | ||
Error::new_invalid_arg(anyhow!( | ||
"Invalid version. Minor did not parse: {}", | ||
s.to_string() | ||
)) | ||
})?; | ||
let patch = parts[2].parse().map_err(|_| { | ||
Error::new_invalid_arg(anyhow!( | ||
"Invalid version. Patch did not parse: {}", | ||
s.to_string() | ||
)) | ||
})?; | ||
Ok(Self { | ||
major, | ||
minor, | ||
patch, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
/// Access to ceramic version information | ||
pub struct CeramicOneVersion {} | ||
|
||
impl CeramicOneVersion { | ||
/// Fetch the previous version from the database. May be None if no previous version exists. | ||
pub async fn fetch_previous(pool: &SqlitePool) -> Result<Option<VersionRow>> { | ||
let current = SemVer::from_str(env!("CARGO_PKG_VERSION"))?; | ||
VersionRow::_fetch_previous(pool, ¤t.to_string()).await | ||
} | ||
|
||
/// Insert the current version into the database | ||
pub async fn insert_current(pool: &SqlitePool) -> Result<()> { | ||
let current = SemVer::from_str(env!("CARGO_PKG_VERSION"))?; | ||
VersionRow::insert_current(pool, ¤t.to_string()).await | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
use crate::SqlitePool; | ||
|
||
#[tokio::test] | ||
async fn insert_version() { | ||
let mem = SqlitePool::connect_in_memory().await.unwrap(); | ||
CeramicOneVersion::insert_current(&mem).await.unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn prev_version() { | ||
let mem = SqlitePool::connect_in_memory().await.unwrap(); | ||
CeramicOneVersion::fetch_previous(&mem).await.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.