Skip to content

Commit 422f7f8

Browse files
committed
feat: multiple versions for the timescaledb-apache extension
1 parent cb61848 commit 422f7f8

File tree

5 files changed

+210
-108
lines changed

5 files changed

+210
-108
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: 96 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
pkgs,
23
lib,
34
stdenv,
45
fetchFromGitHub,
@@ -8,51 +9,104 @@
89
libkrb5,
910
}:
1011

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

15-
nativeBuildInputs = [ cmake ];
16-
buildInputs = [
17-
postgresql
18-
openssl
19-
libkrb5
20-
];
19+
nativeBuildInputs = [ cmake ];
20+
buildInputs = [
21+
postgresql
22+
openssl
23+
libkrb5
24+
];
2125

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

29-
cmakeFlags = [
30-
"-DSEND_TELEMETRY_DEFAULT=OFF"
31-
"-DREGRESS_CHECKS=OFF"
32-
"-DTAP_CHECKS=OFF"
33-
"-DAPACHE_ONLY=1"
34-
] ++ lib.optionals stdenv.isDarwin [ "-DLINTER=OFF" ];
35-
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-
'';
33+
cmakeFlags = [
34+
"-DSEND_TELEMETRY_DEFAULT=OFF"
35+
"-DREGRESS_CHECKS=OFF"
36+
"-DTAP_CHECKS=OFF"
37+
"-DAPACHE_ONLY=1"
38+
] ++ lib.optionals stdenv.isDarwin [ "-DLINTER=OFF" ];
39+
40+
postPatch = ''
41+
for x in CMakeLists.txt sql/CMakeLists.txt; do
42+
if [ -f "$x" ]; then
43+
substituteInPlace "$x" \
44+
--replace 'DESTINATION "''${PG_SHAREDIR}/extension"' "DESTINATION \"$out/share/postgresql/extension\""
45+
fi
46+
done
47+
48+
for x in src/CMakeLists.txt src/loader/CMakeLists.txt tsl/src/CMakeLists.txt; do
49+
if [ -f "$x" ]; then
50+
substituteInPlace "$x" \
51+
--replace 'DESTINATION ''${PG_PKGLIBDIR}' "DESTINATION \"$out/lib\""
52+
fi
53+
done
54+
'';
4955

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";
56+
postInstall = ''
57+
if [ -f $out/lib/timescaledb.so ]; then
58+
mv $out/lib/timescaledb.so $out/lib/timescaledb-${version}.so
59+
fi
60+
if [ -f $out/share/postgresql/extension/timescaledb.control ]; then
61+
mv $out/share/postgresql/extension/timescaledb.control $out/share/postgresql/extension/timescaledb--${version}.control
62+
fi
63+
'';
64+
65+
meta = with lib; {
66+
description = "Scales PostgreSQL for time-series data via automatic partitioning across time and space";
67+
homepage = "https://www.timescale.com/";
68+
changelog = "https://github.com/timescale/timescaledb/blob/${version}/CHANGELOG.md";
69+
license = licenses.postgresql;
70+
inherit (postgresql.meta) platforms;
71+
};
72+
};
73+
74+
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).timescaledb;
75+
supportedVersions = lib.filterAttrs (
76+
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
77+
) allVersions;
78+
versions = lib.naturalSort (lib.attrNames supportedVersions);
79+
latestVersion = lib.last versions;
80+
numberOfVersions = builtins.length versions;
81+
packages = builtins.attrValues (
82+
lib.mapAttrs (name: value: build name value.hash (value.revision or name)) supportedVersions
83+
);
84+
in
85+
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 = [
103+
"/lib"
104+
"/share/postgresql/extension"
105+
];
106+
passthru = {
107+
inherit versions numberOfVersions;
108+
pname = "${pname}-all";
109+
version =
110+
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
57111
};
58112
}

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/packages/postgres.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
ourExtensions = [
2020
../ext/rum.nix
2121
../ext/timescaledb.nix
22-
../ext/timescaledb-2.9.1.nix
2322
../ext/pgroonga.nix
2423
../ext/index_advisor.nix
2524
../ext/wal2json.nix

nix/tests/timescaledb.nix

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{ self, pkgs }:
2+
let
3+
inherit (pkgs) lib;
4+
installedExtension =
5+
postgresMajorVersion:
6+
self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/timescaledb-all";
7+
versions = (installedExtension "15").versions;
8+
firstVersion = lib.head versions;
9+
postgresqlWithExtension =
10+
postgresql:
11+
let
12+
majorVersion = lib.versions.major postgresql.version;
13+
pkg = pkgs.buildEnv {
14+
name = "postgresql-${majorVersion}-timescaledb";
15+
paths = [
16+
postgresql
17+
postgresql.lib
18+
(installedExtension majorVersion)
19+
];
20+
passthru = {
21+
inherit (postgresql) version psqlSchema;
22+
lib = pkg;
23+
withPackages = _: pkg;
24+
};
25+
nativeBuildInputs = [ pkgs.makeWrapper ];
26+
pathsToLink = [
27+
"/"
28+
"/bin"
29+
"/lib"
30+
];
31+
postBuild = ''
32+
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
33+
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib
34+
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib
35+
'';
36+
};
37+
in
38+
pkg;
39+
in
40+
self.inputs.nixpkgs.lib.nixos.runTest {
41+
name = "timescaledb";
42+
hostPkgs = pkgs;
43+
nodes.server =
44+
{ ... }:
45+
{
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+
62+
services.postgresql = {
63+
enable = true;
64+
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
65+
settings = {
66+
shared_preload_libraries = "timescaledb";
67+
};
68+
};
69+
70+
specialisation.postgresql15.configuration = {
71+
services.postgresql = {
72+
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15);
73+
};
74+
};
75+
};
76+
testScript =
77+
{ ... }:
78+
''
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}"
92+
93+
start_all()
94+
95+
server.wait_for_unit("multi-user.target")
96+
server.wait_for_unit("postgresql.service")
97+
98+
check_upgrade_path()
99+
'';
100+
}

0 commit comments

Comments
 (0)