Skip to content

Commit 066acd2

Browse files
committed
feat: multiple versions for the pg_hashids extension
Build multiple versions of the pg_hashids extension on different PostgreSQL versions. Add test for the extensions and their upgrade on PostgreSQL 15 and 17.
1 parent a3e960f commit 066acd2

File tree

5 files changed

+251
-20
lines changed

5 files changed

+251
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ __pycache__/
1717
result*
1818
.env-local
1919
.history
20+
.nixos-test-history
2021

2122

2223
#IDE

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@
13811381
psql_15 = makeCheckHarness basePackages.psql_15.bin;
13821382
psql_17 = makeCheckHarness basePackages.psql_17.bin;
13831383
psql_orioledb-17 = makeCheckHarness basePackages.psql_orioledb-17.bin;
1384+
pg_hashids = import ./nix/tests/pg_hashids.nix { inherit self; inherit pkgs; };
13841385
};
13851386

13861387
# Apps is a list of names of things that can be executed with 'nix run';

nix/ext/pg_hashids.nix

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,92 @@
1-
{ lib, stdenv, fetchFromGitHub, postgresql }:
1+
{ pkgs, lib, stdenv, fetchFromGitHub, postgresql }:
22

3-
stdenv.mkDerivation rec {
3+
let
44
pname = "pg_hashids";
5-
version = "cd0e1b31d52b394a0df64079406a14a4f7387cd6";
5+
build = version: hash: revision:
6+
stdenv.mkDerivation rec {
7+
inherit pname version;
68

7-
buildInputs = [ postgresql ];
9+
buildInputs = [ postgresql ];
810

9-
src = fetchFromGitHub {
10-
owner = "iCyberon";
11-
repo = pname;
12-
rev = "${version}";
13-
hash = "sha256-Nmb7XLqQflYZfqj0yrewfb1Hl5YgEB5wfjBunPwIuOU=";
14-
};
11+
src = fetchFromGitHub {
12+
owner = "iCyberon";
13+
repo = pname;
14+
rev = version;
15+
inherit hash;
16+
};
17+
18+
installPhase = ''
19+
mkdir -p $out/{lib,share/postgresql/extension}
20+
21+
mv ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
22+
23+
create_sql_files() {
24+
if test -f ${pname}--${version}.sql; then
25+
cp ${pname}--${version}.sql $out/share/postgresql/extension
26+
fi
27+
echo "Creating SQL files for previous versions..."
28+
if [[ "${version}" == "${latestVersion}" ]]; then
29+
cp *.sql $out/share/postgresql/extension
30+
31+
# anything after 1.2.1 is unreleased
32+
cp pg_hashids--1.3.sql $out/share/postgresql/extension/pg_hashids--${version}.sql
33+
cp pg_hashids--1.2.1--1.3.sql $out/share/postgresql/extension/pg_hashids--1.2.1--${version}.sql
34+
fi
35+
}
36+
37+
create_control_files() {
38+
sed -e "/^default_version =/d" \
39+
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}'|" \
40+
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
41+
42+
if [[ "${version}" == "${latestVersion}" ]]; then
43+
{
44+
echo "default_version = '${latestVersion}'"
45+
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control
46+
} > $out/share/postgresql/extension/${pname}.control
47+
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
48+
fi
49+
}
1550
16-
installPhase = ''
17-
mkdir -p $out/{lib,share/postgresql/extension}
51+
create_sql_files
52+
create_control_files
53+
'';
1854

19-
cp *${postgresql.dlSuffix} $out/lib
20-
cp *.sql $out/share/postgresql/extension
21-
cp *.control $out/share/postgresql/extension
55+
meta = with lib; {
56+
description = "Generate short unique IDs in PostgreSQL";
57+
homepage = "https://github.com/iCyberon/pg_hashids";
58+
license = licenses.postgresql;
59+
inherit (postgresql.meta) platforms;
60+
};
61+
};
62+
allVersions =
63+
(builtins.fromJSON (builtins.readFile ./versions.json)).pg_hashids;
64+
supportedVersions = lib.filterAttrs (_: value:
65+
builtins.elem (lib.versions.major postgresql.version) value.postgresql)
66+
allVersions;
67+
versions = lib.naturalSort (lib.attrNames supportedVersions);
68+
latestVersion = lib.last versions;
69+
numberOfVersions = builtins.length versions;
70+
packages = builtins.attrValues
71+
(lib.mapAttrs (name: value: build name value.hash (value.revision or name))
72+
supportedVersions);
73+
in pkgs.buildEnv {
74+
name = pname;
75+
paths = packages;
76+
pathsToLink = [ "/lib" "/share/postgresql/extension" ];
77+
postBuild = ''
78+
# checks
79+
(set -x
80+
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${
81+
toString (numberOfVersions + 1)
82+
}"
83+
)
2284
'';
2385

24-
meta = with lib; {
25-
description = "Generate short unique IDs in PostgreSQL";
26-
homepage = "https://github.com/iCyberon/pg_hashids";
27-
platforms = postgresql.meta.platforms;
28-
license = licenses.postgresql;
86+
passthru = {
87+
inherit versions numberOfVersions;
88+
pname = "${pname}-all";
89+
version = "multi-" + lib.concatStringsSep "-"
90+
(map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
2991
};
3092
}

nix/ext/versions.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"pg_hashids": {
3+
"1.2.1": {
4+
"postgresql": [
5+
"15",
6+
"17"
7+
],
8+
"revision": "v1.2.1",
9+
"hash": "sha256-2yQ0JrhwZF9dGEIydOviDpQ+3ZhJ8T16dQt6KIirAJ0="
10+
},
11+
"1.3.0-cd0e1b31d52b394a0df64079406a14a4f7387cd6": {
12+
"postgresql": [
13+
"15",
14+
"17"
15+
],
16+
"revision": "cd0e1b31d52b394a0df64079406a14a4f7387cd6",
17+
"hash": "sha256-Nmb7XLqQflYZfqj0yrewfb1Hl5YgEB5wfjBunPwIuOU="
18+
}
19+
}
20+
}

nix/tests/pg_hashids.nix

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

0 commit comments

Comments
 (0)