Skip to content

Commit 4cdb762

Browse files
committed
feat: support multiple versions of the pg_tap extension
Build multiple versions of the pg_tap extension on different PostgreSQL versions. Add test for the extensions and their upgrade on PostgreSQL 15 and 17.
1 parent 52deb6f commit 4cdb762

File tree

6 files changed

+303
-35
lines changed

6 files changed

+303
-35
lines changed

nix/ext/pgtap.nix

Lines changed: 106 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,117 @@
66
perl,
77
perlPackages,
88
which,
9+
buildEnv,
10+
fetchpatch2,
911
}:
10-
11-
stdenv.mkDerivation rec {
12+
let
1213
pname = "pgtap";
13-
version = "1.2.0";
1414

15-
src = fetchFromGitHub {
16-
owner = "theory";
17-
repo = "pgtap";
18-
rev = "v${version}";
19-
hash = "sha256-lb0PRffwo6J5a6Hqw1ggvn0cW7gPZ02OEcLPi9ineI8=";
20-
};
15+
# Load version configuration from external file
16+
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname};
17+
18+
# Filter versions compatible with current PostgreSQL version
19+
supportedVersions = lib.filterAttrs (
20+
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
21+
) allVersions;
22+
23+
# Derived version information
24+
versions = lib.naturalSort (lib.attrNames supportedVersions);
25+
latestVersion = lib.last versions;
26+
numberOfVersions = builtins.length versions;
27+
packages = builtins.attrValues (
28+
lib.mapAttrs (name: value: build name value.hash) supportedVersions
29+
);
30+
repoOwner = "theory";
31+
repo = "${repoOwner}/${pname}";
32+
33+
# Build function for individual versions
34+
build =
35+
version: hash:
36+
stdenv.mkDerivation rec {
37+
inherit pname version;
38+
39+
src = fetchFromGitHub {
40+
owner = repoOwner;
41+
repo = pname;
42+
rev = "v${version}";
43+
inherit hash;
44+
};
45+
46+
nativeBuildInputs = [
47+
postgresql
48+
perl
49+
perlPackages.TAPParserSourceHandlerpgTAP
50+
which
51+
];
52+
53+
patches = lib.optionals (version == "1.3.3") [
54+
# Fix error in upgrade script from 1.2.0 to 1.3.3
55+
(fetchpatch2 {
56+
name = "pgtap-fix-upgrade-from-1.2.0-to-1.3.3.patch";
57+
url = "https://github.com/${repoOwner}/${pname}/pull/338.diff?full_index=1";
58+
hash = "sha256-AVRQyqCGoc0gcoMRWBJKMmUBjadGtWg7rvHmTq5rRpw=";
59+
})
60+
];
61+
62+
installPhase = ''
63+
runHook preInstall
64+
65+
mkdir -p $out/{lib,share/postgresql/extension}
66+
67+
# Create version-specific control file
68+
sed -e "/^default_version =/d" \
69+
-e "s|^module_pathname = .*|module_pathname = '$ext'|" \
70+
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
71+
72+
# Copy SQL file to install the specific version
73+
cp sql/${pname}--${version}.sql $out/share/postgresql/extension
74+
75+
if [[ -f src/pgtap.so ]]; then
76+
# Install the shared library with version suffix
77+
install -Dm755 src/pgtap.so $out/lib/${pname}-${version}${postgresql.dlSuffix}
78+
fi
79+
80+
# For the latest version, create default control file and symlink and copy SQL upgrade scripts
81+
if [[ "${version}" == "${latestVersion}" ]]; then
82+
{
83+
echo "default_version = '${version}'"
84+
cat $out/share/postgresql/extension/${pname}--${version}.control
85+
} > $out/share/postgresql/extension/${pname}.control
86+
cp sql/${pname}--*--*.sql $out/share/postgresql/extension
87+
elif [[ "${version}" == "1.3.1" ]]; then
88+
# 1.3.1 is the first and only version with a C extension
89+
ln -sfn ${pname}-${version}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
90+
fi
91+
'';
92+
93+
meta = with lib; {
94+
description = "A unit testing framework for PostgreSQL";
95+
longDescription = ''
96+
pgTAP is a unit testing framework for PostgreSQL written in PL/pgSQL and PL/SQL.
97+
It includes a comprehensive collection of TAP-emitting assertion functions,
98+
as well as the ability to integrate with other TAP-emitting test frameworks.
99+
It can also be used in the xUnit testing style.
100+
'';
101+
homepage = "https://pgtap.org";
102+
inherit (postgresql.meta) platforms;
103+
license = licenses.mit;
104+
};
105+
};
106+
in
107+
buildEnv {
108+
name = pname;
109+
paths = packages;
21110

22-
nativeBuildInputs = [
23-
postgresql
24-
perl
25-
perlPackages.TAPParserSourceHandlerpgTAP
26-
which
111+
pathsToLink = [
112+
"/lib"
113+
"/share/postgresql/extension"
27114
];
28115

29-
installPhase = ''
30-
install -D {sql/pgtap--${version}.sql,pgtap.control} -t $out/share/postgresql/extension
31-
'';
32-
33-
meta = with lib; {
34-
description = "A unit testing framework for PostgreSQL";
35-
longDescription = ''
36-
pgTAP is a unit testing framework for PostgreSQL written in PL/pgSQL and PL/SQL.
37-
It includes a comprehensive collection of TAP-emitting assertion functions,
38-
as well as the ability to integrate with other TAP-emitting test frameworks.
39-
It can also be used in the xUnit testing style.
40-
'';
41-
homepage = "https://pgtap.org";
42-
inherit (postgresql.meta) platforms;
43-
license = licenses.mit;
116+
passthru = {
117+
inherit versions numberOfVersions;
118+
pname = "${pname}-all";
119+
version =
120+
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
44121
};
45122
}

nix/ext/tests/pgtap.nix

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
{ self, pkgs }:
2+
let
3+
pname = "pgtap";
4+
inherit (pkgs) lib;
5+
installedExtension =
6+
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all";
7+
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions;
8+
postgresqlWithExtension =
9+
postgresql:
10+
let
11+
majorVersion = lib.versions.major postgresql.version;
12+
pkg = pkgs.buildEnv {
13+
name = "postgresql-${majorVersion}-${pname}";
14+
paths = [
15+
postgresql
16+
postgresql.lib
17+
(installedExtension majorVersion)
18+
];
19+
passthru = {
20+
inherit (postgresql) version psqlSchema;
21+
lib = pkg;
22+
withPackages = _: pkg;
23+
};
24+
nativeBuildInputs = [ pkgs.makeWrapper ];
25+
pathsToLink = [
26+
"/"
27+
"/bin"
28+
"/lib"
29+
];
30+
postBuild = ''
31+
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
32+
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib
33+
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib
34+
'';
35+
};
36+
in
37+
pkg;
38+
in
39+
self.inputs.nixpkgs.lib.nixos.runTest {
40+
name = pname;
41+
hostPkgs = pkgs;
42+
nodes.server =
43+
{ config, ... }:
44+
{
45+
virtualisation = {
46+
forwardPorts = [
47+
{
48+
from = "host";
49+
host.port = 13022;
50+
guest.port = 22;
51+
}
52+
];
53+
};
54+
services.openssh = {
55+
enable = true;
56+
};
57+
58+
services.postgresql = {
59+
enable = true;
60+
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
61+
};
62+
63+
specialisation.postgresql17.configuration = {
64+
services.postgresql = {
65+
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17);
66+
};
67+
68+
systemd.services.postgresql-migrate = {
69+
serviceConfig = {
70+
Type = "oneshot";
71+
RemainAfterExit = true;
72+
User = "postgres";
73+
Group = "postgres";
74+
StateDirectory = "postgresql";
75+
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
76+
};
77+
script =
78+
let
79+
oldPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
80+
newPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
81+
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
82+
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
83+
in
84+
''
85+
if [[ ! -d ${newDataDir} ]]; then
86+
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
87+
${newPostgresql}/bin/initdb -D "${newDataDir}"
88+
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
89+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
90+
else
91+
echo "${newDataDir} already exists"
92+
fi
93+
'';
94+
};
95+
96+
systemd.services.postgresql = {
97+
after = [ "postgresql-migrate.service" ];
98+
requires = [ "postgresql-migrate.service" ];
99+
};
100+
};
101+
};
102+
testScript =
103+
{ nodes, ... }:
104+
let
105+
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
106+
in
107+
''
108+
versions = {
109+
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
110+
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
111+
}
112+
113+
def run_sql(query):
114+
return server.succeed(f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """).strip()
115+
116+
def check_upgrade_path(pg_version):
117+
with subtest("Check ${pname} upgrade path"):
118+
firstVersion = versions[pg_version][0]
119+
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION IF EXISTS ${pname};'")
120+
run_sql(f"""CREATE EXTENSION ${pname} WITH VERSION '{firstVersion}' CASCADE;""")
121+
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = '${pname}';""")
122+
assert installed_version == firstVersion, f"Expected ${pname} version {firstVersion}, but found {installed_version}"
123+
for version in versions[pg_version][1:]:
124+
run_sql(f"""ALTER EXTENSION ${pname} UPDATE TO '{version}';""")
125+
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = '${pname}';""")
126+
assert installed_version == version, f"Expected ${pname} version {version}, but found {installed_version}"
127+
128+
start_all()
129+
130+
server.wait_for_unit("multi-user.target")
131+
server.wait_for_unit("postgresql.service")
132+
133+
check_upgrade_path("15")
134+
135+
with subtest("Check ${pname} latest extension version"):
136+
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION ${pname};'")
137+
server.succeed("sudo -u postgres psql -c 'CREATE EXTENSION ${pname} CASCADE;'")
138+
installed_extensions=run_sql(r"""SELECT extname, extversion FROM pg_extension;""")
139+
latestVersion = versions["15"][-1]
140+
assert f"${pname},{latestVersion}" in installed_extensions
141+
142+
with subtest("switch to postgresql 17"):
143+
server.succeed(
144+
"${pg17-configuration}/bin/switch-to-configuration test >&2"
145+
)
146+
147+
with subtest("Check ${pname} latest extension version after upgrade"):
148+
installed_extensions=run_sql(r"""SELECT extname, extversion FROM pg_extension;""")
149+
latestVersion = versions["17"][-1]
150+
assert f"${pname},{latestVersion}" in installed_extensions
151+
152+
check_upgrade_path("17")
153+
'';
154+
}

nix/ext/versions.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,5 +622,27 @@
622622
],
623623
"hash": "sha256-wfjiLkx+S3zVrAynisX1GdazueVJ3EOwQEPcgUQt7eA="
624624
}
625+
},
626+
"pgtap": {
627+
"1.2.0": {
628+
"postgresql": [
629+
"15"
630+
],
631+
"hash": "sha256-lb0PRffwo6J5a6Hqw1ggvn0cW7gPZ02OEcLPi9ineI8="
632+
},
633+
"1.3.1": {
634+
"postgresql": [
635+
"15",
636+
"17"
637+
],
638+
"hash": "sha256-HOgCb1CCfsfbMbMMWuzFJ4B8CfVm9b0sI2zBY3/kqyI="
639+
},
640+
"1.3.3": {
641+
"postgresql": [
642+
"15",
643+
"17"
644+
],
645+
"hash": "sha256-YgvfLGF7pLVcCKD66NnWAydDxtoYHH1DpLiYTEKHJ0E="
646+
}
625647
}
626648
}

0 commit comments

Comments
 (0)