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/main.rs | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index b7306ea..8b4e9eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,11 @@ use std::sync::Arc; use axum::{Router, routing::post}; use axum::routing::{get, put}; use sqlx::PgPool; -use sqlx::postgres::PgPoolOptions; use time::util::local_offset; -use tracing::{debug, info, level_filters::LevelFilter}; +use tracing::{info, level_filters::LevelFilter}; use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; use crate::config::SETTINGS; +use crate::db::{init_db_pool, setup_db}; use crate::routes::device::{get_device, post_device, put_device}; use crate::routes::start::start; @@ -41,6 +41,7 @@ async fn main() { info!("start webol v{}", version); let db = init_db_pool().await; + setup_db(&db).await.unwrap(); let shared_state = Arc::new(AppState { db }); @@ -62,23 +63,3 @@ async fn main() { pub struct AppState { db: PgPool } - -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 From bf1aeb7191bfaa75f1acf47c675bc68b9fac1cd8 Mon Sep 17 00:00:00 2001 From: fx Date: Sat, 21 Oct 2023 21:11:28 +0200 Subject: add migrate macro --- migrations/20231009123228_devices.sql | 4 ++-- src/db.rs | 21 --------------------- src/main.rs | 4 ++-- 3 files changed, 4 insertions(+), 25 deletions(-) (limited to 'src/main.rs') diff --git a/migrations/20231009123228_devices.sql b/migrations/20231009123228_devices.sql index 5856c3a..9d0c7ca 100644 --- a/migrations/20231009123228_devices.sql +++ b/migrations/20231009123228_devices.sql @@ -1,7 +1,7 @@ -- Add migration script here -CREATE TABLE "devices" +CREATE TABLE IF NOT EXISTS "devices" ( "id" TEXT PRIMARY KEY NOT NULL, "mac" TEXT NOT NULL, "broadcast_addr" TEXT NOT NULL -) \ No newline at end of file +) diff --git a/src/db.rs b/src/db.rs index e9d001f..295780e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,8 +4,6 @@ use serde::Serialize; use sqlx::{PgPool, postgres::PgPoolOptions}; use tracing::{debug, info}; -use crate::error::WebolError; - #[derive(Serialize)] pub struct Device { pub id: String, @@ -13,25 +11,6 @@ pub struct Device { 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(); diff --git a/src/main.rs b/src/main.rs index 8b4e9eb..ce12cf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use time::util::local_offset; use tracing::{info, level_filters::LevelFilter}; use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; use crate::config::SETTINGS; -use crate::db::{init_db_pool, setup_db}; +use crate::db::init_db_pool; use crate::routes::device::{get_device, post_device, put_device}; use crate::routes::start::start; @@ -41,7 +41,7 @@ async fn main() { info!("start webol v{}", version); let db = init_db_pool().await; - setup_db(&db).await.unwrap(); + sqlx::migrate!().run(&db).await.unwrap(); let shared_state = Arc::new(AppState { db }); -- cgit v1.2.3