1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use chrono::NaiveDateTime;
use diesel::PgConnection;
use diesel::prelude::*;
use crate::db::models::GitHash;
use crate::db::models::Image;
use crate::db::models::Package;
use crate::schema::submits::*;
use crate::schema::submits;
#[derive(Clone, Debug, Identifiable, Queryable)]
pub struct Submit {
pub id: i32,
pub uuid: ::uuid::Uuid,
pub submit_time: NaiveDateTime,
pub requested_image_id: i32,
pub requested_package_id: i32,
pub repo_hash_id: i32,
pub tree: serde_json::Value,
}
#[derive(Insertable)]
#[table_name="submits"]
struct NewSubmit<'a> {
pub uuid: &'a ::uuid::Uuid,
pub submit_time: &'a NaiveDateTime,
pub requested_image_id: i32,
pub requested_package_id: i32,
pub repo_hash_id: i32,
pub tree: serde_json::Value,
}
impl Submit {
pub fn create(database_connection: &PgConnection,
t: &crate::package::Tree,
submit_datetime: &NaiveDateTime,
submit_id: &::uuid::Uuid,
requested_image: &Image,
requested_package: &Package,
repo_hash: &GitHash)
-> Result<Submit>
{
let tree_json = serde_json::to_value(t)
.context("Converting tree to JSON string")
.with_context(|| anyhow!("Tree = {:#?}", t))?;
let new_submit = NewSubmit {
uuid: submit_id,
submit_time: submit_datetime,
requested_image_id: requested_image.id,
requested_package_id: requested_package.id,
repo_hash_id: repo_hash.id,
tree: tree_json,
};
diesel::insert_into(submits::table)
.values(&new_submit)
.on_conflict_do_nothing()
.execute(database_connection)
.context("Inserting new submit into submits table")?;
Self::with_id(database_connection, submit_id)
}
pub fn with_id(database_connection: &PgConnection, submit_id: &::uuid::Uuid) -> Result<Submit> {
dsl::submits
.filter(submits::uuid.eq(submit_id))
.first::<Submit>(database_connection)
.context("Loading submit")
.map_err(Error::from)
}
}
|