From f5928b90748b0bb4c0c498ccc77ebde4eaec8841 Mon Sep 17 00:00:00 2001 From: fx Date: Sat, 21 Oct 2023 20:46:31 +0200 Subject: add init function for db tables --- src/db.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 87943ca..e9d001f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,8 +1,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 -} \ No newline at end of file +} + +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 +} -- cgit v1.2.3