Skip to content

Commit 33d6a4f

Browse files
committed
feat: switch both timescaledb and its loader when switching versions
Also reuse default tests defined in lib.py to test extension upgrade paths and switching extension versions.
1 parent c0e5f3f commit 33d6a4f

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

nix/ext/tests/lib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def check_switch_extension_with_background_worker(
147147
)
148148
last_version = available_versions[-1]
149149
assert ext_version.endswith(
150-
f"{self.extension_name}-{last_version}.so"
150+
f"{last_version}.so"
151151
), f"Expected {self.extension_name} version {last_version}, but found {ext_version}"
152152

153153
# Switch to the first version
@@ -157,13 +157,13 @@ def check_switch_extension_with_background_worker(
157157
# Check that we are using the first version now
158158
ext_version = self.vm.succeed(f"readlink -f {extension_lib_path}").strip()
159159
assert ext_version.endswith(
160-
f"{self.extension_name}-{first_version}.so"
160+
f"{first_version}.so"
161161
), f"Expected {self.extension_name} version {first_version}, but found {ext_version}"
162162

163163
# Switch to the first version
164164
self.vm.succeed(f"switch_{self.extension_name}_version {last_version}")
165165
# Check that we are using the last version now
166166
ext_version = self.vm.succeed(f"readlink -f {extension_lib_path}").strip()
167167
assert ext_version.endswith(
168-
f"{self.extension_name}-{last_version}.so"
168+
f"{last_version}.so"
169169
), f"Expected {self.extension_name} version {last_version}, but found {ext_version}"

nix/ext/tests/timescaledb.nix

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{ self, pkgs }:
22
let
3+
pname = "timescaledb";
34
inherit (pkgs) lib;
45
installedExtension =
5-
postgresMajorVersion:
6-
self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/timescaledb-all";
6+
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all";
77
versions = (installedExtension "15").versions;
8-
firstVersion = lib.head versions;
98
postgresqlWithExtension =
109
postgresql:
1110
let
1211
majorVersion = lib.versions.major postgresql.version;
1312
pkg = pkgs.buildEnv {
14-
name = "postgresql-${majorVersion}-timescaledb";
13+
name = "postgresql-${majorVersion}-${pname}";
1514
paths = [
1615
postgresql
1716
postgresql.lib
@@ -36,65 +35,44 @@ let
3635
};
3736
in
3837
pkg;
38+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
3939
in
4040
self.inputs.nixpkgs.lib.nixos.runTest {
4141
name = "timescaledb";
4242
hostPkgs = pkgs;
4343
nodes.server =
4444
{ ... }:
4545
{
46-
virtualisation = {
47-
forwardPorts = [
48-
{
49-
from = "host";
50-
host.port = 13022;
51-
guest.port = 22;
52-
}
53-
];
54-
};
55-
services.openssh = {
56-
enable = true;
57-
};
58-
users.users.root.openssh.authorizedKeys.keys = [
59-
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIArkmq6Th79Z4klW6Urgi4phN8yq769/l/10jlE00tU9"
60-
];
61-
6246
services.postgresql = {
6347
enable = true;
64-
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
48+
package = (postgresqlWithExtension psql_15);
6549
settings = {
6650
shared_preload_libraries = "timescaledb";
6751
};
6852
};
69-
70-
specialisation.postgresql15.configuration = {
71-
services.postgresql = {
72-
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15);
73-
};
74-
};
7553
};
7654
testScript =
7755
{ ... }:
7856
''
79-
def run_sql(query):
80-
return server.succeed(f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """).strip()
81-
82-
def check_upgrade_path():
83-
with subtest("Check timescaledb upgrade path"):
84-
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION IF EXISTS timescaledb;'")
85-
run_sql(r"""CREATE EXTENSION timescaledb WITH VERSION \"${firstVersion}\";""")
86-
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';""")
87-
assert installed_version == "${firstVersion}", f"Expected timescaledb version ${firstVersion}, but found {installed_version}"
88-
for version in [${lib.concatStringsSep ", " (map (s: ''"${s}"'') versions)}][1:]:
89-
run_sql(f"""ALTER EXTENSION timescaledb UPDATE TO '{version}';""")
90-
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';""")
91-
assert installed_version == version, f"Expected timescaledb version {version}, but found {installed_version}"
57+
${builtins.readFile ./lib.py}
9258
9359
start_all()
9460
9561
server.wait_for_unit("multi-user.target")
9662
server.wait_for_unit("postgresql.service")
9763
98-
check_upgrade_path()
64+
versions = {
65+
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') versions)}],
66+
}
67+
extension_name = "${pname}"
68+
support_upgrade = True
69+
70+
test = PostgresExtensionTest(server, extension_name, versions, support_upgrade)
71+
72+
with subtest("Check upgrade path with postgresql 15"):
73+
test.check_upgrade_path("15")
74+
75+
with subtest("Test switch_${pname}_version"):
76+
test.check_switch_extension_with_background_worker(Path("${psql_15}/lib/${pname}.so"), "15")
9977
'';
10078
}

nix/ext/timescaledb.nix

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
buildEnv,
1010
makeWrapper,
1111
switch-ext-version,
12+
coreutils,
13+
writeShellApplication,
1214
}:
1315

1416
let
@@ -65,7 +67,7 @@ let
6567
6668
# Rename the loader library to be version-specific
6769
if [ -f $out/lib/timescaledb${postgresql.dlSuffix} ]; then
68-
mv $out/lib/timescaledb${postgresql.dlSuffix} $out/lib/timescaledb-${version}-loader${postgresql.dlSuffix}
70+
mv $out/lib/timescaledb${postgresql.dlSuffix} $out/lib/timescaledb-loader-${version}${postgresql.dlSuffix}
6971
fi
7072
7173
# The versioned library (timescaledb-VERSION.so) is already correctly named
@@ -98,6 +100,16 @@ let
98100
packages = builtins.attrValues (
99101
lib.mapAttrs (name: value: build name value.hash (value.revision or name)) supportedVersions
100102
);
103+
switch-timescaledb-loader = writeShellApplication {
104+
name = "switch_timescaledb_loader";
105+
runtimeInputs = [ coreutils ];
106+
text = ''
107+
EXT_LOADER_TO_USE="$EXT_WRAPPER_LIB/$EXT_NAME-loader-$VERSION${postgresql.dlSuffix}"
108+
if [ -f "$EXT_LOADER_TO_USE" ]; then
109+
ln -sfnv "$EXT_LOADER_TO_USE" "$EXT_WRAPPER_LIB/$EXT_NAME-loader${postgresql.dlSuffix}"
110+
fi
111+
'';
112+
};
101113
in
102114
buildEnv {
103115
name = pname;
@@ -110,7 +122,7 @@ buildEnv {
110122
} > $out/share/postgresql/extension/${pname}.control
111123
112124
# Create symlink from the latest versioned loader to timescaledb.so
113-
ln -sfn ${pname}-${latestVersion}-loader${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
125+
ln -sfn ${pname}-loader-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
114126
115127
# The versioned extension libraries (timescaledb-VERSION.so) are already in place
116128
@@ -119,13 +131,14 @@ buildEnv {
119131
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" -gt 0
120132
)
121133
makeWrapper ${lib.getExe switch-ext-version} $out/bin/switch_timescaledb_version \
122-
--prefix EXT_WRAPPER : "$out" --prefix EXT_NAME : "${pname}"
123-
134+
--prefix EXT_WRAPPER : "$out" --prefix EXT_NAME : "${pname}" --prefix EXTRA_STEPS : ${lib.getExe switch-timescaledb-loader}
124135
'';
136+
125137
pathsToLink = [
126138
"/lib"
127139
"/share/postgresql/extension"
128140
];
141+
129142
passthru = {
130143
inherit versions numberOfVersions switch-ext-version;
131144
pname = "${pname}-all";

nix/packages/switch-ext-version.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ writeShellApplication {
5454
# Update library symlink
5555
ln -sfnv "$EXT_LIB_TO_USE" "$EXT_WRAPPER_LIB/$EXT_NAME${postgresql.dlSuffix}"
5656
57+
# Handle extension specific steps
58+
if [ -x "''${EXTRA_STEPS:-}" ]; then
59+
#shellcheck disable=SC1090
60+
source "''${EXTRA_STEPS}"
61+
fi
62+
5763
# Update control file
5864
EXT_WRAPPER_SHARE="$EXT_WRAPPER/share/postgresql/extension"
5965
echo "default_version = '$VERSION'" > "$EXT_WRAPPER_SHARE/$EXT_NAME.control"

0 commit comments

Comments
 (0)