From 9367cbdb009c9a2a1658ceeb74ceec20fccaf6bc Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 7 Feb 2024 05:21:02 -0500 Subject: [PATCH 1/3] Upgrading from postgres 15 -> 16-alpine. (#4426) - Includes an upgrade script. - Fixes #4406 --- .woodpecker.yml | 2 +- docker/docker-compose.yml | 2 +- docker/federation/docker-compose.yml | 2 +- scripts/postgres_15_to_16_upgrade.sh | 42 ++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100755 scripts/postgres_15_to_16_upgrade.sh diff --git a/.woodpecker.yml b/.woodpecker.yml index 409f962d1..a51a1499d 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -257,7 +257,7 @@ steps: services: database: - image: postgres:15.2-alpine + image: postgres:16-alpine environment: POSTGRES_USER: lemmy POSTGRES_PASSWORD: password diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 7b22c2420..30b38ad85 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -99,7 +99,7 @@ services: logging: *default-logging postgres: - image: postgres:15-alpine + image: postgres:16-alpine # this needs to match the database host in lemmy.hson # Tune your settings via # https://pgtune.leopard.in.ua/#/ diff --git a/docker/federation/docker-compose.yml b/docker/federation/docker-compose.yml index 814d43119..8dd70f3bc 100644 --- a/docker/federation/docker-compose.yml +++ b/docker/federation/docker-compose.yml @@ -20,7 +20,7 @@ x-lemmy-default: &lemmy-default restart: always x-postgres-default: &postgres-default - image: postgres:15-alpine + image: postgres:16-alpine environment: - POSTGRES_USER=lemmy - POSTGRES_PASSWORD=password diff --git a/scripts/postgres_15_to_16_upgrade.sh b/scripts/postgres_15_to_16_upgrade.sh new file mode 100755 index 000000000..20a6c594c --- /dev/null +++ b/scripts/postgres_15_to_16_upgrade.sh @@ -0,0 +1,42 @@ +#!/bin/sh +set -e + +echo "Do not stop in the middle of this upgrade, wait until you see the message: Upgrade complete." + +echo "Stopping lemmy and all services..." +sudo docker-compose stop + +echo "Make sure postgres is started..." +sudo docker-compose up -d postgres +echo "Waiting..." +sleep 20s + +echo "Exporting the Database to 15_16.dump.sql ..." +sudo docker-compose exec -T postgres pg_dumpall -c -U lemmy > 15_16_dump.sql +echo "Done." + +echo "Stopping postgres..." +sudo docker-compose stop postgres +echo "Waiting..." +sleep 20s + +echo "Removing the old postgres folder" +sudo rm -rf volumes/postgres + +echo "Updating docker-compose to use postgres version 16." +sed -i "s/image: postgres:.*/image: postgres:16-alpine/" ./docker-compose.yml + +echo "Starting up new postgres..." +sudo docker-compose up -d postgres +echo "Waiting..." +sleep 20s + +echo "Importing the database...." +cat 15_16_dump.sql | sudo docker-compose exec -T postgres psql -U lemmy +echo "Done." + +echo "Starting up lemmy..." +sudo docker-compose up -d + +echo "A copy of your old database is at 15_16.dump.sql . You can delete this file if the upgrade went smoothly." +echo "Upgrade complete." From f631f43024632845d384229408da4d19dfceec80 Mon Sep 17 00:00:00 2001 From: dullbananas Date: Wed, 7 Feb 2024 20:37:44 -0700 Subject: [PATCH 2/3] Run analyze in db_perf (#4420) --- crates/db_perf/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/db_perf/src/main.rs b/crates/db_perf/src/main.rs index 60bd30c82..2da9f9574 100644 --- a/crates/db_perf/src/main.rs +++ b/crates/db_perf/src/main.rs @@ -130,6 +130,10 @@ async fn try_main() -> LemmyResult<()> { // Make sure the println above shows the correct amount assert_eq!(num_inserted_posts, num_posts as usize); + // Manually trigger and wait for a statistics update to ensure consistent and high amount of accuracy in the statistics used for query planning + println!("🧮 updating database statistics"); + conn.batch_execute("ANALYZE;").await?; + // Enable auto_explain conn .batch_execute( From 3c5b1ac6dd60e153899adf5eda03c0e0e21557ed Mon Sep 17 00:00:00 2001 From: Elara Date: Thu, 8 Feb 2024 01:53:03 -0800 Subject: [PATCH 3/3] Use the Accept-Language header to set new users' language (#4435) * Use the Accept-Language header to set new users' language * Implement clippy suggestions --------- Co-authored-by: Dessalines --- Cargo.lock | 7 +++++++ crates/api_crud/Cargo.toml | 1 + crates/api_crud/src/user/create.rs | 9 +++++++++ 3 files changed, 17 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 0f7695df5..abe673cd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +[[package]] +name = "accept-language" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f27d075294830fcab6f66e320dab524bc6d048f4a151698e153205559113772" + [[package]] name = "activitypub_federation" version = "0.5.1-beta.1" @@ -2608,6 +2614,7 @@ dependencies = [ name = "lemmy_api_crud" version = "0.19.3" dependencies = [ + "accept-language", "activitypub_federation", "actix-web", "anyhow", diff --git a/crates/api_crud/Cargo.toml b/crates/api_crud/Cargo.toml index f4f89c2e5..d72ed27c9 100644 --- a/crates/api_crud/Cargo.toml +++ b/crates/api_crud/Cargo.toml @@ -29,6 +29,7 @@ moka.workspace = true once_cell.workspace = true anyhow.workspace = true webmention = "0.5.0" +accept-language = "3.1.0" [package.metadata.cargo-machete] ignored = ["futures"] diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 39e375150..5f11384f7 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -126,6 +126,14 @@ pub async fn register( // Also fixes a bug which allows users to log in when registrations are changed to closed. let accepted_application = Some(!require_registration_application); + // Get the user's preferred language using the Accept-Language header + let language_tag = req.headers().get("Accept-Language").and_then(|hdr| { + accept_language::parse(hdr.to_str().unwrap_or_default()) + .first() + // Remove the optional region code + .map(|lang_str| lang_str.split('-').next().unwrap_or_default().to_string()) + }); + // Create the local user let local_user_form = LocalUserInsertForm::builder() .person_id(inserted_person.id) @@ -134,6 +142,7 @@ pub async fn register( .show_nsfw(Some(data.show_nsfw)) .accepted_application(accepted_application) .default_listing_type(Some(local_site.default_post_listing_type)) + .interface_language(language_tag) // If its the initial site setup, they are an admin .admin(Some(!local_site.site_setup)) .build();