diff --git a/CHANGELOG.md b/CHANGELOG.md index 31ea187..0c8bf94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,20 @@ and this project adheres to a _modified_ form of _[Semantic Versioning][semver]_ ### Removed +## [0.3.2] + +### Added + +### Changed + +### Fixed + +- Fix `stelae update` partial update when there are new publications ([56]) + +### Removed + +[56]: https://github.com/openlawlibrary/stelae/pull/56 + ## [0.3.1] ### Added @@ -110,7 +124,8 @@ and this project adheres to a _modified_ form of _[Semantic Versioning][semver]_ ### Removed -[Unreleased]: https://github.com/openlawlibrary/stelae/compare/v0.3.1...HEAD +[Unreleased]: https://github.com/openlawlibrary/stelae/compare/v0.3.2...HEAD +[0.3.2]: https://github.com/openlawlibrary/stelae/compare/v0.3.1...v0.3.2 [0.3.1]: https://github.com/openlawlibrary/stelae/compare/v0.3.0...v0.3.1 [0.3.0]: https://github.com/openlawlibrary/stelae/compare/v0.2.1...v0.3.0 [0.2.1]: https://github.com/openlawlibrary/stelae/compare/v0.2.0...v0.2.1 diff --git a/Cargo.lock b/Cargo.lock index 459aaed..d188713 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3110,7 +3110,7 @@ dependencies = [ [[package]] name = "stelae" -version = "0.3.1" +version = "0.3.2" dependencies = [ "actix-http", "actix-service", diff --git a/Cargo.toml b/Cargo.toml index c8fc63f..cba070e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "stelae" description = "A collection of tools in Rust and Python for preserving, authenticating, and accessing laws in perpetuity." -version = "0.3.1" +version = "0.3.2" edition = "2021" readme = "README.md" license = "AGPL-3.0" diff --git a/migrations/postgres/.keep b/migrations/postgres/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/db/init.rs b/src/db/init.rs index fd3f6d8..6219d72 100644 --- a/src/db/init.rs +++ b/src/db/init.rs @@ -20,11 +20,6 @@ pub async fn connect(archive_path: &Path) -> anyhow::Result .run(&connection.pool) .await?; } - DatabaseKind::Postgres => { - sqlx::migrate!("./migrations/postgres") - .run(&connection.pool) - .await?; - } } Ok(connection) } diff --git a/src/db/mod.rs b/src/db/mod.rs index 8954e47..55d7e42 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -40,8 +40,6 @@ pub trait Tx { pub enum DatabaseKind { /// Sqlite database. Sqlite, - /// Postgres database. - Postgres, } /// Database connection. @@ -78,10 +76,6 @@ impl Db for DatabaseConnection { pool, kind: DatabaseKind::Sqlite, }, - url if url.starts_with("postgres://") => Self { - pool, - kind: DatabaseKind::Postgres, - }, _ => anyhow::bail!("Unsupported database URL: {}", db_url), }; diff --git a/src/db/models/document_change/manager.rs b/src/db/models/document_change/manager.rs index faed424..e2d9b28 100644 --- a/src/db/models/document_change/manager.rs +++ b/src/db/models/document_change/manager.rs @@ -27,7 +27,7 @@ impl super::Manager for DatabaseConnection { WHERE dc.doc_mpath LIKE $1 AND phpv.publication_id = $2 "; let mut rows = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, Version>(statement) .bind(format!("{mpath}%")) @@ -45,7 +45,7 @@ impl super::Manager for DatabaseConnection { LIMIT 1 "; let element_added = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, Version>(statement) .bind(mpath) @@ -76,7 +76,7 @@ impl super::Manager for DatabaseConnection { doc.push('|'); let document_effective = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, Version>(statement) .bind(doc) diff --git a/src/db/models/document_element/manager.rs b/src/db/models/document_element/manager.rs index 3b7e991..6467367 100644 --- a/src/db/models/document_element/manager.rs +++ b/src/db/models/document_element/manager.rs @@ -20,7 +20,7 @@ impl super::Manager for DatabaseConnection { LIMIT 1 "; let row = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, (String,)>(statement) .bind(url) diff --git a/src/db/models/library/manager.rs b/src/db/models/library/manager.rs index bba67e9..dd0f665 100644 --- a/src/db/models/library/manager.rs +++ b/src/db/models/library/manager.rs @@ -18,7 +18,7 @@ impl super::Manager for DatabaseConnection { LIMIT 1 "; let row = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, (String,)>(statement) .bind(url) diff --git a/src/db/models/library_change/manager.rs b/src/db/models/library_change/manager.rs index 1f83e29..6dd4b48 100644 --- a/src/db/models/library_change/manager.rs +++ b/src/db/models/library_change/manager.rs @@ -28,7 +28,7 @@ impl super::Manager for DatabaseConnection { WHERE cld.library_mpath LIKE $1 AND phpv.publication_id = $2 "; let mut rows = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, Version>(statement) .bind(format!("{mpath}%")) @@ -46,7 +46,7 @@ impl super::Manager for DatabaseConnection { LIMIT 1 "; let element_added = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, Version>(statement) .bind(format!("{mpath}%")) diff --git a/src/db/models/publication/manager.rs b/src/db/models/publication/manager.rs index 0be81b8..3745c14 100644 --- a/src/db/models/publication/manager.rs +++ b/src/db/models/publication/manager.rs @@ -22,7 +22,7 @@ impl super::Manager for DatabaseConnection { ORDER BY name DESC "; let rows = match self.kind { - DatabaseKind::Postgres | DatabaseKind::Sqlite => { + DatabaseKind::Sqlite => { let mut connection = self.pool.acquire().await?; sqlx::query_as::<_, Publication>(statement) .bind(stele) diff --git a/src/db/models/publication_version/manager.rs b/src/db/models/publication_version/manager.rs index 51dacab..cc2d6c4 100644 --- a/src/db/models/publication_version/manager.rs +++ b/src/db/models/publication_version/manager.rs @@ -101,7 +101,7 @@ impl super::TxManager for DatabaseTransaction { /// Recursively find all publication versions starting from a given publication ID. - /// This is necessary publication versions can be the same across publications. + /// This is necessary because publication versions can be the same across publications. /// To make versions query simpler, we walk the publication hierarchy starting from /// `publication_name` looking for related publications. /// The function returns all the `publication_version` IDs, even in simple cases where a publication diff --git a/src/history/changes.rs b/src/history/changes.rs index 355931e..d73fad2 100644 --- a/src/history/changes.rs +++ b/src/history/changes.rs @@ -189,6 +189,25 @@ async fn load_delta_from_publications( let publications_dir_entry = tree.get_path(&PathBuf::from("_publication"))?; let publications_subtree = rdf_repo.repo.find_tree(publications_dir_entry.id())?; let mut last_inserted_date: Option = None; + let last_inserted_pub_date = if let Some(last_inserted_pub) = last_inserted_publication.as_ref() + { + last_inserted_date = + publication_version::TxManager::find_last_inserted_date_by_publication_id( + tx, + &last_inserted_pub.id, + ) + .await? + .map(|pv| { + NaiveDate::parse_from_str(&pv.version, "%Y-%m-%d").context("Could not parse date") + }) + .and_then(Result::ok); + Some(NaiveDate::parse_from_str( + &last_inserted_pub.date, + "%Y-%m-%d", + )?) + } else { + None + }; for publication_entry in &publications_subtree { let mut pub_graph = StelaeGraph::new(); let object = publication_entry.to_object(&rdf_repo.repo)?; @@ -208,26 +227,13 @@ async fn load_delta_from_publications( let pub_date = pub_graph.literal_from_triple_matching(None, Some(dcterms::available), None)?; let pub_date = NaiveDate::parse_from_str(pub_date.as_str(), "%Y-%m-%d")?; - if let Some(last_inserted_pub) = last_inserted_publication.as_ref() { - let last_inserted_pub_date = - NaiveDate::parse_from_str(&last_inserted_pub.date, "%Y-%m-%d")?; - // continue from last inserted publication, since that publication can contain - // new changes (versions) that are not in db - if pub_date < last_inserted_pub_date { + // continue from last inserted publication, since that publication can contain + // new changes (versions) that are not in db + if let Some(last_inserted_publication_date) = last_inserted_pub_date { + if pub_date < last_inserted_publication_date { // skip past publications since they are already in db continue; } - last_inserted_date = - publication_version::TxManager::find_last_inserted_date_by_publication_id( - tx, - &last_inserted_pub.id, - ) - .await? - .map(|pv| { - NaiveDate::parse_from_str(&pv.version, "%Y-%m-%d") - .context("Could not parse date") - }) - .and_then(Result::ok); } tracing::info!("[{stele}] | Publication: {pub_name}"); publication_tree.walk(TreeWalkMode::PreOrder, |_, entry| { @@ -274,6 +280,8 @@ async fn load_delta_from_publications( let publication = publication::TxManager::find_by_name_and_stele(tx, &pub_name, stele).await?; load_delta_for_publication(tx, publication, &pub_graph, last_inserted_date).await?; + // reset last inserted date for next publication + last_inserted_date = None; } Ok(()) }