diff --git a/hooks/boot/06-postgresql-upgrade-extensions.rb b/hooks/boot/06-postgresql-upgrade-extensions.rb index fe76c8b1..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 @@ -12,6 +14,11 @@ def os_needs_postgresql_upgrade? def postgresql_upgrade(new_version) logger.notice("Performing upgrade of PostgreSQL to #{new_version}") + start_services(['postgresql']) + 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 logger.notice("Upgrading PostgreSQL packages") @@ -35,7 +42,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..1facfeb0 100644 --- a/spec/postgresql_upgrade_hook_context_extension_spec.rb +++ b/spec/postgresql_upgrade_hook_context_extension_spec.rb @@ -36,6 +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 --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 @@ -57,7 +74,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