Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fact for installed postgres version #1609

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/facter/postgres_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Facter.add('postgres_version') do
confine { Facter::Core::Execution.which('postgres') }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least on Debian family, postgres is not in PATH, so this wouldn't work. Maybe it'd be better to get the version from psql, which is in the PATH?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with that is that psqlmight be a different version, and I would like to ensure that it's actually the database server version that gets reported. I guess this needs a bit more thought.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated below, perhaps pg_config --version could be used.

setcode do
version = Facter::Core::Execution.execute('postgres -V 2>/dev/null')
version.match(%r{\d+\.\d+$})[0] if version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex won't match the output of Debian family packages:

kenyon@lithium ~ % /usr/lib/postgresql/14/bin/postgres -V
postgres (PostgreSQL) 14.13 (Debian 14.13-1.pgdg110+1)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Guess the regex will have to be adjusted. This is RHEL 9 by the way, and I believe also vanilla PostgreSQL:

% /bin/postgres -V
postgres (PostgreSQL) 13.14
%

end
end
20 changes: 20 additions & 0 deletions spec/unit/facter/postgres_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe Facter::Util::Fact.to_s do
before(:each) do
Facter.clear
end

describe 'postgres_version' do
context 'with value' do
before :each do
allow(Facter::Core::Execution).to receive(:which).and_return('/usr/bin/postgres')
allow(Facter::Core::Execution).to receive(:execute).with('postgres -V 2>/dev/null').and_return('postgres (PostgreSQL) 10.0')
end

it 'postgres_version' do
expect(Facter.fact(:postgres_version).value).to eq('10.0')
end
end
end
end
Loading