Skip to content

Commit 8588260

Browse files
committed
feat: multiple versions for the timescaledb-apache extension
1 parent 9b278d5 commit 8588260

File tree

4 files changed

+177
-94
lines changed

4 files changed

+177
-94
lines changed

nix/ext/timescaledb-2.9.1.nix

Lines changed: 0 additions & 65 deletions
This file was deleted.

nix/ext/timescaledb.nix

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
libkrb5,
99
}:
1010

11-
stdenv.mkDerivation rec {
12-
pname = "timescaledb-apache";
13-
version = "2.16.1";
11+
let
12+
pname = "timescaledb";
13+
build = version: hash: revision:
14+
stdenv.mkDerivation rec {
15+
inherit pname version;
1416

1517
nativeBuildInputs = [ cmake ];
1618
buildInputs = [
@@ -19,12 +21,12 @@ stdenv.mkDerivation rec {
1921
libkrb5
2022
];
2123

22-
src = fetchFromGitHub {
23-
owner = "timescale";
24-
repo = "timescaledb";
25-
rev = version;
26-
hash = "sha256-sLxWdBmih9mgiO51zLLxn9uwJVYc5JVHJjSWoADoJ+w=";
27-
};
24+
src = fetchFromGitHub {
25+
owner = "timescale";
26+
repo = "timescaledb";
27+
rev = version;
28+
inherit hash;
29+
};
2830

2931
cmakeFlags = [
3032
"-DSEND_TELEMETRY_DEFAULT=OFF"
@@ -33,26 +35,75 @@ stdenv.mkDerivation rec {
3335
"-DAPACHE_ONLY=1"
3436
] ++ lib.optionals stdenv.isDarwin [ "-DLINTER=OFF" ];
3537

36-
# Fix the install phase which tries to install into the pgsql extension dir,
37-
# and cannot be manually overridden. This is rather fragile but works OK.
38-
postPatch = ''
39-
for x in CMakeLists.txt sql/CMakeLists.txt; do
40-
substituteInPlace "$x" \
41-
--replace 'DESTINATION "''${PG_SHAREDIR}/extension"' "DESTINATION \"$out/share/postgresql/extension\""
42-
done
43-
44-
for x in src/CMakeLists.txt src/loader/CMakeLists.txt tsl/src/CMakeLists.txt; do
45-
substituteInPlace "$x" \
46-
--replace 'DESTINATION ''${PG_PKGLIBDIR}' "DESTINATION \"$out/lib\""
47-
done
48-
'';
38+
postPatch = ''
39+
for x in CMakeLists.txt sql/CMakeLists.txt; do
40+
if [ -f "$x" ]; then
41+
substituteInPlace "$x" \
42+
--replace 'DESTINATION "''${PG_SHAREDIR}/extension"' "DESTINATION \"$out/share/postgresql/extension\""
43+
fi
44+
done
45+
46+
for x in src/CMakeLists.txt src/loader/CMakeLists.txt tsl/src/CMakeLists.txt; do
47+
if [ -f "$x" ]; then
48+
substituteInPlace "$x" \
49+
--replace 'DESTINATION ''${PG_PKGLIBDIR}' "DESTINATION \"$out/lib\""
50+
fi
51+
done
52+
'';
53+
54+
postInstall = ''
55+
if [ -f $out/lib/timescaledb.so ]; then
56+
mv $out/lib/timescaledb.so $out/lib/timescaledb-${version}.so
57+
fi
58+
if [ -f $out/share/postgresql/extension/timescaledb.control ]; then
59+
mv $out/share/postgresql/extension/timescaledb.control $out/share/postgresql/extension/timescaledb--${version}.control
60+
fi
61+
'';
4962

50-
meta = with lib; {
51-
description = "Scales PostgreSQL for time-series data via automatic partitioning across time and space";
52-
homepage = "https://www.timescale.com/";
53-
changelog = "https://github.com/timescale/timescaledb/blob/${version}/CHANGELOG.md";
54-
platforms = postgresql.meta.platforms;
55-
license = licenses.asl20;
56-
broken = versionOlder postgresql.version "13";
63+
meta = with lib; {
64+
description =
65+
"Scales PostgreSQL for time-series data via automatic partitioning across time and space";
66+
homepage = "https://www.timescale.com/";
67+
changelog =
68+
"https://github.com/timescale/timescaledb/blob/${version}/CHANGELOG.md";
69+
license = licenses.postgresql;
70+
inherit (postgresql.meta) platforms;
71+
};
72+
};
73+
74+
allVersions =
75+
(builtins.fromJSON (builtins.readFile ./versions.json)).timescaledb;
76+
supportedVersions = lib.filterAttrs (_: value:
77+
builtins.elem (lib.versions.major postgresql.version) value.postgresql)
78+
allVersions;
79+
versions = lib.naturalSort (lib.attrNames supportedVersions);
80+
latestVersion = lib.last versions;
81+
numberOfVersions = builtins.length versions;
82+
packages = builtins.attrValues
83+
(lib.mapAttrs (name: value: build name value.hash (value.revision or name))
84+
supportedVersions);
85+
in pkgs.buildEnv {
86+
name = pname;
87+
paths = packages;
88+
postBuild = ''
89+
{
90+
echo "default_version = '${latestVersion}'"
91+
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control
92+
} > $out/share/postgresql/extension/${pname}.control
93+
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
94+
95+
# checks
96+
(set -x
97+
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${
98+
toString (numberOfVersions + 1)
99+
}"
100+
)
101+
'';
102+
pathsToLink = [ "/lib" "/share/postgresql/extension" ];
103+
passthru = {
104+
inherit versions numberOfVersions;
105+
pname = "${pname}-all";
106+
version = "multi-" + lib.concatStringsSep "-"
107+
(map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
57108
};
58109
}

nix/ext/versions.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,19 @@
1010
"pgrx": "0.14.3",
1111
"rust": "1.87.0"
1212
}
13+
},
14+
"timescaledb": {
15+
"2.9.1": {
16+
"postgresql": [
17+
"15"
18+
],
19+
"hash": "sha256-fvVSxDiGZAewyuQ2vZDb0I6tmlDXl6trjZp8+qDBtb8="
20+
},
21+
"2.16.1": {
22+
"postgresql": [
23+
"15"
24+
],
25+
"hash": "sha256-sLxWdBmih9mgiO51zLLxn9uwJVYc5JVHJjSWoADoJ+w="
26+
}
1327
}
1428
}

nix/tests/timescaledb.nix

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{ self, pkgs }:
2+
let
3+
inherit (pkgs) lib;
4+
installedExtension = postgresMajorVersion:
5+
self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/timescaledb-all";
6+
versions = (installedExtension "15").versions;
7+
firstVersion = lib.head versions;
8+
latestVersion = lib.last versions;
9+
postgresqlWithExtension = postgresql:
10+
let
11+
majorVersion = lib.versions.major postgresql.version;
12+
pkg = pkgs.buildEnv {
13+
name = "postgresql-${majorVersion}-timescaledb";
14+
paths = [ postgresql postgresql.lib (installedExtension majorVersion) ];
15+
passthru = {
16+
inherit (postgresql) version psqlSchema;
17+
lib = pkg;
18+
withPackages = _: pkg;
19+
};
20+
nativeBuildInputs = [ pkgs.makeWrapper ];
21+
pathsToLink = [ "/" "/bin" "/lib" ];
22+
postBuild = ''
23+
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
24+
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib
25+
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib
26+
'';
27+
};
28+
in pkg;
29+
in self.inputs.nixpkgs.lib.nixos.runTest {
30+
name = "timescaledb";
31+
hostPkgs = pkgs;
32+
nodes.server = { config, ... }: {
33+
virtualisation = {
34+
forwardPorts = [{
35+
from = "host";
36+
host.port = 13022;
37+
guest.port = 22;
38+
}];
39+
};
40+
services.openssh = { enable = true; };
41+
users.users.root.openssh.authorizedKeys.keys = [
42+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIArkmq6Th79Z4klW6Urgi4phN8yq769/l/10jlE00tU9"
43+
];
44+
45+
services.postgresql = {
46+
enable = true;
47+
package =
48+
postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
49+
settings = { shared_preload_libraries = "timescaledb"; };
50+
};
51+
52+
specialisation.postgresql15.configuration = {
53+
services.postgresql = {
54+
package = lib.mkForce
55+
(postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15);
56+
};
57+
};
58+
};
59+
testScript = { nodes, ... }: ''
60+
def run_sql(query):
61+
return server.succeed(f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """).strip()
62+
63+
def check_upgrade_path():
64+
with subtest("Check timescaledb upgrade path"):
65+
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION IF EXISTS timescaledb;'")
66+
run_sql(r"""CREATE EXTENSION timescaledb WITH VERSION \"${firstVersion}\";""")
67+
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';""")
68+
assert installed_version == "${firstVersion}", f"Expected timescaledb version ${firstVersion}, but found {installed_version}"
69+
for version in [${
70+
lib.concatStringsSep ", " (map (s: ''"${s}"'') versions)
71+
}][1:]:
72+
run_sql(f"""ALTER EXTENSION timescaledb UPDATE TO '{version}';""")
73+
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';""")
74+
assert installed_version == version, f"Expected timescaledb version {version}, but found {installed_version}"
75+
76+
start_all()
77+
78+
server.wait_for_unit("multi-user.target")
79+
server.wait_for_unit("postgresql.service")
80+
81+
check_upgrade_path()
82+
'';
83+
}

0 commit comments

Comments
 (0)