init db needs its own connection, fix #241
This commit is contained in:
parent
52856d3f09
commit
edfff8269b
@ -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<Sqlite>) -> Result<SqliteQueryResult, sqlx::E
|
||||
sqlx::query(query).execute(conn).await
|
||||
}
|
||||
|
||||
pub async fn db_init(
|
||||
conn: &Pool<Sqlite>,
|
||||
domain: Option<String>,
|
||||
) -> Result<&'static str, Box<dyn std::error::Error>> {
|
||||
pub async fn db_init(domain: Option<String>) -> Result<&'static str, Box<dyn std::error::Error>> {
|
||||
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!")
|
||||
|
@ -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() {
|
||||
|
@ -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<String, Box<dyn std::error::Error>> {
|
||||
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() {
|
||||
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 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<Sqlite>, 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...");
|
||||
|
Loading…
x
Reference in New Issue
Block a user