8000 feat(pg_graphql): build multiple versions of the pg_graphql extension · jfroche/postgres@57ccdb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57ccdb2

Browse files
committed
feat(pg_graphql): build multiple versions of the pg_graphql extension
Build multiple versions of the pg_graphql extension on different PostgreSQL versions. Add test for the extensions and their upgrade on PostgreSQL 15 and 17.
1 parent 19bdfcc commit 57ccdb2

File tree

5 files changed

+424
-56
lines changed

5 files changed

+424
-56
lines changed

flake.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
inherit (final) writeShellScriptBin;
6565
};
6666

67+
buildPgrxExtension_0_11_2 = prev.buildPgrxExtension.override {
68+
cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_11_2;
69+
};
70+
6771
buildPgrxExtension_0_11_3 = prev.buildPgrxExtension.override {
6872
cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_11_3;
6973
};
@@ -1382,6 +1386,7 @@
13821386
psql_15 = makeCheckHarness basePackages.psql_15.bin;
13831387
psql_17 = makeCheckHarness basePackages.psql_17.bin;
13841388
psql_orioledb-17 = makeCheckHarness basePackages.psql_orioledb-17.bin;
1389+
pg_graphql = import ./nix/tests/pg_graphql.nix { inherit self; inherit pkgs; };
13851390
};
13861391

13871392
# Apps is a list of names of things that can be executed with 'nix run';

nix/cargo-pgrx/default.nix

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
{ lib
2-
, darwin
3-
, fetchCrate
4-
, openssl
5-
, pkg-config
6-
, makeRustPlatform
7-
, stdenv
8-
, rust-bin
1+
{
2+
lib,
3+
darwin,
4+
fetchCrate,
5+
openssl,
6+
pkg-config,
7+
makeRustPlatform,
8+
stdenv,
9+
rust-bin,
910
}:
1011
let
1112
rustVersion = "1.85.1";
@@ -14,9 +15,10 @@ let
1415
rustc = rust-bin.stable.${rustVersion}.default;
1516
};
1617
generic =
17-
{ version
18-
, hash
19-
, cargoHash
18+
{
19+
version,
20+
hash,
21+
cargoHash,
2022
}:
2123
rustPlatform.buildRustPackage rec {
2224
# rust-overlay uses 'cargo-auditable' wrapper for 'cargo' command, but it
@@ -33,12 +35,14 @@ let
3335
nativeBuildInputs = lib.optionals stdenv.hostPlatform.isLinux [
3436
pkg-config
3537
];
36-
buildInputs = lib.optionals stdenv.hostPlatform.isLinux [
37-
openssl
38-
] ++ lib.optionals stdenv.hostPlatform.isDarwin [
39-
darwin.apple_sdk.frameworks.Security
40-
];
41-
38+
buildInputs =
39+
lib.optionals stdenv.hostPlatform.isLinux [
40+
openssl
41+
]
42+
++ lib.optionals stdenv.hostPlatform.isDarwin [
43+
darwin.apple_sdk.frameworks.Security
44+
];
45+
4246
OPENSSL_DIR = "${openssl.dev}";
4347
OPENSSL_INCLUDE_DIR = "${openssl.dev}/include";
4448
OPENSSL_LIB_DIR = "${openssl.out}/lib";
@@ -61,6 +65,11 @@ let
6165
};
6266
in
6367
{
68+
cargo-pgrx_0_11_2 = generic {
69+
version = "0.11.2";
70+
hash = "sha256-8NlpMDFaltTIA8G4JioYm8LaPJ2RGKH5o6sd6lBHmmM=";
71+
cargoHash = "sha256-CbU5B0pvB9ApTZOdYP/ZwuIG8bqGzk/ING2PCM0q2bQ=";
72+
};
6473
cargo-pgrx_0_11_3 = generic {
6574
version = "0.11.3";
6675
hash = "sha256-UHIfwOdXoJvR4Svha6ud0FxahP1wPwUtviUwUnTmLXU=";

nix/ext/pg_graphql.nix

Lines changed: 157 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,166 @@
1-
{ lib, stdenv, fetchFromGitHub, postgresql, buildPgrxExtension_0_12_9, cargo, rust-bin }:
1+
{
2+
pkgs,
3+
lib,
4+
stdenv,
5+
fetchFromGitHub,
6+
postgresql,
7+
buildPgrxExtension_0_11_2,
8+
buildPgrxExtension_0_11_3,
9+
buildPgrxExtension_0_12_6,
10+
buildPgrxExtension_0_12_9,
11+
rust-bin,
12+
}:
213

314
let
4-
rustVersion = "1.81.0";
5-
cargo = rust-bin.stable.${rustVersion}.default;
6-
in
7-
buildPgrxExtension_0_12_9 rec {
815
pname = "pg_graphql";
9-
version = "1.5.11";
10-
inherit postgresql;
11-
12-
src = fetchFromGitHub {
13-
owner = "supabase";
14-
repo = pname;
15-
rev = "v${version}";
16-
hash = "sha256-BMZc9ui+2J3U24HzZZVCU5+KWhz+5qeUsRGeptiqbek=";
17-
};
16+
build =
17+
version: hash: rustVersion: buildPgrxExtension:
18+
let
19+
cargo = rust-bin.stable.${rustVersion}.default;
20+
previousVersions = lib.filter (v: v != version) versions;
21+
in
22+
buildPgrxExtension rec {
23+
inherit pname version postgresql;
1824

19-
nativeBuildInputs = [ cargo ];
20-
buildInputs = [ postgresql ];
21-
22-
CARGO = "${cargo}/bin/cargo";
23-
24-
cargoLock = {
25-
lockFile = "${src}/Cargo.lock";
26-
};
27-
# Setting RUSTFLAGS in env to ensure it's available for all phases
28-
env = lib.optionalAttrs stdenv.isDarwin {
29-
POSTGRES_LIB = "${postgresql}/lib";
30-
PGPORT = toString (5430 +
31-
(if builtins.match ".*_.*" postgresql.version != null then 1 else 0) + # +1 for OrioleDB
32-
((builtins.fromJSON (builtins.substring 0 2 postgresql.version)) - 15) * 2); # +2 for each major version
33-
RUSTFLAGS = "-C link-arg=-undefined -C link-arg=dynamic_lookup";
34-
NIX_BUILD_CORES = "4"; # Limit parallel jobs
35-
CARGO_BUILD_JOBS = "4"; # Limit cargo parallelism
36-
};
37-
CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG = true;
25+
src = fetchFromGitHub {
26+
owner = "supabase";
27+
repo = pname;
28+
rev = "v${version}";
29+
inherit hash;
30+
};
31+
32+
nativeBuildInputs = [ cargo ];
33+
buildInputs = [ postgresql ];
34+
35+
CARGO = "${cargo}/bin/cargo";
36+
37+
cargoLock = {
38+
lockFile = "${src}/Cargo.lock";
39+
};
40+
# Setting RUSTFLAGS in env to ensure it's available for all phases
41+
env = lib.optionalAttrs stdenv.isDarwin {
42+
POSTGRES_LIB = "${postgresql}/lib";
43+
PGPORT = toString (
44+
5430
45+
+ (if builtins.match ".*_.*" postgresql.version != null then 1 else 0)
46+
# +1 for OrioleDB
47+
+ ((builtins.fromJSON (builtins.substring 0 2 postgresql.version)) - 15) * 2
48+
); # +2 for each major version
49+
RUSTFLAGS = "-C link-arg=-undefined -C link-arg=dynamic_lookup";
50+
NIX_BUILD_CORES = "4"; # Limit parallel jobs
51+
CARGO_BUILD_JOBS = "4"; # Limit cargo parallelism
52+
};
53+
CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG = true;
54+
55+
preBuild = ''
56+
echo "Processing git tags..."
57+
echo '${builtins.concatStringsSep "," previousVersions}' | sed 's/,/\n/g' > git_tags.txt
58+
'';
3859

60+
postInstall = ''
61+
mv $out/lib/${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
3962
40-
doCheck = false;
63+
create_sql_files() {
64+
echo "Creating SQL files for previous versions..."
65+
current_version="${version}"
66+
sql_file="$out/share/postgresql/extension/${pname}--$current_version.sql"
4167
42-
meta = with lib; {
43-
description = "GraphQL support for PostreSQL";
44-
homepage = "https://github.com/supabase/${pname}";
45-
platforms = postgresql.meta.platforms;
46-
license = licenses.postgresql;
68+
if [ -f "$sql_file" ]; then
69+
while read -r previous_version; do
70+
if [ "$(printf '%s\n' "$previous_version" "$current_version" | sort -V | head -n1)" = "$previous_version" ] && [ "$previous_version" != "$current_version" ]; then
71+
new_file="$out/share/postgresql/extension/${pname}--$previous_version--$current_version.sql"
72+
sed -i 's/create\s\+function/CREATE OR REPLACE FUNCTION/Ig' "$sql_file"
73+
echo "Creating $new_file"
74+
{
75+
echo "DROP EVENT TRIGGER IF EXISTS graphql_watch_ddl;"
76+
echo "DROP EVENT TRIGGER IF EXISTS graphql_watch_drop;"
77+
cat $sql_file
78+
} > "$new_file"
79+
fi
80+
done < git_tags.txt
81+
else
82+
echo "Warning: $sql_file not found"
83+
fi
84+
rm git_tags.txt
85+
}
86+
87+
create_control_files() {
88+
sed -e "/^default_version =/d" \
89+
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}'|" \
90+
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
91+
rm $out/share/postgresql/extension/${pname}.control
92+
93+
if [[ "${version}" == "${latestVersion}" ]]; then
94+
{
95+
echo "default_version = '${latestVersion}'"
96+
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control
97+
} > $out/share/postgresql/extension/${pname}.control
98+
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
99+
fi
100+
}
101+
102+
create_sql_files
103+
create_control_files
104+
'';
105+
106+
preCheck = ''
107+
export PGRX_HOME=$(mktemp -d)
108+
export NIX_PGLIBDIR=$PGRX_HOME/${lib.versions.major postgresql.version}/lib
109+
${lib.getExe pkgs.rsync} --chmod=ugo+w -a ${postgresql}/ ${postgresql.lib}/ $PGRX_HOME/${lib.versions.major postgresql.version}/
110+
cargo pgrx init --pg${lib.versions.major postgresql.version} $PGRX_HOME/${lib.versions.major postgresql.version}/bin/pg_config
111+
'';
112+
113+
doCheck = false;
114+
115+
meta = with lib; {
116+
description = "GraphQL support for PostreSQL";
117+
homepage = "https://github.com/supabase/${pname}";
118+
license = licenses.postgresql;
119+
inherit (postgresql.meta) platforms;
120+
};
121+
};
122+
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).pg_graphql;
123+
supportedVersions = lib.filterAttrs (
124+
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
125+
) allVersions;
126+
versions = lib.naturalSort (lib.attrNames supportedVersions);
127+
latestVersion = lib.last versions;
128+
numberOfVersions = builtins.length versions;
129+
mapPgrxExtension =
130+
version:
131+
{
132+
"0.11.2" = buildPgrxExtension_0_11_2;
133+
"0.11.3" = buildPgrxExtension_0_11_3;
134+
"0.12.6" = buildPgrxExtension_0_12_6;
135+
"0.12.9" = buildPgrxExtension_0_12_9;
136+
}
137+
."${version}";
138+
packages = builtins.attrValues (
139+
lib.mapAttrs (
140+
name: value: build name value.hash value.rust (mapPgrxExtension value.pgrx)
141+
) supportedVersions
142+
);
143+
144+
in
145+
pkgs.buildEnv {
146+
name = pname;
147+
paths = packages;
148+
pathsToLink = [
149+
"/lib"
150+
"/share/postgresql/extension"
151+
];
152+
postBuild = ''
153+
# checks
154+
(set -x
155+
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${
156+
toString (numberOfVersions + 1)
157+
}"
158+
)
159+
'';
160+
passthru = {
161+
inherit versions numberOfVersions;
162+
pname = "${pname}-all";
163+
version =
164+
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
47165
};
48166
}

nix/ext/versions.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"pg_graphql": {
3+
"1.4.4": {
4+
"postgresql": [
5+
"15"
6+
],
7+
"hash": "sha256-Kxo4o8+hfSTOjvhYyGF2BpksWfW/AMCCH4qom4AGw18=",
8+
"pgrx": "0.11.2",
9+
"rust": "1.85.1"
10+
},
11+
"1.5.0": {
12+
"postgresql": [
13+
"15"
14+
],
15+
"hash": "sha256-28ANRZyF22qF2YAxNAAkPfGOM3+xiO6IHdXsTp0CTQE=",
16+
"pgrx": "0.11.2",
17+
"rust": "1.85.1"
18+
},
19+
"1.5.1": {
20+
"postgresql": [
21+
"15"
22+
],
23+
"hash": "sha256-cAiD2iSFmZwC+Zy0x+MABseWCxXRtRY74Dj0oBKet+o=",
24+
"pgrx": "0.11.2",
25+
"rust": "1.85.1"
26+
},
27+
"1.5.1-mergeless": {
28+
"postgresql": [
29+
"15"
30+
],
31+
"hash": "sha256-X4YR2ishxWCQDMwxHKuGGjlpbpRrUBoHeeLfM/UIHWc=",
32+
"pgrx": "0.11.2",
33+
"rust": "1.85.1"
34+
},
35+
"1.5.4": {
36+
"postgresql": [
37+
"15"
38+
],
39+
"hash": "sha256-419RVol44akUFZ/0B97VjAXCUrWcKFDAFuVjvJnbkP4=",
40+
"pgrx": "0.11.3",
41+
"rust": "1.85.1"
42+
},
43+
"1.5.6": {
44+
"postgresql": [
45+
"15"
46+
],
47+
"hash": "sha256-v/40TR/1bplbQuD3Hv3gE7oh6cfn9fA6U5s+FTAwxtA=",
48+
"pgrx": "0.11.3",
49+
"rust": "1.85.1"
50+
},
51+
"1.5.7": {
52+
"postgresql": [
53+
"15"
54+
],
55+
"hash": "sha256-Q6XfcTKVOjo5pGy8QACc4QCHolKxEGU8e0TTC6Zg8go=",
56+
"pgrx": "0.11.3",
57+
"rust": "1.85.1"
58+
},
59+
"1.5.9": {
60+
"postgresql": [
61+
"15",
62+
"17"
63+
],
64+
"hash": "sha256-YpLN43FtLhp2cb7cyM+4gEx8GTwsRiKTfxaMq0b8hk0=",
65+
"pgrx": "0.12.6",
66+
"rust": "1.81.0"
67+
},
68+
"1.5.11": {
69+
"postgresql": [
70+
"15",
71+
"17"
72+
],
73+
"hash": "sha256-BMZc9ui+2J3U24HzZZVCU5+KWhz+5qeUsRGeptiqbek=",
74+
"pgrx": "0.12.9",
75+
"rust": "1.81.0"
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)
0