|
1 | 1 | {
|
2 |
| - pkgs, |
3 | 2 | lib,
|
4 | 3 | stdenv,
|
5 | 4 | fetchFromGitHub,
|
|
31 | 30 |
|
32 | 31 | patches = map (p: ./. + "/${p}") (versionData.patches or [ ]);
|
33 | 32 |
|
| 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 | + |
34 | 45 | installPhase = ''
|
35 | 46 | mkdir -p $out/{lib,share/postgresql/extension}
|
36 | 47 |
|
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} |
39 | 50 |
|
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/ |
44 | 54 |
|
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/ \; |
47 | 57 |
|
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 |
49 | 59 | if [[ "${version}" == "${latestVersion}" ]]; then
|
50 | 60 | {
|
51 | 61 | echo "default_version = '${version}'"
|
52 | 62 | cat $out/share/postgresql/extension/${pname}--${version}.control
|
53 | 63 | } > $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} |
56 | 65 | fi
|
57 | 66 | '';
|
58 | 67 |
|
|
66 | 75 | };
|
67 | 76 | packages = builtins.attrValues (lib.mapAttrs (name: value: build name value) supportedVersions);
|
68 | 77 | 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 | + ''; |
76 | 173 |
|
77 | 174 | passthru = {
|
78 | 175 | 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; |
82 | 183 | };
|
83 | 184 | }
|
0 commit comments