Skip to content

Commit

Permalink
Refs #37797 - use CSV output from psql
Browse files Browse the repository at this point in the history
(cherry picked from commit c35c1e3)
  • Loading branch information
evgeni authored and ekohl committed Oct 7, 2024
1 parent ead2caa commit fa49e6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 5 additions & 2 deletions hooks/boot/06-postgresql-upgrade-extensions.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
18 changes: 17 additions & 1 deletion spec/postgresql_upgrade_hook_context_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fa49e6d

Please sign in to comment.