From edfff8269b660ef149023d859451b94c198474ba Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Mon, 28 Nov 2022 18:28:00 +0100 Subject: [PATCH] init db needs its own connection, fix #241 --- ffplayout-api/src/db/handles.rs | 20 +++++++++++++------- ffplayout-api/src/main.rs | 8 ++++---- ffplayout-api/src/utils/mod.rs | 20 +++++++++++++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/ffplayout-api/src/db/handles.rs b/ffplayout-api/src/db/handles.rs index 9a663f50..7aad6869 100644 --- a/ffplayout-api/src/db/handles.rs +++ b/ffplayout-api/src/db/handles.rs @@ -7,7 +7,10 @@ use rand::{distributions::Alphanumeric, Rng}; use simplelog::*; use sqlx::{migrate::MigrateDatabase, sqlite::SqliteQueryResult, Pool, Sqlite}; -use crate::db::models::{Channel, TextPreset, User}; +use crate::db::{ + db_pool, + models::{Channel, TextPreset, User}, +}; use crate::utils::{db_path, local_utc_offset, GlobalSettings}; #[derive(Debug, sqlx::FromRow)] @@ -74,15 +77,15 @@ async fn create_schema(conn: &Pool) -> Result, - domain: Option, -) -> Result<&'static str, Box> { +pub async fn db_init(domain: Option) -> Result<&'static str, Box> { let db_path = db_path()?; if !Sqlite::database_exists(&db_path).await.unwrap_or(false) { Sqlite::create_database(&db_path).await.unwrap(); - match create_schema(conn).await { + + let pool = db_pool().await?; + + match create_schema(&pool).await { Ok(_) => info!("Database created Successfully"), Err(e) => panic!("{e}"), } @@ -115,10 +118,13 @@ pub async fn db_init( '1', '#000000@0x80', '4', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),0,if(lt(t,ld(1)+2),(t-(ld(1)+1))/1,if(lt(t,ld(1)+8),1,if(lt(t,ld(1)+9),(1-(t-(ld(1)+8)))/1,0))))', '1'), ('Scrolling Text', 'We have a very important announcement to make.', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),w+4,w-w/12*mod(t-ld(1),12*(w+tw)/w))', '(h-line_h)*0.9', '24', '4', '#ffffff', '1', '#000000@0x80', '4', '1.0', '1');"; + + let pool = db_pool().await?; + sqlx::query(query) .bind(secret) .bind(url) - .execute(conn) + .execute(&pool) .await?; Ok("Database initialized!") diff --git a/ffplayout-api/src/main.rs b/ffplayout-api/src/main.rs index 9604dee8..0fc9775f 100644 --- a/ffplayout-api/src/main.rs +++ b/ffplayout-api/src/main.rs @@ -64,6 +64,10 @@ async fn main() -> std::io::Result<()> { let logging = init_logging(&config, None, None); CombinedLogger::init(logging).unwrap(); + if let Err(c) = run_args(args.clone()).await { + exit(c); + } + let pool = match db_pool().await { Ok(p) => p, Err(e) => { @@ -72,10 +76,6 @@ async fn main() -> std::io::Result<()> { } }; - if let Err(c) = run_args(&pool, args.clone()).await { - exit(c); - } - if let Some(conn) = args.listen { if let Ok(p) = db_path() { if !Path::new(&p).is_file() { diff --git a/ffplayout-api/src/utils/mod.rs b/ffplayout-api/src/utils/mod.rs index 42cfd12d..c33fbdc6 100644 --- a/ffplayout-api/src/utils/mod.rs +++ b/ffplayout-api/src/utils/mod.rs @@ -21,6 +21,7 @@ pub mod files; pub mod playlist; use crate::db::{ + db_pool, handles::{db_init, insert_user, select_channel, select_global}, models::{Channel, User}, }; @@ -90,7 +91,7 @@ pub fn db_path() -> Result> { Ok(db_path) } -pub async fn run_args(conn: &Pool, mut args: Args) -> Result<(), i32> { +pub async fn run_args(mut args: Args) -> Result<(), i32> { if !args.init && args.listen.is_none() && !args.ask && args.username.is_none() { error!("Wrong number of arguments! Run ffpapi --help for more information."); @@ -98,7 +99,7 @@ pub async fn run_args(conn: &Pool, mut args: Args) -> Result<(), i32> { } if args.init { - if let Err(e) = db_init(conn, args.domain).await { + if let Err(e) = db_init(args.domain).await { panic!("{e}"); }; @@ -162,9 +163,18 @@ pub async fn run_args(conn: &Pool, mut args: Args) -> Result<(), i32> { token: None, }; - if let Err(e) = insert_user(conn, user).await { - error!("{e}"); - return Err(1); + match db_pool().await { + Ok(conn) => { + if let Err(e) = insert_user(&conn, user).await { + error!("{e}"); + return Err(1); + }; + } + + Err(e) => { + error!("{e}"); + return Err(1); + } }; info!("Create admin user \"{username}\" done...");