summaryrefslogtreecommitdiff
path: root/src/db.rs
blob: e9d001f04bfe5d7f9cccf735ef4bb5a672843fd2 (plain) (blame)
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
use std::env;

use serde::Serialize;
use sqlx::{PgPool, postgres::PgPoolOptions};
use tracing::{debug, info};

use crate::error::WebolError;

#[derive(Serialize)]
pub struct Device {
    pub id: String,
    pub mac: String,
    pub broadcast_addr: String
}

impl Device {
    async fn init(db: &PgPool) -> Result<(), WebolError> {
        sqlx::query!(r#"
            CREATE TABLE IF NOT EXISTS "devices"
            (
                "id"                TEXT PRIMARY KEY NOT NULL,
                "mac"               TEXT NOT NULL,
                "broadcast_addr"    TEXT NOT NULL
            );"#
        ).execute(db).await.map_err(|err| WebolError::Server(Box::new(err)))?;

        Ok(())
    }
}

pub async fn setup_db(db: &PgPool) -> Result<(), WebolError> {
    Device::init(db).await
}

pub async fn init_db_pool() -> PgPool {
    #[cfg(not(debug_assertions))]
    let db_url = SETTINGS.get_string("database.url").unwrap();

    #[cfg(debug_assertions)]
    let db_url = env::var("DATABASE_URL").unwrap();

    debug!("attempt to connect dbPool to '{}'", db_url);

    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(&db_url)
        .await
        .unwrap();

    info!("dbPool successfully connected to '{}'", db_url);

    pool
}