migration, tests, fix establish_connection

pull/4797/head
Dull Bananas 2024-06-25 00:46:14 +00:00
parent 923b24b18e
commit 6bc8e76303
8 changed files with 73 additions and 71 deletions

View File

@ -1,25 +0,0 @@
ALTER TABLE comment
ALTER COLUMN ap_id DROP DEFAULT;
ALTER TABLE community
ALTER COLUMN actor_id DROP DEFAULT,
ALTER COLUMN followers_url DROP DEFAULT,
ALTER COLUMN inbox_url DROP DEFAULT,
ALTER COLUMN shared_inbox_url DROP DEFAULT;
ALTER TABLE person
ALTER COLUMN actor_id DROP DEFAULT,
ALTER COLUMN inbox_url DROP DEFAULT;
ALTER TABLE post
ALTER COLUMN ap_id DROP DEFAULT;
ALTER TABLE private_message
ALTER COLUMN ap_id DROP DEFAULT;
ALTER TABLE site
ALTER COLUMN actor_id DROP DEFAULT,
ALTER COLUMN inbox_url DROP DEFAULT;
DROP FUNCTION generate_unique_changeme;

View File

@ -566,7 +566,7 @@ BEGIN
END IF;
-- Set local ap_id
IF NEW.local THEN
NEW.ap_id = coalesce(r.null_if_needs_replacement (NEW.ap_id), r.local_url('/comment/' || id));
NEW.ap_id = coalesce(NEW.ap_id, r.local_url('/comment/' || id));
END IF;
RETURN NEW;
END
@ -584,7 +584,7 @@ CREATE FUNCTION r.post_change_values ()
BEGIN
-- Set local ap_id
IF NEW.local THEN
NEW.ap_id = coalesce(r.null_if_needs_replacement (NEW.ap_id), r.local_url('/post/' || NEW.id::text));
NEW.ap_id = coalesce(NEW.ap_id, r.local_url('/post/' || NEW.id::text));
END IF;
RETURN NEW;
END
@ -602,7 +602,7 @@ CREATE FUNCTION r.private_message_change_values ()
BEGIN
-- Set local ap_id
IF NEW.local THEN
NEW.ap_id = coalesce(r.null_if_needs_replacement (NEW.ap_id), r.local_url('/private_message/' || NEW.id::text));
NEW.ap_id = coalesce(NEW.ap_id, r.local_url('/private_message/' || NEW.id::text));
END IF;
RETURN NEW;
END

View File

@ -223,6 +223,7 @@ mod tests {
use diesel_ltree::Ltree;
use pretty_assertions::assert_eq;
use serial_test::serial;
use url::Url;
#[tokio::test]
#[serial]
@ -273,7 +274,7 @@ mod tests {
path: Ltree(format!("0.{}", inserted_comment.id)),
published: inserted_comment.published,
updated: None,
ap_id: inserted_comment.ap_id.clone(),
ap_id: Url::parse(&format!("https://lemmy-alpha/comment/{}", inserted_comment.id)).unwrap().into(),
distinguished: false,
local: true,
language_id: LanguageId::default(),

View File

@ -389,6 +389,7 @@ mod tests {
};
use pretty_assertions::assert_eq;
use serial_test::serial;
use url::Url;
use std::collections::HashSet;
#[tokio::test]
@ -447,7 +448,7 @@ mod tests {
embed_description: None,
embed_video_url: None,
thumbnail_url: None,
ap_id: inserted_post.ap_id.clone(),
ap_id: Url::parse(&format!("https://lemmy-alpha/post/{}", inserted_post.id)).unwrap().into(),
local: true,
language_id: Default::default(),
featured_community: false,

View File

@ -100,6 +100,7 @@ mod tests {
};
use pretty_assertions::assert_eq;
use serial_test::serial;
use url::Url;
#[tokio::test]
#[serial]
@ -138,7 +139,7 @@ mod tests {
read: false,
updated: None,
published: inserted_private_message.published,
ap_id: inserted_private_message.ap_id.clone(),
ap_id: Url::parse(&format!("https://lemmy-alpha/private_message/{}", inserted_private_message.id)).unwrap().into(),
local: true,
};

View File

@ -24,13 +24,11 @@ use diesel::{
OptionalExtension,
};
use diesel_async::{
pg::AsyncPgConnection,
pooled_connection::{
pg::AsyncPgConnection, pooled_connection::{
deadpool::{Hook, HookError, Object as PooledConnection, Pool},
AsyncDieselConnectionManager,
ManagerConfig,
},
SimpleAsyncConnection,
}, AsyncConnection, RunQueryDsl
};
use futures_util::{future::BoxFuture, Future, FutureExt};
use i_love_jesus::CursorKey;
@ -332,32 +330,44 @@ pub fn diesel_url_create(opt: Option<&str>) -> LemmyResult<Option<DbUrl>> {
fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConnection>> {
let fut = async {
rustls::crypto::ring::default_provider()
.install_default()
.expect("Failed to install rustls crypto provider");
// We only support TLS with sslmode=require currently
let mut conn = if config.contains("sslmode=require") {
rustls::crypto::ring::default_provider()
.install_default()
.expect("Failed to install rustls crypto provider");
let rustls_config = DangerousClientConfigBuilder {
cfg: ClientConfig::builder(),
}
.with_custom_certificate_verifier(Arc::new(NoCertVerifier {}))
.with_no_client_auth();
let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
let (client, conn) = tokio_postgres::connect(config, tls)
.await
.map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
tokio::spawn(async move {
if let Err(e) = conn.await {
error!("Database connection failed: {e}");
let rustls_config = DangerousClientConfigBuilder {
cfg: ClientConfig::builder(),
}
});
let mut conn = AsyncPgConnection::try_from(client).await?;
// * Change geqo_threshold back to default value if it was changed, so it's higher than the
// collapse limits
// * Change collapse limits from 8 to 11 so the query planner can find a better table join order
// for more complicated queries
conn
.batch_execute("SET geqo_threshold=12;SET from_collapse_limit=11;SET join_collapse_limit=11;")
.with_custom_certificate_verifier(Arc::new(NoCertVerifier {}))
.with_no_client_auth();
let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
let (client, conn) = tokio_postgres::connect(config, tls)
.await
.map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
tokio::spawn(async move {
if let Err(e) = conn.await {
error!("Database connection failed: {e}");
}
});
AsyncPgConnection::try_from(client).await?
} else {
AsyncPgConnection::establish(config).await?
};
diesel::select((
// Change geqo_threshold back to default value if it was changed, so it's higher than the
// collapse limits
functions::set_config("geqo_threshold", "12", false),
// Change collapse limits from 8 to 11 so the query planner can find a better table join order
// for more complicated queries
functions::set_config("from_collapse_limit", "11", false),
functions::set_config("join_collapse_limit", "11", false),
// Set `lemmy.protocol_and_hostname` so triggers can use it
functions::set_config("lemmy.protocol_and_hostname", SETTINGS.get_protocol_and_hostname(), false),
))
.execute(&mut conn)
.await
.map_err(ConnectionError::CouldntSetupConfiguration)?;
Ok(conn)
@ -418,17 +428,11 @@ impl ServerCertVerifier for NoCertVerifier {
pub async fn build_db_pool() -> LemmyResult<ActualDbPool> {
let db_url = SETTINGS.get_database_url();
// We only support TLS with sslmode=require currently
let tls_enabled = db_url.contains("sslmode=require");
let manager = if tls_enabled {
// diesel-async does not support any TLS connections out of the box, so we need to manually
// provide a setup function which handles creating the connection
let mut config = ManagerConfig::default();
config.custom_setup = Box::new(establish_connection);
AsyncDieselConnectionManager::<AsyncPgConnection>::new_with_config(&db_url, config)
} else {
AsyncDieselConnectionManager::<AsyncPgConnection>::new(&db_url)
};
// diesel-async does not support any TLS connections out of the box, so we need to manually
// provide a setup function which handles creating the connection
let mut config = ManagerConfig::default();
config.custom_setup = Box::new(establish_connection);
let manager = AsyncDieselConnectionManager::<AsyncPgConnection>::new_with_config(&db_url, config);
let pool = Pool::builder(manager)
.max_size(SETTINGS.database.pool_size)
.runtime(Runtime::Tokio1)
@ -485,7 +489,7 @@ static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
});
pub mod functions {
use diesel::sql_types::{BigInt, Text, Timestamptz};
use diesel::sql_types::{BigInt, Bool, Text, Timestamptz};
sql_function! {
#[sql_name = "r.hot_rank"]
@ -508,6 +512,8 @@ pub mod functions {
// really this function is variadic, this just adds the two-argument version
sql_function!(fn coalesce<T: diesel::sql_types::SqlType + diesel::sql_types::SingleValue>(x: diesel::sql_types::Nullable<T>, y: T) -> T);
sql_function!(fn set_config(setting_name: Text, new_value: Text, is_local: Bool) -> Text);
}
pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";

View File

@ -0,0 +1,9 @@
ALTER TABLE comment
ALTER COLUMN ap_id SET DEFAULT generate_unique_changeme ();
ALTER TABLE post
ALTER COLUMN ap_id SET DEFAULT generate_unique_changeme ();
ALTER TABLE private_message
ALTER COLUMN ap_id SET DEFAULT generate_unique_changeme ();

View File

@ -0,0 +1,9 @@
ALTER TABLE comment
ALTER COLUMN ap_id DROP DEFAULT;
ALTER TABLE post
ALTER COLUMN ap_id DROP DEFAULT;
ALTER TABLE private_message
ALTER COLUMN ap_id DROP DEFAULT;