From 3780ad31c8b7c0e55c97921d00f2ed3b7bf5b194 Mon Sep 17 00:00:00 2001 From: Jose Garcia <47431411+ruxwez@users.noreply.github.com> Date: Thu, 23 Oct 2025 22:38:37 +0200 Subject: [PATCH] Add detection for latest Postgres and limit CI --- .github/workflows/docker-build.yml | 2 +- .github/workflows/pr-test.yml | 4 ++-- Dockerfile | 2 +- src/common.rs | 17 +++++++++++++++++ src/main.rs | 22 ++++++++++++++++++++-- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index c12cda4..bc02708 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - postgres_version: ["18", "17", "17.6", "16", "16.10"] + postgres_version: ["latest", "18", "17", "17.6", "16", "16.10"] fail-fast: false steps: diff --git a/.github/workflows/pr-test.yml b/.github/workflows/pr-test.yml index d0fced7..ad91e29 100644 --- a/.github/workflows/pr-test.yml +++ b/.github/workflows/pr-test.yml @@ -14,8 +14,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - postgres_version: ["18", "17", "17.6", "16", "16.10"] - platform: ["linux/amd64", "linux/arm64"] + postgres_version: ["latest", "18", "17", "17.6", "16", "16.10"] + platform: ["linux/amd64"] fail-fast: false steps: diff --git a/Dockerfile b/Dockerfile index 0b8bde6..1ec55fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG POSTGRES_VERSION=17.5 +ARG POSTGRES_VERSION=latest # Builder stage: compile the Rust binary FROM rust:slim AS builder diff --git a/src/common.rs b/src/common.rs index 638ce5c..02dd5b4 100644 --- a/src/common.rs +++ b/src/common.rs @@ -11,3 +11,20 @@ pub fn run(cmd: &str) { panic!("Command failed: {}", cmd); } } + +/// Run a shell command and return its stdout as a trimmed String. +/// Panics if the command fails to execute or returns a non-zero exit code. +pub fn run_output(cmd: &str) -> String { + let output = Command::new("sh") + .arg("-c") + .arg(cmd) + .output() + .expect("Failed to execute command"); + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + panic!("Command failed: {}\nstderr: {}", cmd, stderr); + } + + String::from_utf8_lossy(&output.stdout).trim().to_string() +} diff --git a/src/main.rs b/src/main.rs index cf03fff..af84b20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::env; -use crate::common::run; +use crate::common::{run, run_output}; mod common; mod extensions; @@ -14,7 +14,25 @@ async fn main() { panic!("Insert PostgreSQL version as argument"); } - let pg_version = &args[1]; + // If user passed "latest", detect the numeric version from the installed postgres binary. + // Otherwise use the provided version string (e.g. "15.3" or "15"). + let mut pg_version = args[1].clone(); + if pg_version.eq_ignore_ascii_case("latest") { + println!("🔎 Detecting PostgreSQL version from the base image (requested: latest)..."); + // Try `postgres --version`. This requires that the base image already provides the postgres binary. + let ver_output = run_output("postgres --version"); + // Typical output: "postgres (PostgreSQL) 15.3" + // We take the last whitespace-separated token as the numeric version. + let numeric_version = ver_output + .split_whitespace() + .last() + .expect("Failed to parse postgres --version output") + .to_string(); + + println!("â„šī¸ Detected PostgreSQL version: {}", numeric_version); + pg_version = numeric_version; + } + let pg_major = pg_version.split(".").next().unwrap(); println!(