-
-
Notifications
You must be signed in to change notification settings - Fork 189
feat: multiple versions of the hypopg extension #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jfroche
wants to merge
1
commit into
supabase:develop
Choose a base branch
from
jfroche:multi-version-ext/hypopg
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,95 @@ | ||
{ lib, stdenv, fetchFromGitHub, postgresql }: | ||
{ | ||
pkgs, | ||
lib, | ||
stdenv, | ||
fetchFromGitHub, | ||
postgresql, | ||
}: | ||
|
||
stdenv.mkDerivation rec { | ||
let | ||
pname = "hypopg"; | ||
version = "1.4.1"; | ||
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname}; | ||
supportedVersions = lib.filterAttrs ( | ||
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql | ||
) allVersions; | ||
versions = lib.naturalSort (lib.attrNames supportedVersions); | ||
latestVersion = lib.last versions; | ||
numberOfVersions = builtins.length versions; | ||
build = | ||
version: hash: | ||
stdenv.mkDerivation rec { | ||
inherit pname version; | ||
|
||
buildInputs = [ postgresql ]; | ||
buildInputs = [ postgresql ]; | ||
|
||
src = fetchFromGitHub { | ||
owner = "HypoPG"; | ||
repo = pname; | ||
rev = "refs/tags/${version}"; | ||
hash = "sha256-88uKPSnITRZ2VkelI56jZ9GWazG/Rn39QlyHKJKSKMM="; | ||
}; | ||
src = fetchFromGitHub { | ||
owner = "HypoPG"; | ||
repo = pname; | ||
rev = "refs/tags/${version}"; | ||
inherit hash; | ||
}; | ||
|
||
installPhase = '' | ||
mkdir -p $out/{lib,share/postgresql/extension} | ||
|
||
mv ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix} | ||
|
||
create_sql_files() { | ||
echo "Creating SQL files for previous versions..." | ||
if [[ "${version}" == "${latestVersion}" ]]; then | ||
cp *.sql $out/share/postgresql/extension | ||
fi | ||
} | ||
|
||
create_control_files() { | ||
sed -e "/^default_version =/d" \ | ||
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}'|" \ | ||
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control | ||
|
||
if [[ "${version}" == "${latestVersion}" ]]; then | ||
{ | ||
echo "default_version = '${latestVersion}'" | ||
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control | ||
} > $out/share/postgresql/extension/${pname}.control | ||
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix} | ||
fi | ||
} | ||
|
||
installPhase = '' | ||
mkdir -p $out/{lib,share/postgresql/extension} | ||
create_sql_files | ||
create_control_files | ||
''; | ||
|
||
cp *${postgresql.dlSuffix} $out/lib | ||
cp *.sql $out/share/postgresql/extension | ||
cp *.control $out/share/postgresql/extension | ||
meta = with lib; { | ||
description = "Hypothetical Indexes for PostgreSQL"; | ||
homepage = "https://github.com/HypoPG/${pname}"; | ||
license = licenses.postgresql; | ||
inherit (postgresql.meta) platforms; | ||
}; | ||
}; | ||
packages = builtins.attrValues ( | ||
lib.mapAttrs (name: value: build name value.hash) supportedVersions | ||
); | ||
in | ||
pkgs.buildEnv { | ||
name = pname; | ||
paths = packages; | ||
pathsToLink = [ | ||
"/lib" | ||
"/share/postgresql/extension" | ||
]; | ||
postBuild = '' | ||
# checks | ||
(set -x | ||
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${ | ||
toString (numberOfVersions + 1) | ||
}" | ||
) | ||
''; | ||
|
||
meta = with lib; { | ||
description = "Hypothetical Indexes for PostgreSQL"; | ||
homepage = "https://github.com/HypoPG/${pname}"; | ||
platforms = postgresql.meta.platforms; | ||
license = licenses.postgresql; | ||
passthru = { | ||
inherit versions numberOfVersions; | ||
pname = "${pname}-all"; | ||
version = | ||
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions); | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"hypopg": { | ||
"1.3.1": { | ||
"postgresql": [ | ||
"15" | ||
], | ||
"hash": "sha256-AIBXy+LxyHUo+1hd8gQTwaBdFiTEzKaCVc4cx5tZgME=" | ||
}, | ||
"1.4.1": { | ||
"postgresql": [ | ||
"15", | ||
"17" | ||
], | ||
"hash": "sha256-88uKPSnITRZ2VkelI56jZ9GWazG/Rn39QlyHKJKSKMM=" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
{ self, pkgs }: | ||
let | ||
pname = "hypopg"; | ||
inherit (pkgs) lib; | ||
installedExtension = | ||
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all"; | ||
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions; | ||
postgresqlWithExtension = | ||
postgresql: | ||
let | ||
majorVersion = lib.versions.major postgresql.version; | ||
pkg = pkgs.buildEnv { | ||
name = "postgresql-${majorVersion}-${pname}"; | ||
paths = [ | ||
postgresql | ||
postgresql.lib | ||
(installedExtension majorVersion) | ||
]; | ||
passthru = { | ||
inherit (postgresql) version psqlSchema; | ||
lib = pkg; | ||
withPackages = _: pkg; | ||
}; | ||
nativeBuildInputs = [ pkgs.makeWrapper ]; | ||
pathsToLink = [ | ||
"/" | ||
"/bin" | ||
"/lib" | ||
]; | ||
postBuild = '' | ||
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib | ||
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib | ||
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib | ||
''; | ||
}; | ||
in | ||
pkg; | ||
in | ||
self.inputs.nixpkgs.lib.nixos.runTest { | ||
name = pname; | ||
hostPkgs = pkgs; | ||
nodes.server = | ||
{ config, ... }: | ||
{ | ||
virtualisation = { | ||
forwardPorts = [ | ||
{ | ||
from = "host"; | ||
host.port = 13022; | ||
guest.port = 22; | ||
} | ||
]; | ||
}; | ||
|
||
services.postgresql = { | ||
enable = true; | ||
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15; | ||
settings = { | ||
shared_preload_libraries = pname; | ||
}; | ||
}; | ||
|
||
specialisation.postgresql17.configuration = { | ||
services.postgresql = { | ||
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17); | ||
}; | ||
|
||
systemd.services.postgresql-migrate = { | ||
serviceConfig = { | ||
Type = "oneshot"; | ||
RemainAfterExit = true; | ||
User = "postgres"; | ||
Group = "postgres"; | ||
StateDirectory = "postgresql"; | ||
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}"; | ||
}; | ||
script = | ||
let | ||
oldPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15; | ||
newPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17; | ||
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}"; | ||
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}"; | ||
in | ||
'' | ||
if [[ ! -d ${newDataDir} ]]; then | ||
install -d -m 0700 -o postgres -g postgres "${newDataDir}" | ||
${newPostgresql}/bin/initdb -D "${newDataDir}" | ||
echo "shared_preload_libraries = '${pname}'" >> "${newDataDir}/postgresql.conf" | ||
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \ | ||
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" | ||
else | ||
echo "${newDataDir} already exists" | ||
fi | ||
''; | ||
}; | ||
|
||
systemd.services.postgresql = { | ||
after = [ "postgresql-migrate.service" ]; | ||
requires = [ "postgresql-migrate.service" ]; | ||
}; | ||
}; | ||
|
||
}; | ||
testScript = | ||
{ nodes, ... }: | ||
let | ||
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17"; | ||
in | ||
'' | ||
versions = { | ||
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}], | ||
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}], | ||
} | ||
|
||
def run_sql(query): | ||
return server.succeed(f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """).strip() | ||
|
||
def check_upgrade_path(pg_version): | ||
with subtest("Check ${pname} upgrade path"): | ||
firstVersion = versions[pg_version][0] | ||
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION IF EXISTS ${pname};'") | ||
run_sql(f"""CREATE EXTENSION ${pname} WITH VERSION '{firstVersion}';""") | ||
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = '${pname}';""") | ||
assert installed_version == firstVersion, f"Expected ${pname} version {firstVersion}, but found {installed_version}" | ||
for version in versions[pg_version][1:]: | ||
run_sql(f"""ALTER EXTENSION ${pname} UPDATE TO '{version}';""") | ||
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = '${pname}';""") | ||
assert installed_version == version, f"Expected ${pname} version {version}, but found {installed_version}" | ||
|
||
start_all() | ||
|
||
server.wait_for_unit("multi-user.target") | ||
server.wait_for_unit("postgresql.service") | ||
|
||
check_upgrade_path("15") | ||
|
||
with subtest("Check ${pname} latest extension version"): | ||
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION ${pname};'") | ||
server.succeed("sudo -u postgres psql -c 'CREATE EXTENSION ${pname};'") | ||
installed_extensions=run_sql(r"""SELECT extname, extversion FROM pg_extension;""") | ||
latestVersion = versions["15"][-1] | ||
assert f"${pname},{latestVersion}" in installed_extensions | ||
|
||
with subtest("switch to postgresql 17"): | ||
server.succeed( | ||
"${pg17-configuration}/bin/switch-to-configuration test >&2" | ||
) | ||
|
||
with subtest("Check ${pname} latest extension version"): | ||
installed_extensions=run_sql(r"""SELECT extname, extversion FROM pg_extension;""") | ||
latestVersion = versions["17"][-1] | ||
assert f"${pname},{latestVersion}" in installed_extensions | ||
|
||
check_upgrade_path("17") | ||
''; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.