Skip to content

Commit 21f9421

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 21f9421

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-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: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
};
93+
testScript =
94+
{ nodes, ... }:
95+
let
96+
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
97+
in
98+
''
99+
from pathlib import Path
100+
versions = {
101+
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
102+
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
103+
}
104+
extension_name = "${pname}"
105+
support_upgrade = True
106+
pg17_configuration = "${pg17-configuration}"
107+
ext_has_background_worker = ${
108+
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
109+
}
110+
sql_test_directory = Path("${../../tests}")
111+
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
112+
113+
${builtins.readFile ./lib.py}
114+
115+
start_all()
116+
117+
server.wait_for_unit("multi-user.target")
118+
server.wait_for_unit("postgresql.service")
119+
120+
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory, support_upgrade)
121+
122+
with subtest("Check upgrade path with postgresql 15"):
123+
test.check_upgrade_path("15")
124+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
125+
126+
with subtest("Check pg_regress with postgresql 15 after extension upgrade"):
127+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
128+
129+
last_version = None
130+
with subtest("Check the install of the last version of the extension"):
131+
last_version = test.check_install_last_version("15")
132+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
133+
134+
if ext_has_background_worker:
135+
with subtest("Test switch_${pname}_version"):
136+
test.check_switch_extension_with_background_worker(Path("${psql_15}/lib/${pname}.so"), "15")
137+
138+
with subtest("Check pg_regress with postgresql 15 after installing the last version"):
139+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
140+
141+
with subtest("switch to postgresql 17"):
142+
server.succeed(
143+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
144+
)
145+
146+
with subtest("Check last version of the extension after postgresql upgrade"):
147+
test.assert_version_matches(last_version)
148+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
149+
150+
with subtest("Check upgrade path with postgresql 17"):
151+
test.check_upgrade_path("17")
152+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
153+
154+
with subtest("Check pg_regress with postgresql 17 after extension upgrade"):
155+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
156+
157+
with subtest("Check the install of the last version of the extension"):
158+
test.check_install_last_version("17")
159+
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
160+
161+
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
162+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
163+
'';
164+
}

0 commit comments

Comments
 (0)