Testing things and being helpless about db migration stuff *shrug*

This commit is contained in:
mStar aka a person 2023-12-31 15:53:34 +01:00
parent 7cb3d0ceae
commit 6f1192a73c
4 changed files with 20 additions and 12 deletions

View file

@ -1,4 +1,4 @@
[config] [config]
database_path = "src/storage/db.sqlite" database_path = "src/storage/db.sqlite"
max_workers = 100 max_workers = 100
min_workers = 0 min_workers = 200

View file

@ -54,8 +54,8 @@ pub struct CLIArguments {
/// Path to the config file /// Path to the config file
#[arg(default_value = "config.toml")] #[arg(default_value = "config.toml")]
config: PathBuf, config: PathBuf,
/// Maximum amount of workers to use /// Maximum amount of workers to use.
/// If not set, the max is calculated depending on system resources /// If not set, the max is calculated depending on system resources.
/// A value of 0 will cause the server to be singlethreaded, potentially reducing the performance and responsiveness by a lot /// A value of 0 will cause the server to be singlethreaded, potentially reducing the performance and responsiveness by a lot
#[arg(long)] #[arg(long)]
max_workers: Option<u32>, max_workers: Option<u32>,
@ -90,7 +90,9 @@ pub fn read_from_env() -> Result<Config, Error> {
Ok(d) => d, Ok(d) => d,
Err(err) => bail!("Failed to parse config file. Error: {err}"), Err(err) => bail!("Failed to parse config file. Error: {err}"),
}; };
Ok(unsanitized.into_config(&args)) let conf = unsanitized.into_config(&args);
conf.sanity_check()?;
Ok(conf)
} }
fn calc_max_workers_from_system() -> u32 { fn calc_max_workers_from_system() -> u32 {
@ -161,3 +163,12 @@ impl ConfigRootToml {
} }
} }
} }
impl Config {
fn sanity_check(&self) -> Result<(), Error> {
if self.min_workers > self.max_workers {
bail!("Minimum amount of workers ({}) can't be greater than maximum amount of workers ({})", self.min_workers, self.max_workers)
};
Ok(())
}
}

View file

@ -17,5 +17,5 @@ mod webui;
fn main() { fn main() {
println!("{:?}", config::read_from_env()); storage::establish_connection("./src/storage/db.sqlite");
} }

View file

@ -8,24 +8,21 @@
// See the Licence for the specific language governing permissions and limitations under the Licence. // See the Licence for the specific language governing permissions and limitations under the Licence.
// //
use diesel::{prelude::*, sqlite::Sqlite}; use diesel::{prelude::*, pg::PgConnection, sqlite::Sqlite};
use diesel::pg::PgConnection;
use diesel_migrations::{EmbeddedMigrations, embed_migrations, MigrationHarness}; use diesel_migrations::{EmbeddedMigrations, embed_migrations, MigrationHarness};
use dotenvy::dotenv; use std::error::Error;
use std::{env, error::Error};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations"); pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
pub fn establish_connection(db_url: &str) -> PgConnection { pub fn establish_connection(db_url: &str) -> PgConnection {
dotenv().ok(); //SqliteConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e))
let conn = PgConnection::establish(db_url) let conn = PgConnection::establish(db_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); .unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e));
// TODO: Add migrations here // TODO: Add migrations here
conn conn
} }
fn run_migrations_sqlite(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Box<dyn Error + Send + Sync + 'static>> { fn run_migrations_sqlite(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// This will run the necessary migrations. // This will run the necessary migrations.