Skip to content

Commit 157047c

Browse files
committed
feat(test): run after-create SQL script during extension upgrade tests
Validates that an after-create SQL script can be executed after each extension create/update operation.
1 parent 6d83326 commit 157047c

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

nix/ext/tests/lib.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
installation, upgrades, and version verification of PostgreSQL extensions.
66
"""
77

8-
from typing import Sequence, Mapping
8+
from typing import Sequence, Mapping, Optional
99
from pathlib import Path
1010
from test_driver.machine import Machine
1111

@@ -41,7 +41,7 @@ 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:
44+
def run_sql_file(self, file: str | Path) -> str:
4545
return self.vm.succeed(
4646
f"""sudo -u postgres psql -v ON_ERROR_STOP=1 -f \"{file}\""""
4747
).strip()
@@ -88,7 +88,9 @@ def assert_version_matches(self, expected_version: str):
8888
installed_version == expected_version
8989
), f"Expected version {expected_version}, but found {installed_version}"
9090

91-
def check_upgrade_path(self, pg_version: str):
91+
def check_upgrade_path(
92+
self, pg_version: str, after_create_script: Optional[Path] = None
93+
):
9294
"""Test the complete upgrade path for a PostgreSQL version.
9395
9496
This method tests all available extension versions for a given PostgreSQL
@@ -113,13 +115,18 @@ def check_upgrade_path(self, pg_version: str):
113115
self.drop_extension()
114116
self.install_extension(first_version)
115117

118+
if after_create_script:
119+
self.run_sql_file(after_create_script)
120+
116121
# Test remaining versions
117122
for version in available_versions[1:]:
118123
if self.support_upgrade:
119124
self.update_extension(version)
120125
else:
121126
self.drop_extension()
122127
self.install_extension(version)
128+
if after_create_script:
129+
self.run_sql_file(after_create_script)
123130

124131
def check_install_last_version(self, pg_version: str) -> str:
125132
"""Test if the install of the last version of the extension works for a given PostgreSQL version.

nix/ext/tests/pgmq.nix

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ self.inputs.nixpkgs.lib.nixos.runTest {
114114
}
115115
sql_test_directory = Path("${../../tests}")
116116
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
117+
after_create_sql = Path("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
117118
118119
${builtins.readFile ./lib.py}
119120
@@ -125,16 +126,15 @@ self.inputs.nixpkgs.lib.nixos.runTest {
125126
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory, support_upgrade)
126127
127128
with subtest("Check upgrade path with postgresql 15"):
128-
test.check_upgrade_path("15")
129-
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
129+
test.check_upgrade_path("15", after_create_sql)
130130
131131
with subtest("Check pg_regress with postgresql 15 after extension upgrade"):
132132
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
133133
134134
last_version = None
135135
with subtest("Check the install of the last version of the extension"):
136136
last_version = test.check_install_last_version("15")
137-
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
137+
test.run_sql_file(after_create_sql)
138138
139139
if ext_has_background_worker:
140140
with subtest("Test switch_${pname}_version"):
@@ -150,18 +150,17 @@ self.inputs.nixpkgs.lib.nixos.runTest {
150150
151151
with subtest("Check last version of the extension after postgresql upgrade"):
152152
test.assert_version_matches(last_version)
153-
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
153+
test.run_sql_file(after_create_sql)
154154
155155
with subtest("Check upgrade path with postgresql 17"):
156-
test.check_upgrade_path("17")
157-
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
156+
test.check_upgrade_path("17", after_create_sql)
158157
159158
with subtest("Check pg_regress with postgresql 17 after extension upgrade"):
160159
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
161160
162161
with subtest("Check the install of the last version of the extension"):
163162
test.check_install_last_version("17")
164-
test.run_sql_file("${../../../ansible/files/postgresql_extension_custom_scripts/pgmq/after-create.sql}")
163+
test.run_sql_file(after_create_sql)
165164
166165
with subtest("Check pg_regress with postgresql 17 after installing the last version"):
167166
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)

0 commit comments

Comments
 (0)