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