aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--migrations/20231009123228_devices.sql4
-rw-r--r--src/db.rs21
-rw-r--r--src/main.rs4
3 files changed, 4 insertions, 25 deletions
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 @@
1-- Add migration script here 1-- Add migration script here
2CREATE TABLE "devices" 2CREATE TABLE IF NOT EXISTS "devices"
3( 3(
4 "id" TEXT PRIMARY KEY NOT NULL, 4 "id" TEXT PRIMARY KEY NOT NULL,
5 "mac" TEXT NOT NULL, 5 "mac" TEXT NOT NULL,
6 "broadcast_addr" TEXT NOT NULL 6 "broadcast_addr" TEXT NOT NULL
7) \ No newline at end of file 7)
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;
4use sqlx::{PgPool, postgres::PgPoolOptions}; 4use sqlx::{PgPool, postgres::PgPoolOptions};
5use tracing::{debug, info}; 5use tracing::{debug, info};
6 6
7use crate::error::WebolError;
8
9#[derive(Serialize)] 7#[derive(Serialize)]
10pub struct Device { 8pub struct Device {
11 pub id: String, 9 pub id: String,
@@ -13,25 +11,6 @@ pub struct Device {
13 pub broadcast_addr: String 11 pub broadcast_addr: String
14} 12}
15 13
16impl Device {
17 async fn init(db: &PgPool) -> Result<(), WebolError> {
18 sqlx::query!(r#"
19 CREATE TABLE IF NOT EXISTS "devices"
20 (
21 "id" TEXT PRIMARY KEY NOT NULL,
22 "mac" TEXT NOT NULL,
23 "broadcast_addr" TEXT NOT NULL
24 );"#
25 ).execute(db).await.map_err(|err| WebolError::Server(Box::new(err)))?;
26
27 Ok(())
28 }
29}
30
31pub async fn setup_db(db: &PgPool) -> Result<(), WebolError> {
32 Device::init(db).await
33}
34
35pub async fn init_db_pool() -> PgPool { 14pub async fn init_db_pool() -> PgPool {
36 #[cfg(not(debug_assertions))] 15 #[cfg(not(debug_assertions))]
37 let db_url = SETTINGS.get_string("database.url").unwrap(); 16 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;
7use tracing::{info, level_filters::LevelFilter}; 7use tracing::{info, level_filters::LevelFilter};
8use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; 8use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*};
9use crate::config::SETTINGS; 9use crate::config::SETTINGS;
10use crate::db::{init_db_pool, setup_db}; 10use crate::db::init_db_pool;
11use crate::routes::device::{get_device, post_device, put_device}; 11use crate::routes::device::{get_device, post_device, put_device};
12use crate::routes::start::start; 12use crate::routes::start::start;
13 13
@@ -41,7 +41,7 @@ async fn main() {
41 info!("start webol v{}", version); 41 info!("start webol v{}", version);
42 42
43 let db = init_db_pool().await; 43 let db = init_db_pool().await;
44 setup_db(&db).await.unwrap(); 44 sqlx::migrate!().run(&db).await.unwrap();
45 45
46 let shared_state = Arc::new(AppState { db }); 46 let shared_state = Arc::new(AppState { db });
47 47