bins: add upgrade-pg script

This commit is contained in:
Aaron Bieber 2023-12-02 08:15:37 -07:00
parent c957533a5c
commit f8195e107f
No known key found for this signature in database
2 changed files with 43 additions and 1 deletions

View File

@ -1,4 +1,5 @@
{ pkgs
, config
, isUnstable
, ...
}:
@ -19,6 +20,10 @@ let
inherit (pkgs) curl;
});
genPatches = pkgs.callPackage ./gen-patches.nix { };
upgrade-pg = pkgs.writeScriptBin "upgrade-pg" (import ./upgrade-pg.nix {
inherit pkgs;
inherit config;
});
in
{
environment.systemPackages = with pkgs; [
@ -29,7 +34,9 @@ in
tstart
xclip
xinStatus
];
] ++ (if config.services.postgresql.enable then
[ upgrade-pg ]
else [ ]);
environment.etc = {
"signify/openbsd-70-base.pub".text =
builtins.readFile ./pubs/openbsd-70-base.pub;

35
bins/upgrade-pg.nix Normal file
View File

@ -0,0 +1,35 @@
{ pkgs, config }:
let
newPostgres = pkgs.postgresql_16;
in
''
#!${pkgs.yash}/bin/yash
set -xe
if [ pgrep postgres ]; then
echo "Please exit all postgres services and stop postgres!"
systemctl list-dependencies postgresql.service --reverse
exit 1;
fi
export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}"
export OLDDATA="${config.services.postgresql.dataDir}"
if [ "$NEWDATA" == "$OLDDATA" ]; then
echo "Nothing to upgrade!"
exit 1;
fi
export NEWBIN="${newPostgres}/bin"
export OLDBIN="${config.services.postgresql.package}/bin"
install -d -m 0700 -o postgres -g postgres "$NEWDATA"
cd "$NEWDATA"
su - postgres -c "$NEWBIN/initdb -D $NEWDATA"
su - postgres -c "$NEWBIN/pg_upgrade \
--old-datadir $OLDDATA --new-datadir $NEWDATA \
--old-bindir $OLDBIN --new-bindir $NEWBIN \
$@"
''