|
1 |
| -{ lib, stdenv, fetchFromGitHub, postgresql }: |
2 |
| - |
| 1 | +{ |
| 2 | + lib, |
| 3 | + stdenv, |
| 4 | + fetchFromGitHub, |
| 5 | + postgresql, |
| 6 | + buildEnv, |
| 7 | +}: |
3 | 8 | let
|
4 |
| - # NOTE (aseipp): the 1.x series of pg_stat_monitor has some non-standard and |
5 |
| - # weird build logic (Percona projects in general seem to have their own |
6 |
| - # strange build harness) where it will try to pick the right .sql file to |
7 |
| - # install into the extension dir based on the postgresql major version. for |
8 |
| - # our purposes, we only need to support v13 and v14+, so just replicate this |
9 |
| - # logic from the makefile and pick the right file here. |
10 |
| - # |
11 |
| - # this seems to all be cleaned up in version 2.0 of the extension, so ideally |
12 |
| - # we could upgrade to it later on and nuke this. |
13 |
| - # DEPRECATED sqlFilename = if lib.versionOlder postgresql.version "14" |
14 |
| - # then "pg_stat_monitor--1.0.13.sql.in" |
15 |
| - # else "pg_stat_monitor--1.0.14.sql.in"; |
16 |
| - |
17 |
| -in |
18 |
| -stdenv.mkDerivation rec { |
19 | 9 | pname = "pg_stat_monitor";
|
20 |
| - version = "2.1.0"; |
21 | 10 |
|
22 |
| - buildInputs = [ postgresql ]; |
| 11 | + # Load version configuration from external file |
| 12 | + allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname}; |
23 | 13 |
|
24 |
| - src = fetchFromGitHub { |
25 |
| - owner = "percona"; |
26 |
| - repo = pname; |
27 |
| - rev = "refs/tags/${version}"; |
28 |
| - hash = "sha256-STJVvvrLVLe1JevNu6u6EftzAWv+X+J8lu66su7Or2s="; |
29 |
| - }; |
| 14 | + # Filter versions compatible with current PostgreSQL version |
| 15 | + supportedVersions = lib.filterAttrs ( |
| 16 | + _: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql |
| 17 | + ) allVersions; |
| 18 | + |
| 19 | + # Derived version information |
| 20 | + versions = lib.naturalSort (lib.attrNames supportedVersions); |
| 21 | + latestVersion = lib.last versions; |
| 22 | + numberOfVersions = builtins.length versions; |
| 23 | + packages = builtins.attrValues ( |
| 24 | + lib.mapAttrs (name: value: build name value.hash value.revision) supportedVersions |
| 25 | + ); |
| 26 | + |
| 27 | + # Build function for individual versions |
| 28 | + build = |
| 29 | + version: hash: revision: |
| 30 | + stdenv.mkDerivation rec { |
| 31 | + inherit pname version; |
| 32 | + |
| 33 | + buildInputs = [ postgresql ]; |
| 34 | + |
| 35 | + src = fetchFromGitHub { |
| 36 | + owner = "percona"; |
| 37 | + repo = pname; |
| 38 | + rev = "refs/tags/${revision}"; |
| 39 | + inherit hash; |
| 40 | + }; |
| 41 | + |
| 42 | + makeFlags = [ "USE_PGXS=1" ]; |
| 43 | + |
| 44 | + installPhase = '' |
| 45 | + mkdir -p $out/{lib,share/postgresql/extension} |
| 46 | +
|
| 47 | + # Install shared library with version suffix |
| 48 | + mv ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix} |
| 49 | +
|
| 50 | + # Create version-specific control file |
| 51 | + sed -e "/^default_version =/d" \ |
| 52 | + -e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}-${version}'|" \ |
| 53 | + ${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control |
| 54 | +
|
| 55 | + # For the latest version, create default control file and symlink and copy SQL upgrade scripts |
| 56 | + if [[ "${version}" == "${latestVersion}" ]]; then |
| 57 | + { |
| 58 | + echo "default_version = '${version}'" |
| 59 | + cat $out/share/postgresql/extension/${pname}--${version}.control |
| 60 | + } > $out/share/postgresql/extension/${pname}.control |
| 61 | + ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix} |
| 62 | + cp *.sql $out/share/postgresql/extension |
| 63 | + else |
| 64 | + mv ./pg_stat_monitor--${version}.sql.in $out/share/postgresql/extension/pg_stat_monitor--${version}.sql |
| 65 | + fi |
| 66 | + ''; |
| 67 | + |
| 68 | + meta = with lib; { |
| 69 | + description = "Query Performance Monitoring Tool for PostgreSQL"; |
| 70 | + homepage = "https://github.com/percona/${pname}"; |
| 71 | + license = licenses.postgresql; |
| 72 | + broken = lib.versionOlder postgresql.version "15"; |
| 73 | + inherit (postgresql.meta) platforms; |
| 74 | + }; |
| 75 | + }; |
| 76 | +in |
| 77 | +buildEnv { |
| 78 | + name = pname; |
| 79 | + paths = packages; |
| 80 | + |
| 81 | + pathsToLink = [ |
| 82 | + "/lib" |
| 83 | + "/share/postgresql/extension" |
| 84 | + ]; |
30 | 85 |
|
31 |
| - makeFlags = [ "USE_PGXS=1" ]; |
| 86 | + postBuild = '' |
| 87 | + # Verify all expected library files are present |
| 88 | + expectedFiles=${toString (numberOfVersions + 1)} |
| 89 | + actualFiles=$(ls -l $out/lib/${pname}*${postgresql.dlSuffix} | wc -l) |
32 | 90 |
|
33 |
| - installPhase = '' |
34 |
| - mkdir -p $out/{lib,share/postgresql/extension} |
35 |
| - |
36 |
| - cp *${postgresql.dlSuffix} $out/lib |
37 |
| - cp *.sql $out/share/postgresql/extension |
38 |
| - cp *.control $out/share/postgresql/extension |
| 91 | + if [[ "$actualFiles" != "$expectedFiles" ]]; then |
| 92 | + echo "Error: Expected $expectedFiles library files, found $actualFiles" |
| 93 | + echo "Files found:" |
| 94 | + ls -la $out/lib/*${postgresql.dlSuffix} || true |
| 95 | + exit 1 |
| 96 | + fi |
39 | 97 | '';
|
40 | 98 |
|
41 |
| - meta = with lib; { |
42 |
| - description = "Query Performance Monitoring Tool for PostgreSQL"; |
43 |
| - homepage = "https://github.com/percona/${pname}"; |
44 |
| - platforms = postgresql.meta.platforms; |
45 |
| - license = licenses.postgresql; |
46 |
| - broken = lib.versionOlder postgresql.version "15"; |
| 99 | + passthru = { |
| 100 | + inherit versions numberOfVersions; |
| 101 | + pname = "${pname}-all"; |
| 102 | + version = |
| 103 | + "multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions); |
47 | 104 | };
|
48 | 105 | }
|
0 commit comments