diff --git a/event-svc/src/store/mod.rs b/event-svc/src/store/mod.rs index 2844b561..fd68d407 100644 --- a/event-svc/src/store/mod.rs +++ b/event-svc/src/store/mod.rs @@ -7,5 +7,5 @@ pub use metrics::{Metrics, StoreMetricsMiddleware}; pub use sql::{ entities::{BlockHash, EventBlockRaw, EventInsertable}, BlockAccess, Error, EventAccess, EventBlockAccess, EventRowDelivered, InsertResult, - InsertedEvent, Result, SqlitePool, SqliteRootStore, VersionAccess, + InsertedEvent, Result, SqlitePool, SqliteRootStore, }; diff --git a/event-svc/src/store/sql/access/mod.rs b/event-svc/src/store/sql/access/mod.rs index 6f79976f..da56e1c9 100644 --- a/event-svc/src/store/sql/access/mod.rs +++ b/event-svc/src/store/sql/access/mod.rs @@ -1,9 +1,7 @@ mod block; mod event; mod event_block; -mod version; pub use block::BlockAccess; pub use event::{EventAccess, EventRowDelivered, InsertResult, InsertedEvent}; pub use event_block::EventBlockAccess; -pub use version::VersionAccess; diff --git a/event-svc/src/store/sql/access/version.rs b/event-svc/src/store/sql/access/version.rs deleted file mode 100644 index fcc7080b..00000000 --- a/event-svc/src/store/sql/access/version.rs +++ /dev/null @@ -1,95 +0,0 @@ -use std::str::FromStr; - -use anyhow::anyhow; - -use crate::store::sql::{entities::VersionRow, Error, Result, SqlitePool}; - -#[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 { - 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 VersionAccess {} - -impl VersionAccess { - /// Fetch the previous version from the database. May be None if no previous version exists. - pub async fn fetch_previous(pool: &SqlitePool) -> Result> { - 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 ceramic_sql::sqlite::SqlitePool; - - use super::*; - - #[tokio::test] - async fn insert_version() { - let mem = SqlitePool::connect_in_memory().await.unwrap(); - VersionAccess::insert_current(&mem).await.unwrap(); - } - - #[tokio::test] - async fn prev_version() { - let mem = SqlitePool::connect_in_memory().await.unwrap(); - VersionAccess::fetch_previous(&mem).await.unwrap(); - } -} diff --git a/event-svc/src/store/sql/entities/mod.rs b/event-svc/src/store/sql/entities/mod.rs index acfa4255..059cee3b 100644 --- a/event-svc/src/store/sql/entities/mod.rs +++ b/event-svc/src/store/sql/entities/mod.rs @@ -3,12 +3,10 @@ mod event; mod event_block; mod hash; mod utils; -mod version; pub use block::{BlockBytes, BlockRow}; pub use event::{rebuild_car, EventInsertable}; pub use event_block::{EventBlockRaw, ReconEventBlockRaw}; pub use hash::{BlockHash, ReconHash}; -pub use version::VersionRow; pub use utils::{CountRow, OrderKey}; diff --git a/event-svc/src/store/sql/entities/version.rs b/event-svc/src/store/sql/entities/version.rs deleted file mode 100644 index b30ebf90..00000000 --- a/event-svc/src/store/sql/entities/version.rs +++ /dev/null @@ -1,39 +0,0 @@ -use sqlx::types::chrono; - -use crate::store::{Result, SqlitePool}; - -#[derive(Debug, Clone, sqlx::FromRow)] -// We want to retrieve these fields for logging but we don't refer to them directly -#[allow(dead_code)] -pub struct VersionRow { - id: i64, - pub version: String, - pub installed_at: chrono::NaiveDateTime, - pub last_started_at: chrono::NaiveDateTime, -} - -impl VersionRow { - /// Return the version installed before the current version - pub async fn _fetch_previous(pool: &SqlitePool, current_version: &str) -> Result> { - Ok(sqlx::query_as( - "SELECT id, version, installed_at - FROM ceramic_one_version - WHERE version <> $1 - ORDER BY installed_at DESC limit 1;", - ) - .bind(current_version) - .fetch_optional(pool.reader()) - .await?) - } - - /// Add the current version to the database, updating the last_started_at field if the version already exists - pub async fn insert_current(pool: &SqlitePool, current_version: &str) -> Result<()> { - sqlx::query( - "INSERT INTO ceramic_one_version (version) VALUES ($1) ON CONFLICT (version) DO UPDATE set last_started_at = CURRENT_TIMESTAMP;", - ) - .bind(current_version) - .execute(pool.writer()) - .await?; - Ok(()) - } -} diff --git a/event-svc/src/store/sql/mod.rs b/event-svc/src/store/sql/mod.rs index 05cbc637..cfab9c33 100644 --- a/event-svc/src/store/sql/mod.rs +++ b/event-svc/src/store/sql/mod.rs @@ -7,7 +7,6 @@ mod test; pub use access::{ BlockAccess, EventAccess, EventBlockAccess, EventRowDelivered, InsertResult, InsertedEvent, - VersionAccess, }; pub use ceramic_sql::{sqlite::SqlitePool, Error, Result}; pub use root::SqliteRootStore; diff --git a/interest-svc/src/store/mod.rs b/interest-svc/src/store/mod.rs index ebcb0cf2..e5544ebb 100644 --- a/interest-svc/src/store/mod.rs +++ b/interest-svc/src/store/mod.rs @@ -4,6 +4,4 @@ mod metrics; mod sql; pub use metrics::{Metrics, StoreMetricsMiddleware}; -pub use sql::{ - CeramicOneInterest, CeramicOneVersion, Error, Result, SqlitePool, SqliteTransaction, -}; +pub use sql::{CeramicOneInterest, Error, Result, SqlitePool, SqliteTransaction}; diff --git a/interest-svc/src/store/sql/access/mod.rs b/interest-svc/src/store/sql/access/mod.rs index c12668ae..0443563a 100644 --- a/interest-svc/src/store/sql/access/mod.rs +++ b/interest-svc/src/store/sql/access/mod.rs @@ -1,5 +1,3 @@ mod interest; -mod version; pub use interest::CeramicOneInterest; -pub use version::CeramicOneVersion; diff --git a/interest-svc/src/store/sql/access/version.rs b/interest-svc/src/store/sql/access/version.rs deleted file mode 100644 index fd4202a6..00000000 --- a/interest-svc/src/store/sql/access/version.rs +++ /dev/null @@ -1,98 +0,0 @@ -use std::str::FromStr; - -use anyhow::anyhow; - -use crate::store::{ - 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 { - 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> { - 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::store::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(); - } -} diff --git a/interest-svc/src/store/sql/entities/mod.rs b/interest-svc/src/store/sql/entities/mod.rs index 71602a52..9785abe6 100644 --- a/interest-svc/src/store/sql/entities/mod.rs +++ b/interest-svc/src/store/sql/entities/mod.rs @@ -1,7 +1,5 @@ mod hash; mod order_key; -mod version; pub use hash::ReconHash; pub use order_key::OrderKey; -pub use version::VersionRow; diff --git a/interest-svc/src/store/sql/entities/version.rs b/interest-svc/src/store/sql/entities/version.rs deleted file mode 100644 index b30ebf90..00000000 --- a/interest-svc/src/store/sql/entities/version.rs +++ /dev/null @@ -1,39 +0,0 @@ -use sqlx::types::chrono; - -use crate::store::{Result, SqlitePool}; - -#[derive(Debug, Clone, sqlx::FromRow)] -// We want to retrieve these fields for logging but we don't refer to them directly -#[allow(dead_code)] -pub struct VersionRow { - id: i64, - pub version: String, - pub installed_at: chrono::NaiveDateTime, - pub last_started_at: chrono::NaiveDateTime, -} - -impl VersionRow { - /// Return the version installed before the current version - pub async fn _fetch_previous(pool: &SqlitePool, current_version: &str) -> Result> { - Ok(sqlx::query_as( - "SELECT id, version, installed_at - FROM ceramic_one_version - WHERE version <> $1 - ORDER BY installed_at DESC limit 1;", - ) - .bind(current_version) - .fetch_optional(pool.reader()) - .await?) - } - - /// Add the current version to the database, updating the last_started_at field if the version already exists - pub async fn insert_current(pool: &SqlitePool, current_version: &str) -> Result<()> { - sqlx::query( - "INSERT INTO ceramic_one_version (version) VALUES ($1) ON CONFLICT (version) DO UPDATE set last_started_at = CURRENT_TIMESTAMP;", - ) - .bind(current_version) - .execute(pool.writer()) - .await?; - Ok(()) - } -} diff --git a/interest-svc/src/store/sql/mod.rs b/interest-svc/src/store/sql/mod.rs index 0a753ed8..d66dfa7f 100644 --- a/interest-svc/src/store/sql/mod.rs +++ b/interest-svc/src/store/sql/mod.rs @@ -2,7 +2,7 @@ mod access; pub mod entities; mod query; -pub use access::{CeramicOneInterest, CeramicOneVersion}; +pub use access::CeramicOneInterest; pub use ceramic_sql::{ sqlite::{SqlitePool, SqliteTransaction}, Error, Result,