Skip to content

Commit 8a0ed1b

Browse files
committed
feat: test that the pgmq extension works well with the after create script
We create a new custom nixos test for the pgmq extension to test that the extension works well with the after-create.sql script present in `ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql`.
1 parent b8a12e1 commit 8a0ed1b

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

nix/ext/tests/default.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ builtins.listToAttrs (
205205
"pg_cron"
206206
"pg_graphql"
207207
"pg_net"
208-
"pgmq"
209208
"vector"
210209
"wrappers"
211210
]

nix/ext/tests/lib.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def run_sql(self, query: str) -> str:
4141
f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """
4242
).strip()
4343

44+
def run_sql_file(self, file: str) -> str:
45+
return self.vm.succeed(
46+
f"""sudo -u postgres psql -v ON_ERROR_STOP=1 -f \"{file}\""""
47+
).strip()
48+
4449
def drop_extension(self):
4550
self.run_sql(f"DROP EXTENSION IF EXISTS {self.extension_name};")
4651

nix/ext/tests/pgmq.nix

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
{ self, pkgs }:
2+
let
3+
pname = "pgmq";
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+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
39+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
40+
in
41+
self.inputs.nixpkgs.lib.nixos.runTest {
42+
name = "timescaledb";
43+
hostPkgs = pkgs;
44+
nodes.server =
45+
{ config, ... }:
46+
{
47+
services.postgresql = {
48+
enable = true;
49+
package = (postgresqlWithExtension psql_15);
50+
settings = (installedExtension "15").defaultSettings or { };
51+
};
52+
53+
specialisation.postgresql17.configuration = {
54+
services.postgresql = {
55+
package = lib.mkForce psql_17;
56+
};
57+
58+
systemd.services.postgresql-migrate = {
59+
serviceConfig = {
60+
Type = "oneshot";
61+
RemainAfterExit = true;
62+
User = "postgres";
63+
Group = "postgres";
64+
StateDirectory = "postgresql";
65+
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
66+
};
67+
script =
68+
let
69+
oldPostgresql = psql_15;
70+
newPostgresql = psql_17;
71+
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
72+
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
73+
in
74+
''
75+
if [[ ! -d ${newDataDir} ]]; then
76+
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
77+
${newPostgresql}/bin/initdb -D "${newDataDir}"
78+
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
79+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
80+
else
81+
echo "${newDataDir} already exists"
82+
fi
83+
'';
84+
};
85+
86+
systemd.services.postgresql = {
87+
after = [ "postgresql-migrate.service" ];
88+
requires = [ "postgresql-migrate.service" ];
89+
};
90+
};
91+
};
92+
testScript =
93+
{ nodes, ... }:
94+
let
95+
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
96+
in
97+
''
98+
from pathlib import Path
99+
versions = {
100+
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
101+
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
102+
}
103+
extension_name = "${pname}"
104+
support_upgrade = True
105+
pg17_configuration = "${pg17-configuration}"
106+
ext_has_background_worker = ${
107+
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
108+
}
109+
sql_test_directory = Path("${../../tests}")
110+
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
111+
112+
${builtins.readFile ./lib.py}
113+
114+
start_all()
115+
116+
server.wait_for_unit("multi-user.target")
117+
server.wait_for_unit("postgresql.service")
118+
119+
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory, support_upgrade)
120+
121+
with subtest("Check upgrade path with postgresql 15"):
122+
test.check_upgrade_path("15")
123+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
124+
125+
with subtest("Check pg_regress with postgresql 15 after extension upgrade"):
126+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
127+
128+
last_version = None
129+
with subtest("Check the install of the last version of the extension"):
130+
last_version = test.check_install_last_version("15")
131+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
132+
133+
if ext_has_background_worker:
134+
with subtest("Test switch_${pname}_version"):
135+
test.check_switch_extension_with_background_worker(Path("${psql_15}/lib/${pname}.so"), "15")
136+
137+
with subtest("Check pg_regress with postgresql 15 after installing the last version"):
138+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
139+
140+
with subtest("switch to postgresql 17"):
141+
server.succeed(
142+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
143+
)
144+
145+
with subtest("Check last version of the extension after postgresql upgrade"):
146+
test.assert_version_matches(last_version)
147+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
148+
149+
with subtest("Check upgrade path with postgresql 17"):
150+
test.check_upgrade_path("17")
151+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
152+
153+
with subtest("Check pg_regress with postgresql 17 after extension upgrade"):
154+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
155+
156+
with subtest("Check the install of the last version of the extension"):
157+
test.check_install_last_version("17")
158+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
159+
160+
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
161+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
162+
'';
163+
}

0 commit comments

Comments
 (0)