nixos: services: postgres: simplify update script

This commit is contained in:
Bruno BELANYI 2024-05-07 11:10:29 +00:00
parent 6162f4f4d5
commit 48beb9f1fe

View file

@ -27,17 +27,26 @@ in
environment.systemPackages = environment.systemPackages =
let let
newpg = config.containers.temp-pg.config.services.postgresql; pgCfg = config.services.postgresql;
newPackage' = pkgs.postgresql_13;
oldPackage = if pgCfg.enableJIT then pgCfg.package.withJIT else pgCfg.package;
oldData = pgCfg.dataDir;
oldBin = "${if pgCfg.extraPlugins == [] then oldPackage else oldPackage.withPackages pgCfg.extraPlugins}/bin";
newPackage = if pgCfg.enableJIT then newPackage'.withJIT else newPackage';
newData = "/var/lib/postgresql/${newPackage.psqlSchema}";
newBin = "${if pgCfg.extraPlugins == [] then newPackage else newPackage.withPackages pgCfg.extraPlugins}/bin";
in in
[ [
(pkgs.writeScriptBin "upgrade-pg-cluster" '' (pkgs.writeScriptBin "upgrade-pg-cluster" ''
#!/usr/bin/env bash #!/usr/bin/env bash
set -x set -eux
export OLDDATA="${config.services.postgresql.dataDir}" export OLDDATA="${oldData}"
export NEWDATA="${newpg.dataDir}" export NEWDATA="${newData}"
export OLDBIN="${config.services.postgresql.package}/bin" export OLDBIN="${oldBin}"
export NEWBIN="${newpg.package}/bin" export NEWBIN="${newBin}"
if [ "$OLDDATA" -ef "$NEWDATA" ]; then if [ "$OLDDATA" -ef "$NEWDATA" ]; then
echo "Cannot migrate to same data directory" >&2 echo "Cannot migrate to same data directory" >&2
@ -46,13 +55,13 @@ in
install -d -m 0700 -o postgres -g postgres "$NEWDATA" install -d -m 0700 -o postgres -g postgres "$NEWDATA"
cd "$NEWDATA" cd "$NEWDATA"
sudo -u postgres $NEWBIN/initdb -D "$NEWDATA" sudo -u postgres "$NEWBIN/initdb" -D "$NEWDATA"
systemctl stop postgresql # old one systemctl stop postgresql # old one
sudo -u postgres $NEWBIN/pg_upgrade \ sudo -u postgres "$NEWBIN/pg_upgrade" \
--old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \ --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
--old-bindir $OLDBIN --new-bindir $NEWBIN \ --old-bindir "$OLDBIN" --new-bindir "$NEWBIN" \
"$@" "$@"
'') '')
]; ];