summaryrefslogtreecommitdiffstats
path: root/src/db/models/githash.rs
blob: df7937c20980e205440bee53e1eee117b1c3e8c9 (plain)
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
use anyhow::Error;
use anyhow::Result;
use diesel::PgConnection;
use diesel::prelude::*;

use crate::schema::githashes::*;
use crate::schema::githashes;

#[derive(Queryable)]
pub struct GitHash {
    pub id: i32,
    pub hash: String,
}

#[derive(Insertable)]
#[table_name="githashes"]
struct NewGitHash<'a> {
    pub hash: &'a str,
}

impl GitHash {
    pub fn create_or_fetch(database_connection: &PgConnection, githash: &str) -> Result<GitHash> {
        let new_hash = NewGitHash { hash: githash };

        diesel::insert_into(githashes::table)
            .values(&new_hash)
            .on_conflict_do_nothing()
            .execute(database_connection)?;

        dsl::githashes
            .filter(hash.eq(githash))
            .first::<GitHash>(database_connection)
            .map_err(Error::from)
    }
}