Skip to content

Commit 3c3c0ac

Browse files
jfrocheyvan-sraka
authored andcommitted
feat: test pg_upgrade compatibility with older extension versions
Add test to verify that all extension versions from PostgreSQL 15 can successfully upgrade to PostgreSQL 17 using pg_upgrade. The test now validates: - Each PG 15 extension version can be upgraded to PG 17 - Extension update scripts are properly generated during upgrade - Version handling works correctly with and without update scripts - Final extension versions match expected values after upgrade
1 parent 1081090 commit 3c3c0ac

File tree

7 files changed

+213
-206
lines changed

7 files changed

+213
-206
lines changed

nix/ext/tests/default.nix

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ let
137137
};
138138
testScript =
139139
{ nodes, ... }:
140-
let
141-
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
142-
in
143140
''
144141
from pathlib import Path
145142
versions = {
@@ -148,7 +145,9 @@ let
148145
}
149146
extension_name = "${pname}"
150147
support_upgrade = True
151-
pg17_configuration = "${pg17-configuration}"
148+
system = "${nodes.server.system.build.toplevel}"
149+
pg15_configuration = system
150+
pg17_configuration = f"{system}/specialisation/postgresql17"
152151
ext_has_background_worker = ${
153152
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
154153
}
@@ -204,6 +203,33 @@ let
204203
205204
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
206205
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
206+
207+
with subtest("Test pg_upgrade from postgresql 15 to 17 with older extension version"):
208+
# Test that all extension versions from postgresql 15 can be upgraded to postgresql 17 using pg_upgrade
209+
for version in versions["15"]:
210+
server.systemctl("stop postgresql.service")
211+
server.succeed("rm -fr /var/lib/postgresql/update_extensions.sql /var/lib/postgresql/17")
212+
server.succeed(
213+
f"{pg15_configuration}/bin/switch-to-configuration test >&2"
214+
)
215+
test.drop_extension()
216+
test.install_extension(version)
217+
server.succeed(
218+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
219+
)
220+
has_update_script = server.succeed(
221+
"test -f /var/lib/postgresql/update_extensions.sql && echo 'yes' || echo 'no'"
222+
).strip() == "yes"
223+
if has_update_script:
224+
# Run the extension update script generated during the upgrade
225+
test.run_sql_file("/var/lib/postgresql/update_extensions.sql")
226+
# If there was an update script, the last version should be installed
227+
test.assert_version_matches(versions["17"][-1])
228+
else:
229+
# Otherwise, the version should match the version from postgresql 15
230+
test.assert_version_matches(version)
231+
232+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
207233
'';
208234
};
209235
in
@@ -227,10 +253,11 @@ builtins.listToAttrs (
227253
"pg_hashids"
228254
"pg_jsonschema"
229255
"pg_net"
256+
"pg_partman"
230257
"pg_stat_monitor"
231258
"pg_tle"
232259
"pgaudit"
233-
"pg_partman"
260+
"postgis"
234261
"vector"
235262
"wal2json"
236263
"wrappers"

nix/ext/tests/lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def run_sql_file(self, file: str) -> str:
5252
).strip()
5353

5454
def drop_extension(self):
55-
self.run_sql(f"DROP EXTENSION IF EXISTS {self.extension_name};")
55+
self.run_sql(f"DROP EXTENSION IF EXISTS {self.extension_name} CASCADE;")
5656

5757
def install_extension(self, version: str):
5858
if self.schema != "public":

nix/ext/tests/pgmq.nix

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let
4040
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
4141
in
4242
self.inputs.nixpkgs.lib.nixos.runTest {
43-
name = "timescaledb";
43+
name = "pgmq";
4444
hostPkgs = pkgs;
4545
nodes.server =
4646
{ config, ... }:
@@ -106,9 +106,6 @@ self.inputs.nixpkgs.lib.nixos.runTest {
106106
};
107107
testScript =
108108
{ nodes, ... }:
109-
let
110-
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
111-
in
112109
''
113110
from pathlib import Path
114111
versions = {
@@ -117,7 +114,9 @@ self.inputs.nixpkgs.lib.nixos.runTest {
117114
}
118115
extension_name = "${pname}"
119116
support_upgrade = True
120-
pg17_configuration = "${pg17-configuration}"
117+
system = "${nodes.server.system.build.toplevel}"
118+
pg15_configuration = system
119+
pg17_configuration = f"{system}/specialisation/postgresql17"
121120
ext_has_background_worker = ${
122121
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
123122
}
@@ -174,5 +173,30 @@ self.inputs.nixpkgs.lib.nixos.runTest {
174173
175174
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
176175
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
176+
177+
with subtest("Test pg_upgrade from postgresql 15 to 17 with older extension version"):
178+
# Test that all extension versions from postgresql 15 can be upgraded to postgresql 17 using pg_upgrade
179+
for version in versions["15"]:
180+
server.systemctl("stop postgresql.service")
181+
server.succeed("rm -fr /var/lib/postgresql/update_extensions.sql /var/lib/postgresql/17")
182+
server.succeed(
183+
f"{pg15_configuration}/bin/switch-to-configuration test >&2"
184+
)
185+
test.drop_extension()
186+
test.install_extension(version)
187+
server.succeed(
188+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
189+
)
190+
has_update_script = server.succeed(
191+
"test -f /var/lib/postgresql/update_extensions.sql && echo 'yes' || echo 'no'"
192+
).strip() == "yes"
193+
if has_update_script:
194+
# Run the extension update script generated during the upgrade
195+
test.run_sql_file("/var/lib/postgresql/update_extensions.sql")
196+
# If there was an update script, the last version should be installed
197+
test.assert_version_matches(versions["17"][-1])
198+
else:
199+
# Otherwise, the version should match the version from postgresql 15
200+
test.assert_version_matches(version)
177201
'';
178202
}

nix/ext/tests/pgroonga.nix

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,16 @@ self.inputs.nixpkgs.lib.nixos.runTest {
126126
};
127127
testScript =
128128
{ nodes, ... }:
129-
let
130-
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
131-
in
132129
''
133130
from pathlib import Path
134131
versions = {
135132
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
136133
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
137134
}
138135
extension_name = "${pname}"
139-
pg17_configuration = "${pg17-configuration}"
136+
system = "${nodes.server.system.build.toplevel}"
137+
pg15_configuration = system
138+
pg17_configuration = f"{system}/specialisation/postgresql17"
140139
ext_has_background_worker = ${
141140
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
142141
}
@@ -188,5 +187,32 @@ self.inputs.nixpkgs.lib.nixos.runTest {
188187
189188
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
190189
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
190+
191+
with subtest("Test pg_upgrade from postgresql 15 to 17 with older extension version"):
192+
# Test that all extension versions from postgresql 15 can be upgraded to postgresql 17 using pg_upgrade
193+
for version in versions["15"]:
194+
server.systemctl("stop postgresql.service")
195+
server.succeed("rm -fr /var/lib/postgresql/update_extensions.sql /var/lib/postgresql/17")
196+
server.succeed(
197+
f"{pg15_configuration}/bin/switch-to-configuration test >&2"
198+
)
199+
test.drop_extension()
200+
test.install_extension(version)
201+
server.succeed(
202+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
203+
)
204+
has_update_script = server.succeed(
205+
"test -f /var/lib/postgresql/update_extensions.sql && echo 'yes' || echo 'no'"
206+
).strip() == "yes"
207+
if has_update_script:
208+
# Run the extension update script generated during the upgrade
209+
test.run_sql_file("/var/lib/postgresql/update_extensions.sql")
210+
# If there was an update script, the last version should be installed
211+
test.assert_version_matches(versions["17"][-1])
212+
else:
213+
# Otherwise, the version should match the version from postgresql 15
214+
test.assert_version_matches(version)
215+
216+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
191217
'';
192218
}

nix/ext/tests/pgsodium.nix

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ let
4242
echo 0000000000000000000000000000000000000000000000000000000000000000
4343
''
4444
);
45+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
46+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
4547
in
4648
self.inputs.nixpkgs.lib.nixos.runTest {
4749
name = pname;
@@ -61,7 +63,21 @@ self.inputs.nixpkgs.lib.nixos.runTest {
6163

6264
services.postgresql = {
6365
enable = true;
64-
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
66+
package = psql_15;
67+
authentication = ''
68+
local all postgres peer map=postgres
69+
local all all peer map=root
70+
'';
71+
identMap = ''
72+
root root supabase_admin
73+
postgres postgres postgres
74+
'';
75+
ensureUsers = [
76+
{
77+
name = "supabase_admin";
78+
ensureClauses.superuser = true;
79+
}
80+
];
6581
settings = {
6682
"shared_preload_libraries" = pname;
6783
"pgsodium.getkey_script" = pgsodiumGetKey;
@@ -70,7 +86,7 @@ self.inputs.nixpkgs.lib.nixos.runTest {
7086

7187
specialisation.postgresql17.configuration = {
7288
services.postgresql = {
73-
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17);
89+
package = lib.mkForce psql_17;
7490
};
7591

7692
systemd.services.postgresql-migrate = {
@@ -84,8 +100,8 @@ self.inputs.nixpkgs.lib.nixos.runTest {
84100
};
85101
script =
86102
let
87-
oldPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
88-
newPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
103+
oldPostgresql = psql_15;
104+
newPostgresql = psql_17;
89105
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
90106
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
91107
in
@@ -111,49 +127,93 @@ self.inputs.nixpkgs.lib.nixos.runTest {
111127
};
112128
testScript =
113129
{ nodes, ... }:
114-
let
115-
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
116-
in
117130
''
131+
from pathlib import Path
118132
versions = {
119133
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
120134
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
121135
}
136+
extension_name = "${pname}"
137+
system = "${nodes.server.system.build.toplevel}"
138+
pg15_configuration = system
139+
pg17_configuration = f"{system}/specialisation/postgresql17"
140+
ext_has_background_worker = ${
141+
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
142+
}
143+
sql_test_directory = Path("${../../tests}")
144+
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
122145
123-
def run_sql(query):
124-
return server.succeed(f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """).strip()
125-
126-
def check_upgrade_path(pg_version):
127-
with subtest("Check ${pname} upgrade path"):
128-
firstVersion = versions[pg_version][0]
129-
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION IF EXISTS ${pname};'")
130-
run_sql(f"""CREATE EXTENSION ${pname} WITH VERSION '{firstVersion}' CASCADE;""")
131-
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = '${pname}';""")
132-
assert installed_version == firstVersion, f"Expected ${pname} version {firstVersion}, but found {installed_version}"
133-
for version in versions[pg_version][1:]:
134-
run_sql(f"""ALTER EXTENSION ${pname} UPDATE TO '{version}';""")
135-
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = '${pname}';""")
136-
assert installed_version == version, f"Expected ${pname} version {version}, but found {installed_version}"
146+
${builtins.readFile ./lib.py}
137147
138148
start_all()
139149
140150
server.wait_for_unit("multi-user.target")
141151
server.wait_for_unit("postgresql.service")
142152
143-
check_upgrade_path("15")
153+
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory)
154+
155+
with subtest("Check upgrade path with postgresql 15"):
156+
test.check_upgrade_path("15")
144157
145-
with subtest("Check ${pname} latest extension version"):
146-
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION ${pname};'")
147-
server.succeed("sudo -u postgres psql -c 'CREATE EXTENSION ${pname} CASCADE;'")
148-
installed_extensions=run_sql(r"""SELECT extname, extversion FROM pg_extension;""")
149-
latestVersion = versions["15"][-1]
150-
assert f"${pname},{latestVersion}" in installed_extensions
158+
with subtest("Check pg_regress with postgresql 15 after extension upgrade"):
159+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
160+
161+
last_version = None
162+
with subtest("Check the install of the last version of the extension"):
163+
last_version = test.check_install_last_version("15")
164+
165+
if ext_has_background_worker:
166+
with subtest("Test switch_${pname}_version"):
167+
test.check_switch_extension_with_background_worker(Path("${psql_15}/lib/${pname}.so"), "15")
168+
169+
with subtest("Check pg_regress with postgresql 15 after installing the last version"):
170+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
151171
152172
with subtest("switch to postgresql 17"):
153173
server.succeed(
154-
"${pg17-configuration}/bin/switch-to-configuration test >&2"
174+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
155175
)
156176
157-
check_upgrade_path("17")
177+
with subtest("Check last version of the extension after postgresql upgrade"):
178+
test.assert_version_matches(last_version)
179+
180+
with subtest("Check upgrade path with postgresql 17"):
181+
test.check_upgrade_path("17")
182+
183+
with subtest("Check pg_regress with postgresql 17 after extension upgrade"):
184+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
185+
186+
with subtest("Check the install of the last version of the extension"):
187+
test.check_install_last_version("17")
188+
189+
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
190+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
191+
192+
with subtest("Test pg_upgrade from postgresql 15 to 17 with older extension version"):
193+
# Test that all extension versions from postgresql 15 can be upgraded to postgresql 17 using pg_upgrade
194+
for version in versions["15"]:
195+
server.systemctl("stop postgresql.service")
196+
server.succeed("rm -fr /var/lib/postgresql/update_extensions.sql /var/lib/postgresql/17")
197+
server.succeed(
198+
f"{pg15_configuration}/bin/switch-to-configuration test >&2"
199+
)
200+
test.drop_extension()
201+
test.install_extension(version)
202+
server.succeed(
203+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
204+
)
205+
has_update_script = server.succeed(
206+
"test -f /var/lib/postgresql/update_extensions.sql && echo 'yes' || echo 'no'"
207+
).strip() == "yes"
208+
if has_update_script:
209+
# Run the extension update script generated during the upgrade
210+
test.run_sql_file("/var/lib/postgresql/update_extensions.sql")
211+
# If there was an update script, the last version should be installed
212+
test.assert_version_matches(versions["17"][-1])
213+
else:
214+
# Otherwise, the version should match the version from postgresql 15
215+
test.assert_version_matches(version)
216+
217+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
158218
'';
159219
}

0 commit comments

Comments
 (0)