Skip to content

Commit 886a86b

Browse files
committed
fix: bring in some function from previous version to test
1 parent 7c0e496 commit 886a86b

File tree

1 file changed

+123
-22
lines changed

1 file changed

+123
-22
lines changed

nix/ext/pg_cron.nix

Lines changed: 123 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
pkgs,
32
lib,
43
stdenv,
54
fetchFromGitHub,
@@ -31,28 +30,38 @@ let
3130

3231
patches = map (p: ./. + "/${p}") (versionData.patches or [ ]);
3332

33+
buildPhase = ''
34+
make PG_CONFIG=${postgresql}/bin/pg_config
35+
36+
# Create version-specific SQL file
37+
cp pg_cron.sql pg_cron--${version}.sql
38+
39+
# Create versioned control file with modified module path
40+
sed -e "/^default_version =/d" \
41+
-e "s|^module_pathname = .*|module_pathname = '\$libdir/pg_cron'|" \
42+
pg_cron.control > pg_cron--${version}.control
43+
'';
44+
3445
installPhase = ''
3546
mkdir -p $out/{lib,share/postgresql/extension}
3647
37-
# Install shared library with version suffix
38-
mv ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
48+
# Install versioned library
49+
install -Dm755 ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
3950
40-
# Create version-specific control file
41-
sed -e "/^default_version =/d" \
42-
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}-${version}'|" \
43-
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
51+
# Install version-specific files
52+
install -Dm644 ${pname}--${version}.sql $out/share/postgresql/extension/
53+
install -Dm644 ${pname}--${version}.control $out/share/postgresql/extension/
4454
45-
# Create version-specific SQL file
46-
cp ${pname}.sql $out/share/postgresql/extension/${pname}--${version}.sql
55+
# Install upgrade scripts
56+
find . -name 'pg_cron--*--*.sql' -exec install -Dm644 {} $out/share/postgresql/extension/ \;
4757
48-
# For the latest version, create default control file and symlink and copy SQL upgrade scripts
58+
# For the latest version, create default control file and symlink
4959
if [[ "${version}" == "${latestVersion}" ]]; then
5060
{
5161
echo "default_version = '${version}'"
5262
cat $out/share/postgresql/extension/${pname}--${version}.control
5363
} > $out/share/postgresql/extension/${pname}.control
54-
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
55-
cp *.sql $out/share/postgresql/extension
64+
ln -sfn ${pname}-${version}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
5665
fi
5766
'';
5867

@@ -66,18 +75,110 @@ let
6675
};
6776
packages = builtins.attrValues (lib.mapAttrs (name: value: build name value) supportedVersions);
6877
in
69-
pkgs.buildEnv {
70-
name = pname;
71-
paths = packages;
72-
pathsToLink = [
73-
"/lib"
74-
"/share/postgresql/extension"
75-
];
78+
stdenv.mkDerivation {
79+
pname = "${pname}-all";
80+
version =
81+
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
82+
83+
buildInputs = packages;
84+
85+
dontUnpack = true;
86+
dontConfigure = true;
87+
dontBuild = true;
88+
89+
installPhase = ''
90+
mkdir -p $out/{lib,share/postgresql/extension,bin}
91+
92+
# Install all versions
93+
for drv in ${lib.concatStringsSep " " packages}; do
94+
ln -sv $drv/lib/* $out/lib/
95+
cp -v --no-clobber $drv/share/postgresql/extension/* $out/share/postgresql/extension/ || true
96+
done
97+
98+
# Find latest version
99+
latest_control=$(ls -v $out/share/postgresql/extension/${pname}--*.control | tail -n1)
100+
latest_version=$(basename "$latest_control" | sed -E 's/${pname}--([0-9.]+).control/\1/')
101+
102+
# Create main control file only if it doesn't exist
103+
if [ ! -f "$out/share/postgresql/extension/${pname}.control" ]; then
104+
# Create main control file with default_version
105+
echo "default_version = '$latest_version'" > $out/share/postgresql/extension/${pname}.control
106+
cat "$latest_control" >> $out/share/postgresql/extension/${pname}.control
107+
fi
108+
109+
# Library symlink - only if it doesn't exist
110+
if [ ! -f "$out/lib/${pname}${postgresql.dlSuffix}" ]; then
111+
ln -sfnv ${pname}-$latest_version${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
112+
fi
113+
114+
# Create version switcher script
115+
cat > $out/bin/switch_pg_cron_version <<'EOF'
116+
#!/bin/sh
117+
set -e
118+
119+
if [ $# -ne 1 ]; then
120+
echo "Usage: $0 <version>"
121+
echo "Example: $0 1.4.2"
122+
exit 1
123+
fi
124+
125+
VERSION=$1
126+
NIX_PROFILE="/var/lib/postgresql/.nix-profile"
127+
128+
# Follow the complete chain of symlinks to find the multi-version directory
129+
CURRENT_LINK="$NIX_PROFILE/lib/pg_cron-$VERSION${postgresql.dlSuffix}"
130+
echo "Starting with link: $CURRENT_LINK"
131+
132+
# Follow first two symlinks to get to the multi-version directory
133+
for i in 1 2; do
134+
if [ -L "$CURRENT_LINK" ]; then
135+
NEXT_LINK=$(readlink "$CURRENT_LINK")
136+
echo "Following link: $NEXT_LINK"
137+
if echo "$NEXT_LINK" | grep -q '^/'; then
138+
CURRENT_LINK="$NEXT_LINK"
139+
else
140+
CURRENT_LINK="$(dirname "$CURRENT_LINK")/$NEXT_LINK"
141+
fi
142+
echo "Current link is now: $CURRENT_LINK"
143+
fi
144+
done
145+
146+
# The multi-version directory should be the parent of the current link
147+
MULTI_VERSION_DIR=$(dirname "$CURRENT_LINK")
148+
echo "Found multi-version directory: $MULTI_VERSION_DIR"
149+
LIB_DIR="$MULTI_VERSION_DIR"
150+
EXTENSION_DIR="$NIX_PROFILE/share/postgresql/extension"
151+
152+
echo "Looking for file: $LIB_DIR/pg_cron-$VERSION${postgresql.dlSuffix}"
153+
ls -la "$LIB_DIR" || true
154+
155+
# Check if version exists
156+
if [ ! -f "$LIB_DIR/pg_cron-$VERSION${postgresql.dlSuffix}" ]; then
157+
echo "Error: Version $VERSION not found"
158+
exit 1
159+
fi
160+
161+
# Update library symlink
162+
ln -sfnv "pg_cron-$VERSION${postgresql.dlSuffix}" "$LIB_DIR/pg_cron${postgresql.dlSuffix}"
163+
164+
# Update control file
165+
echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_cron.control"
166+
cat "$EXTENSION_DIR/pg_cron--$VERSION.control" >> "$EXTENSION_DIR/pg_cron.control"
167+
168+
echo "Successfully switched pg_cron to version $VERSION"
169+
EOF
170+
171+
chmod +x $out/bin/switch_pg_cron_version
172+
'';
76173

77174
passthru = {
78175
inherit versions numberOfVersions;
79-
pname = "${pname}-all";
80-
version =
81-
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
176+
};
177+
178+
meta = with lib; {
179+
description = "Run Cron jobs through PostgreSQL (multi-version compatible)";
180+
homepage = "https://github.com/citusdata/pg_cron";
181+
platforms = postgresql.meta.platforms;
182+
license = licenses.postgresql;
82183
};
83184
}

0 commit comments

Comments
 (0)