Skip to content

Commit fe56426

Browse files
moshloopclaude
andcommitted
fix: improve postgres version detection with multiple fallback methods
- Add dpkg-based version detection as primary method - Add postgres --version and initdb --version as fallback methods - Ensure proper fallback to hardcoded versions when all detection methods fail - Prevent empty version strings that cause invalid Docker tags 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ce06f3d commit fe56426

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

scripts/get-postgres-versions.sh

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,34 @@ get_version_from_image() {
1313

1414
echo "🔧 Detecting PostgreSQL $major_version version from image $image_tag..." >&2
1515

16-
# Run the built image to get the PostgreSQL version
16+
# Try multiple approaches to get the PostgreSQL version
17+
version=""
18+
19+
# Method 1: Try using dpkg to get the package version
20+
echo " Trying dpkg approach..." >&2
1721
version=$(docker run --rm --entrypoint="" "$image_tag" \
18-
/usr/lib/postgresql/$major_version/bin/postgres --version 2>/dev/null | \
19-
grep -oE 'PostgreSQL [0-9]+\.[0-9]+' | \
20-
awk '{print $2}' || echo "")
22+
sh -c "dpkg -l postgresql-$major_version 2>/dev/null | tail -1 | awk '{print \$3}' | grep -oE '^[0-9]+\.[0-9]+'" 2>/dev/null || echo "")
23+
24+
# Method 2: If that fails, try the postgres binary with --version
25+
if [ -z "$version" ]; then
26+
echo " Trying postgres --version approach..." >&2
27+
version=$(docker run --rm --entrypoint="" "$image_tag" \
28+
/usr/lib/postgresql/$major_version/bin/postgres --version 2>/dev/null | \
29+
grep -oE 'PostgreSQL [0-9]+\.[0-9]+' | \
30+
awk '{print $2}' || echo "")
31+
fi
32+
33+
# Method 3: If that fails, try initdb --version
34+
if [ -z "$version" ]; then
35+
echo " Trying initdb --version approach..." >&2
36+
version=$(docker run --rm --entrypoint="" "$image_tag" \
37+
/usr/lib/postgresql/$major_version/bin/initdb --version 2>/dev/null | \
38+
grep -oE '[0-9]+\.[0-9]+' | head -1 || echo "")
39+
fi
2140

2241
if [ -z "$version" ]; then
23-
echo "❌ Failed to detect version for PostgreSQL $major_version" >&2
24-
exit 1
42+
echo "❌ Failed to detect version for PostgreSQL $major_version using all methods" >&2
43+
return 1
2544
fi
2645

2746
echo "✅ Detected PostgreSQL $major_version version: $version" >&2
@@ -34,7 +53,31 @@ get_latest_version() {
3453

3554
# Try to get from built image first
3655
if docker image inspect "postgres-upgrade:$major_version" >/dev/null 2>&1; then
37-
get_version_from_image "$major_version"
56+
version=$(get_version_from_image "$major_version" 2>/dev/null)
57+
if [ -n "$version" ]; then
58+
echo "$version"
59+
else
60+
echo "⚠️ Failed to detect version from image, using hardcoded version for PostgreSQL $major_version" >&2
61+
# Fallback to hardcoded versions
62+
case $major_version in
63+
14)
64+
echo "14.19"
65+
;;
66+
15)
67+
echo "15.14"
68+
;;
69+
16)
70+
echo "16.10"
71+
;;
72+
17)
73+
echo "17.6"
74+
;;
75+
*)
76+
echo "Unknown version: $major_version" >&2
77+
exit 1
78+
;;
79+
esac
80+
fi
3881
else
3982
echo "⚠️ Image postgres-upgrade:$major_version not found, using hardcoded version" >&2
4083
# Fallback to hardcoded versions

0 commit comments

Comments
 (0)