Better DB default fields. (#1560)

* Better DB default fields.

* Fixing clippy
remove_extra_save_user_fields
Dessalines 2021-04-08 07:19:58 -04:00 committed by GitHub
parent 3a01949f81
commit 1a70477fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 38 deletions

View File

@ -21,20 +21,15 @@ impl Default for Settings {
} }
} }
pub(in crate::settings) static DEFAULT_DATABASE_USER: &str = "lemmy";
pub(in crate::settings) static DEFAULT_DATABASE_PORT: i32 = 5432;
pub(in crate::settings) static DEFAULT_DATABASE_DB: &str = "lemmy";
pub static DEFAULT_DATABASE_POOL_SIZE: u32 = 5;
impl Default for DatabaseConfig { impl Default for DatabaseConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
user: Some(DEFAULT_DATABASE_USER.to_string()), user: Some("lemmy".to_string()),
password: "password".into(), password: "password".into(),
host: "localhost".into(), host: "localhost".into(),
port: Some(DEFAULT_DATABASE_PORT), port: Some(5432),
database: Some(DEFAULT_DATABASE_DB.to_string()), database: Some("lemmy".to_string()),
pool_size: Some(DEFAULT_DATABASE_POOL_SIZE), pool_size: Some(5),
} }
} }
} }

View File

@ -1,8 +1,6 @@
use crate::{ use crate::{
location_info, location_info,
settings::{ settings::structs::{
defaults::{DEFAULT_DATABASE_DB, DEFAULT_DATABASE_PORT, DEFAULT_DATABASE_USER},
structs::{
CaptchaConfig, CaptchaConfig,
DatabaseConfig, DatabaseConfig,
EmailConfig, EmailConfig,
@ -11,7 +9,6 @@ use crate::{
Settings, Settings,
SetupConfig, SetupConfig,
}, },
},
LemmyError, LemmyError,
}; };
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
@ -63,15 +60,11 @@ impl Settings {
let conf = self.database(); let conf = self.database();
format!( format!(
"postgres://{}:{}@{}:{}/{}", "postgres://{}:{}@{}:{}/{}",
conf conf.user(),
.user
.unwrap_or_else(|| DEFAULT_DATABASE_USER.to_string()),
conf.password, conf.password,
conf.host, conf.host,
conf.port.unwrap_or(DEFAULT_DATABASE_PORT), conf.port(),
conf conf.database(),
.database
.unwrap_or_else(|| DEFAULT_DATABASE_DB.to_string()),
) )
} }

View File

@ -27,12 +27,40 @@ pub struct CaptchaConfig {
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct DatabaseConfig { pub struct DatabaseConfig {
pub user: Option<String>, pub(super) user: Option<String>,
pub password: String, pub password: String,
pub host: String, pub host: String,
pub port: Option<i32>, pub(super) port: Option<i32>,
pub database: Option<String>, pub(super) database: Option<String>,
pub pool_size: Option<u32>, pub(super) pool_size: Option<u32>,
}
impl DatabaseConfig {
pub fn user(&self) -> String {
self
.user
.to_owned()
.unwrap_or_else(|| DatabaseConfig::default().user.expect("missing user"))
}
pub fn port(&self) -> i32 {
self
.port
.unwrap_or_else(|| DatabaseConfig::default().port.expect("missing port"))
}
pub fn database(&self) -> String {
self.database.to_owned().unwrap_or_else(|| {
DatabaseConfig::default()
.database
.expect("missing database")
})
}
pub fn pool_size(&self) -> u32 {
self.pool_size.unwrap_or_else(|| {
DatabaseConfig::default()
.pool_size
.expect("missing pool_size")
})
}
} }
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]

View File

@ -16,7 +16,7 @@ use lemmy_routes::{feeds, images, nodeinfo, webfinger};
use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks}; use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
use lemmy_utils::{ use lemmy_utils::{
rate_limit::{rate_limiter::RateLimiter, RateLimit}, rate_limit::{rate_limiter::RateLimiter, RateLimit},
settings::{defaults::DEFAULT_DATABASE_POOL_SIZE, structs::Settings}, settings::structs::Settings,
LemmyError, LemmyError,
}; };
use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
@ -38,12 +38,7 @@ async fn main() -> Result<(), LemmyError> {
}; };
let manager = ConnectionManager::<PgConnection>::new(&db_url); let manager = ConnectionManager::<PgConnection>::new(&db_url);
let pool = Pool::builder() let pool = Pool::builder()
.max_size( .max_size(settings.database().pool_size())
settings
.database()
.pool_size
.unwrap_or(DEFAULT_DATABASE_POOL_SIZE),
)
.build(manager) .build(manager)
.unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));