diff options
author | Matthias Beyer <matthias.beyer@atos.net> | 2021-01-07 15:19:38 +0100 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@atos.net> | 2021-01-14 13:29:27 +0100 |
commit | 1cd2ed0bca0bbf64759aca8e686631a6f17a0e10 (patch) | |
tree | 33453d0778f09a6fa8a13192e727a0e4ae50646d /src/db | |
parent | 023421f8c91a1e1ab36d6dbfa989dcbafdd59de4 (diff) |
Update implementation for dedicated release table
This patch updates the implementation for the dedicated release table.
First of all, it updates the model types and adds the new
`crate::db::models::Release` type for representing releases in the
database (which are just pointers to the released artifact).
The `db artifacts` command is updated by using a LEFT JOIN with the
"releases" table.
The `release` command is updated as well, though the change was a bit
more complex.
First of all, a LEFT OUTER JOIN was added which is essentially the same
as the filter for un-released artifacts from before the change.
Then, a `.select()` call was added, so we don't load all the objects we
don't need anyways.
This reduces overhead not only with the database querying but also in
the code here, as we do not allocate objects from the database we
destruct right away.
The updating of the artifact object in the database was changed to
generate a new release object in the "releases" table, of course.
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/models/artifact.rs | 10 | ||||
-rw-r--r-- | src/db/models/mod.rs | 3 | ||||
-rw-r--r-- | src/db/models/releases.rs | 44 |
3 files changed, 53 insertions, 4 deletions
diff --git a/src/db/models/artifact.rs b/src/db/models/artifact.rs index 01de301..01ac4c7 100644 --- a/src/db/models/artifact.rs +++ b/src/db/models/artifact.rs @@ -15,6 +15,7 @@ use anyhow::anyhow; use anyhow::Error; use anyhow::Context; use anyhow::Result; +use chrono::NaiveDateTime; use diesel::PgConnection; use diesel::prelude::*; @@ -27,7 +28,6 @@ use crate::schema::artifacts; pub struct Artifact { pub id: i32, pub path: String, - pub released: bool, pub job_id: i32, } @@ -35,7 +35,6 @@ pub struct Artifact { #[table_name="artifacts"] struct NewArtifact<'a> { pub path: &'a str, - pub released: bool, pub job_id: i32, } @@ -44,13 +43,16 @@ impl Artifact { PathBuf::from(&self.path) } - pub fn create(database_connection: &PgConnection, art_path: &ArtifactPath, art_released: bool, job: &Job) -> Result<Artifact> { + pub fn released(self, database_connection: &PgConnection, release_date: &NaiveDateTime) -> Result<crate::db::models::Release> { + crate::db::models::Release::create(database_connection, &self, release_date) + } + + pub fn create(database_connection: &PgConnection, art_path: &ArtifactPath, job: &Job) -> Result<Artifact> { let path_str = art_path.to_str() .ok_or_else(|| anyhow!("Path is not valid UTF-8: {}", art_path.display())) .context("Writing artifact to database")?; let new_art = NewArtifact { path: path_str, - released: art_released, job_id: job.id, }; diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs index cd7aa72..77ebc51 100644 --- a/src/db/models/mod.rs +++ b/src/db/models/mod.rs @@ -32,6 +32,9 @@ pub use githash::*; mod package; pub use package::*; +mod releases; +pub use releases::*; + mod submit; pub use submit::*; diff --git a/src/db/models/releases.rs b/src/db/models/releases.rs new file mode 100644 index 0000000..1975d2c --- /dev/null +++ b/src/db/models/releases.rs @@ -0,0 +1,44 @@ +use anyhow::Error; +use anyhow::Result; +use chrono::NaiveDateTime; +use diesel::PgConnection; +use diesel::prelude::*; + +use crate::db::models::Artifact; +use crate::schema::releases::*; +use crate::schema::releases; + +#[derive(Debug, Identifiable, Queryable, Associations)] +#[belongs_to(Artifact)] +pub struct Release { + pub id: i32, + pub artifact_id: i32, + pub release_date: NaiveDateTime, +} + +#[derive(Insertable)] +#[table_name="releases"] +struct NewRelease<'a> { + pub artifact_id: i32, + pub release_date: &'a NaiveDateTime, +} + +impl Release { + pub fn create<'a>(database_connection: &PgConnection, art: &Artifact, date: &'a NaiveDateTime) -> Result<Release> { + let new_rel = NewRelease { + artifact_id: art.id, + release_date: date, + }; + + diesel::insert_into(releases::table) + .values(&new_rel) + .execute(database_connection)?; + + dsl::releases + .filter(artifact_id.eq(art.id).and(release_date.eq(date))) + .first::<Release>(database_connection) + .map_err(Error::from) + } +} + + |