Skip to content

Commit

Permalink
Merge pull request #56 from openlawlibrary/ndusan/partial-update-fixes
Browse files Browse the repository at this point in the history
fix: partial update fixes
  • Loading branch information
n-dusan authored Nov 1, 2024
2 parents 8c40d26 + c722e83 commit ce3142d
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 40 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Empty file removed migrations/postgres/.keep
Empty file.
5 changes: 0 additions & 5 deletions src/db/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ pub async fn connect(archive_path: &Path) -> anyhow::Result<DatabaseConnection>
.run(&connection.pool)
.await?;
}
DatabaseKind::Postgres => {
sqlx::migrate!("./migrations/postgres")
.run(&connection.pool)
.await?;
}
}
Ok(connection)
}
6 changes: 0 additions & 6 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ pub trait Tx {
pub enum DatabaseKind {
/// Sqlite database.
Sqlite,
/// Postgres database.
Postgres,
}

/// Database connection.
Expand Down Expand Up @@ -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),
};

Expand Down
6 changes: 3 additions & 3 deletions src/db/models/document_change/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}%"))
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/db/models/document_element/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/db/models/library/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/db/models/library_change/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}%"))
Expand All @@ -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}%"))
Expand Down
2 changes: 1 addition & 1 deletion src/db/models/publication/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/db/models/publication_version/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 25 additions & 17 deletions src/history/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NaiveDate> = 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)?;
Expand All @@ -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| {
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit ce3142d

Please sign in to comment.