Skip to content

Commit c9bbdf2

Browse files
jfrochesamrose
authored andcommitted
feat: multiple versions for the pgaudit extension
Build multiple versions of the pgaudit extension on different PostgreSQL versions. Add test for the extensions and their upgrade on PostgreSQL 15 and 17.
1 parent f8c523c commit c9bbdf2

File tree

3 files changed

+157
-43
lines changed

3 files changed

+157
-43
lines changed

nix/ext/pgaudit.nix

Lines changed: 115 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,135 @@
11
{
22
lib,
33
stdenv,
4+
buildEnv,
45
fetchFromGitHub,
56
libkrb5,
67
openssl,
78
postgresql,
89
}:
910
#adapted from https://github.com/NixOS/nixpkgs/blob/master/pkgs/servers/sql/postgresql/ext/pgaudit.nix
1011
let
11-
source =
12-
{
13-
"17" = {
14-
version = "17.0";
15-
hash = "sha256-3ksq09wiudQPuBQI3dhEQi8IkXKLVIsPFgBnwLiicro=";
16-
};
17-
"16" = {
18-
version = "16.0";
19-
hash = "sha256-8+tGOl1U5y9Zgu+9O5UDDE4bec4B0JC/BQ6GLhHzQzc=";
20-
};
21-
"15" = {
22-
version = "1.7.0";
23-
hash = "sha256-8pShPr4HJaJQPjW1iPJIpj3CutTx8Tgr+rOqoXtgCcw=";
24-
};
25-
}
26-
.${lib.versions.major postgresql.version}
27-
or (throw "Source for pgaudit is not available for ${postgresql.version}");
28-
in
29-
stdenv.mkDerivation {
3012
pname = "pgaudit";
31-
inherit (source) version;
13+
# Load version configuration from external file
14+
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname};
3215

33-
src = fetchFromGitHub {
34-
owner = "pgaudit";
35-
repo = "pgaudit";
36-
rev = source.version;
37-
hash = source.hash;
38-
};
16+
# Filter versions compatible with current PostgreSQL version
17+
supportedVersions = lib.filterAttrs (
18+
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
19+
) allVersions;
20+
21+
# Derived version information
22+
versions = lib.naturalSort (lib.attrNames supportedVersions);
23+
latestVersion = lib.last versions;
24+
numberOfVersions = builtins.length versions;
25+
packages = builtins.attrValues (
26+
lib.mapAttrs (name: value: build name value.hash) supportedVersions
27+
);
28+
29+
# Build function for individual pgaudit versions
30+
build =
31+
version: hash:
32+
stdenv.mkDerivation {
33+
inherit pname version;
34+
35+
src = fetchFromGitHub {
36+
owner = "pgaudit";
37+
repo = "pgaudit";
38+
rev = version;
39+
inherit hash;
40+
};
41+
42+
buildInputs = [
43+
libkrb5
44+
openssl
45+
postgresql
46+
];
47+
48+
makeFlags = [ "USE_PGXS=1" ];
3949

40-
buildInputs = [
41-
libkrb5
42-
openssl
43-
postgresql
50+
postBuild =
51+
lib.optionalString (version == "1.7.0") ''
52+
mv ${pname}--1.7.sql ${pname}--1.7.0.sql
53+
cp ${pname}--1.7.0.sql ${pname}--1.6.1--1.7.0.sql
54+
''
55+
+ lib.optionalString (version == "1.7.1") ''
56+
mv ${pname}--1.7--1.7.1.sql ${pname}--1.7.0--1.7.1.sql
57+
'';
58+
59+
installPhase = ''
60+
runHook preInstall
61+
62+
mkdir -p $out/{lib,share/postgresql/extension}
63+
64+
# Install shared library with version suffix
65+
mv ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
66+
67+
# Install SQL files
68+
sed -i '1s/^/DROP EVENT TRIGGER IF EXISTS pgaudit_ddl_command_end; \n/' *.sql
69+
sed -i '1s/^/DROP EVENT TRIGGER IF EXISTS pgaudit_sql_drop; \n/' *.sql
70+
sed -i 's/CREATE FUNCTION/CREATE OR REPLACE FUNCTION/' *.sql
71+
cp *.sql $out/share/postgresql/extension
72+
73+
# Create version-specific control file
74+
sed -e "/^default_version =/d" \
75+
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}'|" \
76+
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
77+
78+
# For the latest version, create default control file and symlink
79+
if [[ "${version}" == "${latestVersion}" ]]; then
80+
{
81+
echo "default_version = '${latestVersion}'"
82+
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control
83+
} > $out/share/postgresql/extension/${pname}.control
84+
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
85+
fi
86+
87+
runHook postInstall
88+
'';
89+
90+
meta = with lib; {
91+
description = "Open Source PostgreSQL Audit Logging";
92+
homepage = "https://github.com/pgaudit/pgaudit";
93+
changelog = "https://github.com/pgaudit/pgaudit/releases/tag/${source.version}";
94+
license = licenses.postgresql;
95+
inherit (postgresql.meta) platforms;
96+
};
97+
};
98+
in
99+
buildEnv {
100+
name = pname;
101+
paths = packages;
102+
pathsToLink = [
103+
"/lib"
104+
"/share/postgresql/extension"
44105
];
106+
postBuild = ''
107+
# checks
108+
(set -x
109+
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${
110+
toString (numberOfVersions + 1)
111+
}"
112+
)
45113
46-
makeFlags = [ "USE_PGXS=1" ];
114+
# Verify all expected library files are present
115+
expectedFiles=${toString (numberOfVersions + 1)}
116+
actualFiles=$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)
47117
48-
installPhase = ''
49-
install -D -t $out/lib pgaudit${postgresql.dlSuffix}
50-
install -D -t $out/share/postgresql/extension *.sql
51-
install -D -t $out/share/postgresql/extension *.control
118+
if [[ "$actualFiles" != "$expectedFiles" ]]; then
119+
echo "Error: Expected $expectedFiles library files, found $actualFiles"
120+
echo "Files found:"
121+
ls -la $out/lib/${pname}*${postgresql.dlSuffix} || true
122+
exit 1
123+
fi
52124
'';
53125

54-
meta = with lib; {
55-
description = "Open Source PostgreSQL Audit Logging";
56-
homepage = "https://github.com/pgaudit/pgaudit";
57-
changelog = "https://github.com/pgaudit/pgaudit/releases/tag/${source.version}";
58-
platforms = postgresql.meta.platforms;
59-
license = licenses.postgresql;
126+
passthru = {
127+
inherit versions numberOfVersions;
128+
pname = "${pname}-all";
129+
version =
130+
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
131+
defaultSettings = {
132+
"shared_preload_libraries" = pname;
133+
};
60134
};
61135
}

nix/ext/tests/default.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ let
8383
specialisation.postgresql17.configuration = {
8484
services.postgresql = {
8585
package = lib.mkForce psql_17;
86+
settings = (installedExtension "17").defaultSettings or { };
8687
};
8788

8889
systemd.services.postgresql-migrate = {
@@ -106,7 +107,13 @@ let
106107
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
107108
${newPostgresql}/bin/initdb -D "${newDataDir}"
108109
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
109-
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
110+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" \
111+
${
112+
if config.services.postgresql.settings.shared_preload_libraries != null then
113+
" --old-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}' --new-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}'"
114+
else
115+
""
116+
}
110117
else
111118
echo "${newDataDir} already exists"
112119
fi
@@ -206,6 +213,7 @@ builtins.listToAttrs (
206213
"pg_graphql"
207214
"pg_jsonschema"
208215
"pg_net"
216+
"pgaudit"
209217
"vector"
210218
"wrappers"
211219
]

nix/ext/versions.json

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,39 @@
321321
"hash": "sha256-Cpi2iASi1QJoED0Qs1dANqg/BNZTsz5S+pw8iYyW03Y="
322322
}
323323
},
324-
"pgmq": {
324+
"pgaudit": {
325+
"1.6.1": {
326+
"postgresql": [
327+
"15"
328+
],
329+
"hash": "sha256-vxUDVq7nWkq7Qugy7HJLOXk4B61MSBIYQkzcbU6wSG8="
330+
},
331+
"1.7.0": {
332+
"postgresql": [
333+
"15"
334+
],
335+
"hash": "sha256-8pShPr4HJaJQPjW1iPJIpj3CutTx8Tgr+rOqoXtgCcw="
336+
},
337+
"1.7.1": {
338+
"postgresql": [
339+
"15"
340+
],
341+
"hash": "sha256-emwoTowT7WKFX0RQDqJXjIblrzqaUIUkzqSqBCHVKQ8="
342+
},
343+
"17.0": {
344+
"postgresql": [
345+
"17"
346+
],
347+
"hash": "sha256-3ksq09wiudQPuBQI3dhEQi8IkXKLVIsPFgBnwLiicro="
348+
},
349+
"17.1": {
350+
"postgresql": [
351+
"17"
352+
],
353+
"hash": "sha256-9St/ESPiFq2NiPKqbwHLwkIyATKUkOGxFcUrWgT+Iqo="
354+
}
355+
},
356+
"pgmq": {
325357
"1.4.4": {
326358
"postgresql": [
327359
"15"

0 commit comments

Comments
 (0)