Skip to content

feat: multiple versions for the wrappers extension #1702

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
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ permissions:
packages: write

jobs:
nix-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v30
- id: set-matrix
name: Generate Nix Matrix
run: |
set -Eeu
matrix="$(nix eval --json '.#githubActions.matrix')"
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"

build-run-image:
name: ${{ matrix.name }} (${{ matrix.system }})
needs: nix-matrix
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- runner: large-linux-x86
arch: amd64
- runner: large-linux-arm
arch: arm64
- runner: macos-latest-xlarge
arch: arm64
runs-on: ${{ matrix.runner }}
matrix: ${{fromJSON(needs.nix-matrix.outputs.matrix)}}
timeout-minutes: 180
steps:
- name: Checkout Repo
Expand Down Expand Up @@ -103,11 +112,7 @@ jobs:
sudo rm -rf /tmp/* 2>/dev/null || true
echo "=== AFTER CLEANUP ==="
df -h
- name: Build psql bundle
run: >
nix run "github:Mic92/nix-fast-build?rev=b1dae483ab7d4139a6297e02b6de9e5d30e43d48"
-- --skip-cached --no-nom ${{ matrix.runner == 'macos-latest-xlarge' && '--max-jobs 1' || '' }}
--flake ".#checks.$(nix eval --raw --impure --expr 'builtins.currentSystem')"
- run: nix build -L '.#${{ matrix.attr }}'
env:
AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
Expand Down
21 changes: 21 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
git-hooks.url = "github:cachix/git-hooks.nix";
git-hooks.inputs.nixpkgs.follows = "nixpkgs";
nix-github-actions.url = "github:nix-community/nix-github-actions";
nix-github-actions.inputs.nixpkgs.follows = "nixpkgs";
};

outputs =
Expand All @@ -35,6 +37,7 @@
nix/nixpkgs.nix
nix/packages
nix/overlays
nix/github-actions.nix
];
});
}
9 changes: 4 additions & 5 deletions nix/checks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,11 @@
postgresql_17_src
;
}
// pkgs.lib.optionalAttrs (system == "x86_64-linux") {
wrappers = import ./ext/tests/wrappers.nix {
// pkgs.lib.optionalAttrs (system == "x86_64-linux") (
import ./ext/tests {
inherit self;
inherit pkgs;
};
devShell = self'.devShells.default;
};
}
);
};
}
173 changes: 173 additions & 0 deletions nix/ext/tests/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{ self, pkgs }:
let
testsDir = ./.;
testFiles = builtins.attrNames (builtins.readDir testsDir);
nixFiles = builtins.filter (
name: builtins.match ".*\\.nix$" name != null && name != "default.nix"
) testFiles;
extTest =
extension_name:
let
pname = extension_name;
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.openssh = {
enable = true;
};

services.postgresql = {
enable = true;
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
enableTCPIP = true;
initialScript = pkgs.writeText "init-postgres-with-password" ''
CREATE USER test WITH PASSWORD 'secret';
'';
authentication = ''
host test postgres samenet scram-sha-256
'';
};

networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];

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}"
${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"))}],
}
extension_name = "${pname}"
support_upgrade = True
pg17_configuration = "${pg17-configuration}"

${builtins.readFile ./lib.py}

start_all()

server.wait_for_unit("multi-user.target")
server.wait_for_unit("postgresql.service")

test = PostgresExtensionTest(server, extension_name, versions, support_upgrade)

with subtest("Check upgrade path with postgresql 15"):
test.check_upgrade_path("15")

last_version = None
with subtest("Check the install of the last version of the extension"):
last_version = test.check_install_last_version("15")

with subtest("switch to postgresql 17"):
server.succeed(
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
)

with subtest("Check last version of the extension after upgrade"):
test.assert_version_matches(last_version)

with subtest("Check upgrade path with postgresql 17"):
test.check_upgrade_path("17")
'';
};
in
builtins.listToAttrs (
map (file: {
name = "ext-" + builtins.replaceStrings [ ".nix" ] [ "" ] file;
value = import (testsDir + "/${file}") { inherit self pkgs; };
}) nixFiles
)
// builtins.listToAttrs (
map (extName: {
name = "ext-${extName}";
value = extTest extName;
}) [ "wrappers" ]
)
Loading
Loading