init db needs its own connection, fix #241

This commit is contained in:
jb-alvarado 2022-11-28 18:28:00 +01:00
parent 52856d3f09
commit edfff8269b
3 changed files with 32 additions and 16 deletions

View File

@ -7,7 +7,10 @@ use rand::{distributions::Alphanumeric, Rng};
use simplelog::*; use simplelog::*;
use sqlx::{migrate::MigrateDatabase, sqlite::SqliteQueryResult, Pool, Sqlite}; 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}; use crate::utils::{db_path, local_utc_offset, GlobalSettings};
#[derive(Debug, sqlx::FromRow)] #[derive(Debug, sqlx::FromRow)]
@ -74,15 +77,15 @@ async fn create_schema(conn: &Pool<Sqlite>) -> Result<SqliteQueryResult, sqlx::E
sqlx::query(query).execute(conn).await sqlx::query(query).execute(conn).await
} }
pub async fn db_init( pub async fn db_init(domain: Option<String>) -> Result<&'static str, Box<dyn std::error::Error>> {
conn: &Pool<Sqlite>,
domain: Option<String>,
) -> Result<&'static str, Box<dyn std::error::Error>> {
let db_path = db_path()?; let db_path = db_path()?;
if !Sqlite::database_exists(&db_path).await.unwrap_or(false) { if !Sqlite::database_exists(&db_path).await.unwrap_or(false) {
Sqlite::create_database(&db_path).await.unwrap(); 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"), Ok(_) => info!("Database created Successfully"),
Err(e) => panic!("{e}"), 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'), '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', ('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');"; '24', '4', '#ffffff', '1', '#000000@0x80', '4', '1.0', '1');";
let pool = db_pool().await?;
sqlx::query(query) sqlx::query(query)
.bind(secret) .bind(secret)
.bind(url) .bind(url)
.execute(conn) .execute(&pool)
.await?; .await?;
Ok("Database initialized!") Ok("Database initialized!")

View File

@ -64,6 +64,10 @@ async fn main() -> std::io::Result<()> {
let logging = init_logging(&config, None, None); let logging = init_logging(&config, None, None);
CombinedLogger::init(logging).unwrap(); CombinedLogger::init(logging).unwrap();
if let Err(c) = run_args(args.clone()).await {
exit(c);
}
let pool = match db_pool().await { let pool = match db_pool().await {
Ok(p) => p, Ok(p) => p,
Err(e) => { 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 Some(conn) = args.listen {
if let Ok(p) = db_path() { if let Ok(p) = db_path() {
if !Path::new(&p).is_file() { if !Path::new(&p).is_file() {

View File

@ -21,6 +21,7 @@ pub mod files;
pub mod playlist; pub mod playlist;
use crate::db::{ use crate::db::{
db_pool,
handles::{db_init, insert_user, select_channel, select_global}, handles::{db_init, insert_user, select_channel, select_global},
models::{Channel, User}, models::{Channel, User},
}; };
@ -90,7 +91,7 @@ pub fn db_path() -> Result<String, Box<dyn std::error::Error>> {
Ok(db_path) Ok(db_path)
} }
pub async fn run_args(conn: &Pool<Sqlite>, 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() { if !args.init && args.listen.is_none() && !args.ask && args.username.is_none() {
error!("Wrong number of arguments! Run ffpapi --help for more information."); error!("Wrong number of arguments! Run ffpapi --help for more information.");
@ -98,7 +99,7 @@ pub async fn run_args(conn: &Pool<Sqlite>, mut args: Args) -> Result<(), i32> {
} }
if args.init { if args.init {
if let Err(e) = db_init(conn, args.domain).await { if let Err(e) = db_init(args.domain).await {
panic!("{e}"); panic!("{e}");
}; };
@ -162,10 +163,19 @@ pub async fn run_args(conn: &Pool<Sqlite>, mut args: Args) -> Result<(), i32> {
token: None, token: None,
}; };
if let Err(e) = insert_user(conn, user).await { match db_pool().await {
Ok(conn) => {
if let Err(e) = insert_user(&conn, user).await {
error!("{e}"); error!("{e}");
return Err(1); return Err(1);
}; };
}
Err(e) => {
error!("{e}");
return Err(1);
}
};
info!("Create admin user \"{username}\" done..."); info!("Create admin user \"{username}\" done...");