Skip to content

Commit

Permalink
Land #19159, improve error handling for postgres platform/arch detection
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 authored May 8, 2024
2 parents 82ce0a9 + 3d044c4 commit 1b9f242
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 12 additions & 8 deletions lib/postgres/postgres-pr/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ def map_compile_arch_to_architecture(compile_arch)
def detect_platform_and_arch
result = {}

query_result = query('select version()').rows.join.match(/on (?<architecture>\w+)-\w+-(?<platform>\w+)/)
server_vars = {
'version_compile_machine' => query_result[:architecture],
'version_compile_os' => query_result[:platform]
}

result[:arch] = map_compile_arch_to_architecture(server_vars['version_compile_machine'])
result[:platform] = map_compile_os_to_platform(server_vars['version_compile_os'])
query_result = query('select version()').rows[0][0]
match_platform_and_arch = query_result.match(/on (?<architecture>\w+)-\w+-(?<platform>\w+)/)

if match_platform_and_arch.nil?
arch = platform = query_result
else
arch = match_platform_and_arch[:architecture]
platform = match_platform_and_arch[:platform]
end

result[:arch] = map_compile_arch_to_architecture(arch)
result[:platform] = map_compile_os_to_platform(platform)

result
end
Expand Down
9 changes: 8 additions & 1 deletion spec/lib/rex/proto/postgresql/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@
[
{ version: 'PostgreSQL 9.4.26 on x86_64-pc-linux-gnu (Debian 9.4.26-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } },
{ version: 'PostgreSQL 14.11 (Debian 14.11-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } },
{ version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } }
{ version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } },
{
version: 'PostgreSQL 14.11 (Homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',
expected: {
arch: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',
platform: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>'
}
}
].each do |test|
context "when the database is version #{test[:version]}" do
it "returns #{test[:expected]}" do
Expand Down

0 comments on commit 1b9f242

Please sign in to comment.