From b6adc918c61d6e0063a4b05d1221d42787312c1c Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 9 Feb 2025 21:56:06 +0100 Subject: [PATCH] Unify database structs --- src/storage.rs | 84 ++++++++++++++++++++------------------------ src/templates.rs | 17 +++++---- templates/flake.html | 2 +- 3 files changed, 47 insertions(+), 56 deletions(-) diff --git a/src/storage.rs b/src/storage.rs index 297b52d..5d815f3 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -28,7 +28,7 @@ impl Storage { }) } - pub async fn flakes(&self) -> Result> { + pub async fn flakes(&self) -> Result> { sqlx::query_as(" SELECT flake_uri @@ -40,11 +40,15 @@ impl Storage { .context("Failed to fetch data from database") } - pub async fn revisions_from_flake(&self, uri: &str) -> Result> { + pub async fn revisions_from_flake(&self, uri: &str) -> Result> { sqlx::query_as(" SELECT revision_uri, - last_modified + flake_uri, + nix_store_path, + nar_hash, + last_modified, + tracker_last_scanned FROM revisions WHERE flake_uri = ? ORDER BY last_modified DESC @@ -55,7 +59,7 @@ impl Storage { .context("Failed to fetch data from database") } - pub async fn inputs_for_revision(&self, revision_uri: &str) -> Result> { + pub async fn inputs_for_revision(&self, revision_uri: &str) -> Result> { sqlx::query_as(" SELECT revision_uri, @@ -74,7 +78,7 @@ impl Storage { .context("Failed to fetch data from database") } - pub async fn input_of_for_revision(&self, revision_uri: &str) -> Result> { + pub async fn input_of_for_revision(&self, revision_uri: &str) -> Result> { sqlx::query_as(" SELECT revision_uri, @@ -93,14 +97,17 @@ impl Storage { .context("Failed to fetch data from database") } - pub async fn current_inputs_for_flake(&self, flake_uri: &str) -> Result> { + pub async fn current_inputs_for_flake(&self, flake_uri: &str) -> Result> { sqlx::query_as(" SELECT revisions.revision_uri, - MAX(revisions.last_modified) AS last_modified, inputs.input_name, - inputs.locked_flake_uri + inputs.locked_revision_uri, + inputs.locked_flake_uri, + inputs.locked_nar_hash, + inputs.last_modified, + MAX(revisions.last_modified) FROM revisions LEFT JOIN @@ -120,57 +127,30 @@ impl Storage { } #[derive(FromRow)] -pub struct FlakeRevisionRow { +pub struct RevisionRow { pub revision_uri: String, pub flake_uri: Option, pub nix_store_path: Option, pub nar_hash: Option, pub last_modified: Option, + pub tracker_last_scanned: Option, } -#[derive(FromRow)] -pub struct InputForFlakeRow { - pub revision_uri: String, - pub last_modified: Option, - pub input_name: String, - pub locked_flake_uri: Option, -} +impl RevisionRow { + pub fn revision_link(&self) -> String { + format!("/r/{}", urlencode(&self.revision_uri)) + } -impl InputForFlakeRow { - pub fn locked_flake_link(&self ) -> String { - match &self.locked_flake_uri { - Some(locked_flake_uri) => format!("/f/{}", urlencode(&locked_flake_uri)), + pub fn flake_link(&self) -> String { + match &self.flake_uri { + Some(flake_uri) => format!("/f/{}", urlencode(&flake_uri)), None => String::from("#"), } } } - #[derive(FromRow)] -pub struct RevisionListModel { - pub revision_uri: String, - pub last_modified: i64, -} - -impl RevisionListModel { - pub fn revision_link(&self ) -> String { - format!("/r/{}", urlencode(&self.revision_uri)) - } -} - -#[derive(FromRow)] -pub struct FlakeUri { - pub flake_uri: String, -} - -impl FlakeUri { - pub fn flake_link(&self ) -> String { - format!("/f/{}", urlencode(&self.flake_uri)) - } -} - -#[derive(FromRow)] -pub struct InputModel { +pub struct InputRow { pub revision_uri: String, pub input_name: String, pub locked_revision_uri: Option, @@ -179,7 +159,7 @@ pub struct InputModel { pub last_modified: Option, } -impl InputModel { +impl InputRow { pub fn revision_link(&self) -> String { format!("/r/{}", urlencode(&self.revision_uri)) } @@ -198,3 +178,15 @@ impl InputModel { } } } + +#[derive(FromRow)] +pub struct FlakeRow { + pub flake_uri: String, +} + +impl FlakeRow { + pub fn flake_link(&self ) -> String { + format!("/f/{}", urlencode(&self.flake_uri)) + } +} + diff --git a/src/templates.rs b/src/templates.rs index 5540d88..2f96517 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -3,10 +3,9 @@ use askama::{ }; use crate::{ storage::{ - FlakeUri, - InputModel, - InputForFlakeRow, - RevisionListModel, + FlakeRow, + InputRow, + RevisionRow, }, }; @@ -18,21 +17,21 @@ pub struct IndexTemplate { #[derive(Template)] #[template(path = "flakes.html")] pub struct FlakesTemplate { - pub flakes: Vec, + pub flakes: Vec, } #[derive(Template)] #[template(path = "flake.html")] pub struct FlakeTemplate { pub uri: String, - pub revisions: Vec, - pub current_inputs: Vec, + pub revisions: Vec, + pub current_inputs: Vec, } #[derive(Template)] #[template(path = "revision.html")] pub struct RevisionTemplate { pub revision_uri: String, - pub inputs: Vec, - pub input_of: Vec, + pub inputs: Vec, + pub input_of: Vec, } diff --git a/templates/flake.html b/templates/flake.html index e97286b..93cf39f 100644 --- a/templates/flake.html +++ b/templates/flake.html @@ -11,7 +11,7 @@