summaryrefslogtreecommitdiffstats
path: root/src/db/models/job_env.rs
blob: f2c5fb1309d90e09549355ec6f63cd418ee93c99 (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::Result;
use diesel::PgConnection;
use diesel::prelude::*;

use crate::schema::job_envs;
use crate::db::models::Job;
use crate::db::models::EnvVar;

#[derive(Identifiable, Queryable, Associations)]
#[belongs_to(Job)]
#[belongs_to(EnvVar, foreign_key = "env_id")]
#[table_name="job_envs"]
pub struct JobEnv {
    pub id: i32,
    pub job_id: i32,
    pub env_id: i32,
}

#[derive(Insertable)]
#[table_name="job_envs"]
struct NewJobEnv {
    pub job_id: i32,
    pub env_id: i32,
}

impl JobEnv {
    pub fn create(database_connection: &PgConnection, job: &Job, env: &EnvVar) -> Result<()> {
        let new_jobenv = NewJobEnv { job_id: job.id, env_id: env.id };

        diesel::insert_into(job_envs::table)
            .values(&new_jobenv)
            .execute(database_connection)?;
        Ok(())
    }
}