3 diesel_migrations::MigrationHarness,
8 use activitypub_federation::{fetch::object_id::ObjectId, traits::Object};
9 use chrono::NaiveDateTime;
10 use deadpool::Runtime;
15 result::{ConnectionError, ConnectionResult, Error as DieselError, Error::QueryBuilderError},
16 serialize::{Output, ToSql},
21 pg::AsyncPgConnection,
23 deadpool::{Object as PooledConnection, Pool},
24 AsyncDieselConnectionManager,
27 use diesel_migrations::EmbeddedMigrations;
28 use futures_util::{future::BoxFuture, FutureExt};
29 use lemmy_utils::{error::LemmyError, settings::structs::Settings};
30 use once_cell::sync::Lazy;
33 client::{ServerCertVerified, ServerCertVerifier},
40 time::{Duration, SystemTime},
42 use tracing::{error, info};
45 const FETCH_LIMIT_DEFAULT: i64 = 10;
46 pub const FETCH_LIMIT_MAX: i64 = 50;
47 const POOL_TIMEOUT: Option<Duration> = Some(Duration::from_secs(5));
49 pub type DbPool = Pool<AsyncPgConnection>;
51 pub async fn get_conn(pool: &DbPool) -> Result<PooledConnection<AsyncPgConnection>, DieselError> {
52 pool.get().await.map_err(|e| QueryBuilderError(e.into()))
55 pub fn get_database_url_from_env() -> Result<String, VarError> {
56 env::var("LEMMY_DATABASE_URL")
59 pub fn fuzzy_search(q: &str) -> String {
60 let replaced = q.replace('%', "\\%").replace('_', "\\_").replace(' ', "%");
61 format!("%{replaced}%")
64 pub fn limit_and_offset(
67 ) -> Result<(i64, i64), diesel::result::Error> {
68 let page = match page {
71 return Err(QueryBuilderError("Page is < 1".into()));
78 let limit = match limit {
80 if !(1..=FETCH_LIMIT_MAX).contains(&limit) {
81 return Err(QueryBuilderError(
82 format!("Fetch limit is > {FETCH_LIMIT_MAX}").into(),
88 None => FETCH_LIMIT_DEFAULT,
90 let offset = limit * (page - 1);
94 pub fn limit_and_offset_unlimited(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
95 let limit = limit.unwrap_or(FETCH_LIMIT_DEFAULT);
96 let offset = limit * (page.unwrap_or(1) - 1);
100 pub fn is_email_regex(test: &str) -> bool {
101 EMAIL_REGEX.is_match(test)
104 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
106 // An empty string is an erase
108 if !unwrapped.eq("") {
109 Some(Some(unwrapped.clone()))
118 pub fn diesel_option_overwrite_to_url(
119 opt: &Option<String>,
120 ) -> Result<Option<Option<DbUrl>>, LemmyError> {
121 match opt.as_ref().map(std::string::String::as_str) {
122 // An empty string is an erase
123 Some("") => Ok(Some(None)),
124 Some(str_url) => match Url::parse(str_url) {
125 Ok(url) => Ok(Some(Some(url.into()))),
126 Err(e) => Err(LemmyError::from_error_message(e, "invalid_url")),
132 pub fn diesel_option_overwrite_to_url_create(
133 opt: &Option<String>,
134 ) -> Result<Option<DbUrl>, LemmyError> {
135 match opt.as_ref().map(std::string::String::as_str) {
136 // An empty string is nothing
137 Some("") => Ok(None),
138 Some(str_url) => match Url::parse(str_url) {
139 Ok(url) => Ok(Some(url.into())),
140 Err(e) => Err(LemmyError::from_error_message(e, "invalid_url")),
146 async fn build_db_pool_settings_opt(settings: Option<&Settings>) -> Result<DbPool, LemmyError> {
147 let db_url = get_database_url(settings);
148 let pool_size = settings.map(|s| s.database.pool_size).unwrap_or(5);
149 // We only support TLS with sslmode=require currently
150 let tls_enabled = db_url.contains("sslmode=require");
151 let manager = if tls_enabled {
152 // diesel-async does not support any TLS connections out of the box, so we need to manually
153 // provide a setup function which handles creating the connection
154 AsyncDieselConnectionManager::<AsyncPgConnection>::new_with_setup(&db_url, establish_connection)
156 AsyncDieselConnectionManager::<AsyncPgConnection>::new(&db_url)
158 let pool = Pool::builder(manager)
160 .wait_timeout(POOL_TIMEOUT)
161 .create_timeout(POOL_TIMEOUT)
162 .recycle_timeout(POOL_TIMEOUT)
163 .runtime(Runtime::Tokio1)
166 // If there's no settings, that means its a unit test, and migrations need to be run
167 if settings.is_none() {
168 run_migrations(&db_url);
174 fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConnection>> {
176 let rustls_config = rustls::ClientConfig::builder()
177 .with_safe_defaults()
178 .with_custom_certificate_verifier(Arc::new(NoCertVerifier {}))
179 .with_no_client_auth();
181 let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
182 let (client, conn) = tokio_postgres::connect(config, tls)
184 .map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
185 tokio::spawn(async move {
186 if let Err(e) = conn.await {
187 error!("Database connection failed: {e}");
190 AsyncPgConnection::try_from(client).await
195 struct NoCertVerifier {}
197 impl ServerCertVerifier for NoCertVerifier {
198 fn verify_server_cert(
200 _end_entity: &rustls::Certificate,
201 _intermediates: &[rustls::Certificate],
202 _server_name: &ServerName,
203 _scts: &mut dyn Iterator<Item = &[u8]>,
204 _ocsp_response: &[u8],
206 ) -> Result<ServerCertVerified, rustls::Error> {
207 // Will verify all (even invalid) certs without any checks (sslmode=require)
208 Ok(ServerCertVerified::assertion())
212 pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
214 pub fn run_migrations(db_url: &str) {
215 // Needs to be a sync connection
217 PgConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {db_url}: {e}"));
218 info!("Running Database migrations (This may take a long time)...");
220 .run_pending_migrations(MIGRATIONS)
221 .unwrap_or_else(|e| panic!("Couldn't run DB Migrations: {e}"));
222 info!("Database migrations complete.");
225 pub async fn build_db_pool(settings: &Settings) -> Result<DbPool, LemmyError> {
226 build_db_pool_settings_opt(Some(settings)).await
229 pub async fn build_db_pool_for_tests() -> DbPool {
230 build_db_pool_settings_opt(None)
232 .expect("db pool missing")
235 pub fn get_database_url(settings: Option<&Settings>) -> String {
236 // The env var should override anything in the settings config
237 match get_database_url_from_env() {
239 Err(e) => match settings {
240 Some(settings) => settings.get_database_url(),
241 None => panic!("Failed to read database URL from env var LEMMY_DATABASE_URL: {e}"),
246 pub fn naive_now() -> NaiveDateTime {
247 chrono::prelude::Utc::now().naive_utc()
250 pub fn post_to_comment_sort_type(sort: SortType) -> CommentSortType {
252 SortType::Active | SortType::Hot => CommentSortType::Hot,
253 SortType::New | SortType::NewComments | SortType::MostComments => CommentSortType::New,
254 SortType::Old => CommentSortType::Old,
256 | SortType::TopSixHour
257 | SortType::TopTwelveHour
263 | SortType::TopThreeMonths
264 | SortType::TopSixMonths
265 | SortType::TopNineMonths => CommentSortType::Top,
269 static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
270 Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
271 .expect("compile email regex")
275 use diesel::sql_types::{BigInt, Text, Timestamp};
278 fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
281 sql_function!(fn lower(x: Text) -> Text);
284 pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";
286 impl ToSql<Text, Pg> for DbUrl {
287 fn to_sql(&self, out: &mut Output<Pg>) -> diesel::serialize::Result {
288 <std::string::String as ToSql<Text, Pg>>::to_sql(&self.0.to_string(), &mut out.reborrow())
292 impl<DB: Backend> FromSql<Text, DB> for DbUrl
294 String: FromSql<Text, DB>,
296 fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
297 let str = String::from_sql(value)?;
298 Ok(DbUrl(Box::new(Url::parse(&str)?)))
302 impl<Kind> From<ObjectId<Kind>> for DbUrl
304 Kind: Object + Send + 'static,
305 for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
307 fn from(id: ObjectId<Kind>) -> Self {
308 DbUrl(Box::new(id.into()))
314 use super::{fuzzy_search, *};
315 use crate::utils::is_email_regex;
318 fn test_fuzzy_search() {
319 let test = "This %is% _a_ fuzzy search";
322 "%This%\\%is\\%%\\_a\\_%fuzzy%search%".to_string()
328 assert!(is_email_regex("gush@gmail.com"));
329 assert!(!is_email_regex("nada_neutho"));
333 fn test_diesel_option_overwrite() {
334 assert_eq!(diesel_option_overwrite(&None), None);
335 assert_eq!(diesel_option_overwrite(&Some(String::new())), Some(None));
337 diesel_option_overwrite(&Some("test".to_string())),
338 Some(Some("test".to_string()))
343 fn test_diesel_option_overwrite_to_url() {
344 assert!(matches!(diesel_option_overwrite_to_url(&None), Ok(None)));
346 diesel_option_overwrite_to_url(&Some(String::new())),
350 diesel_option_overwrite_to_url(&Some("invalid_url".to_string())),
353 let example_url = "https://example.com";
355 diesel_option_overwrite_to_url(&Some(example_url.to_string())),
356 Ok(Some(Some(url))) if url == Url::parse(example_url).unwrap().into()