From 5d1b9e267177e73ae857863b83438d0225d1cab7 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Mon, 9 Sep 2024 14:14:49 +0200 Subject: [PATCH 1/2] Fixes #37797 - ensure PostgreSQL upgrade uses the right locales --- hooks/boot/06-postgresql-upgrade-extensions.rb | 6 +++++- spec/postgresql_upgrade_hook_context_extension_spec.rb | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/hooks/boot/06-postgresql-upgrade-extensions.rb b/hooks/boot/06-postgresql-upgrade-extensions.rb index fe76c8b1..db7298b8 100644 --- a/hooks/boot/06-postgresql-upgrade-extensions.rb +++ b/hooks/boot/06-postgresql-upgrade-extensions.rb @@ -12,6 +12,10 @@ def os_needs_postgresql_upgrade? def postgresql_upgrade(new_version) logger.notice("Performing upgrade of PostgreSQL to #{new_version}") + start_services(['postgresql']) + postgres_list_databases = `runuser -l postgres -c 'psql --list --tuples-only | grep -E "^\s+postgres"'` + (_name, _owner, _enconding, collate, ctype, _privileges) = postgres_list_databases.chomp.split('|').map(&:strip) + stop_services logger.notice("Upgrading PostgreSQL packages") @@ -35,7 +39,7 @@ def postgresql_upgrade(new_version) # see https://bugzilla.redhat.com/show_bug.cgi?id=1935301 execute!("sed -i '/^data_directory/d' /var/lib/pgsql/data/postgresql.conf", false, true) - execute_as!('postgres', 'postgresql-setup --upgrade', false, true) + execute_as!('postgres', "PGSETUP_INITDB_OPTIONS=\"--lc-collate=#{collate} --lc-ctype=#{ctype} --locale=#{collate}\" postgresql-setup --upgrade", false, true) logger.notice("Analyzing the new PostgreSQL cluster") diff --git a/spec/postgresql_upgrade_hook_context_extension_spec.rb b/spec/postgresql_upgrade_hook_context_extension_spec.rb index 3d8089df..aa915799 100644 --- a/spec/postgresql_upgrade_hook_context_extension_spec.rb +++ b/spec/postgresql_upgrade_hook_context_extension_spec.rb @@ -36,6 +36,7 @@ allow(context).to receive(:ensure_packages) allow(context).to receive(:stop_services) allow(context).to receive(:start_services) + allow(context).to receive(:'`').with("runuser -l postgres -c 'psql --list --tuples-only | grep -E \"^\s+postgres\"'").and_return(' postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | ') allow(logger).to receive(:notice) end @@ -57,7 +58,7 @@ it 'runs postgresql-setup --upgrade' do expect(subject).to be_nil - expect(context).to have_received(:'execute!').with("runuser -l postgres -c 'postgresql-setup --upgrade'", false, true) + expect(context).to have_received(:'execute!').with("runuser -l postgres -c 'PGSETUP_INITDB_OPTIONS=\"--lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 --locale=en_US.UTF-8\" postgresql-setup --upgrade'", false, true) end it 'runs vacuumdb --all --analyze-in-stages' do From 67ca477bb716264f0db914d94a229e39830426b5 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Tue, 10 Sep 2024 08:55:19 +0200 Subject: [PATCH 2/2] Refs #37797 - use CSV output from psql --- hooks/boot/06-postgresql-upgrade-extensions.rb | 7 +++++-- ...esql_upgrade_hook_context_extension_spec.rb | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/hooks/boot/06-postgresql-upgrade-extensions.rb b/hooks/boot/06-postgresql-upgrade-extensions.rb index db7298b8..bbc3d5d8 100644 --- a/hooks/boot/06-postgresql-upgrade-extensions.rb +++ b/hooks/boot/06-postgresql-upgrade-extensions.rb @@ -1,3 +1,5 @@ +require 'csv' + module PostgresqlUpgradeHookContextExtension def needs_postgresql_upgrade?(new_version) File.read('/var/lib/pgsql/data/PG_VERSION').chomp.to_i < new_version.to_i @@ -13,8 +15,9 @@ def postgresql_upgrade(new_version) logger.notice("Performing upgrade of PostgreSQL to #{new_version}") start_services(['postgresql']) - postgres_list_databases = `runuser -l postgres -c 'psql --list --tuples-only | grep -E "^\s+postgres"'` - (_name, _owner, _enconding, collate, ctype, _privileges) = postgres_list_databases.chomp.split('|').map(&:strip) + postgres_list = `runuser -l postgres -c 'psql --list --tuples-only --csv'` + postgres_databases = CSV.parse(postgres_list) + (_name, _owner, _enconding, collate, ctype, _privileges) = postgres_databases.find { |line| line.first == 'postgres' } stop_services diff --git a/spec/postgresql_upgrade_hook_context_extension_spec.rb b/spec/postgresql_upgrade_hook_context_extension_spec.rb index aa915799..1facfeb0 100644 --- a/spec/postgresql_upgrade_hook_context_extension_spec.rb +++ b/spec/postgresql_upgrade_hook_context_extension_spec.rb @@ -36,7 +36,23 @@ allow(context).to receive(:ensure_packages) allow(context).to receive(:stop_services) allow(context).to receive(:start_services) - allow(context).to receive(:'`').with("runuser -l postgres -c 'psql --list --tuples-only | grep -E \"^\s+postgres\"'").and_return(' postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | ') + allow(context).to receive(:'`').with("runuser -l postgres -c 'psql --list --tuples-only --csv'") + .and_return(<<~PSQL + candlepin,postgres,UTF8,en_US.utf8,en_US.utf8,"=T/postgres + postgres=CTc/postgres + candlepin=CTc/postgres" + foreman,foreman,UTF8,en_US.utf8,en_US.utf8,"=T/foreman + foreman=CTc/foreman" + postgres,postgres,UTF8,en_US.UTF-8,en_US.UTF-8, + pulpcore,postgres,UTF8,en_US.utf8,en_US.utf8,"=T/postgres + postgres=CTc/postgres + pulp=CTc/postgres" + template0,postgres,UTF8,en_US.UTF-8,en_US.UTF-8,"=c/postgres + postgres=CTc/postgres" + template1,postgres,UTF8,en_US.UTF-8,en_US.UTF-8,"=c/postgres + postgres=CTc/postgres" + PSQL + ) allow(logger).to receive(:notice) end