2021-07-29 12:05:51 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.my.services.postgresql;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.my.services.postgresql = with lib; {
|
|
|
|
enable = my.mkDisableOption "postgres configuration";
|
2021-07-29 12:44:42 +02:00
|
|
|
|
|
|
|
# Transient option to be enabled for migrations
|
|
|
|
upgradeScript = mkEnableOption "postgres upgrade script";
|
2021-07-29 12:05:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkMerge [
|
|
|
|
# Let other services enable postgres when they need it
|
|
|
|
(lib.mkIf cfg.enable {
|
|
|
|
services.postgresql = {
|
2024-12-16 00:20:18 +01:00
|
|
|
package = pkgs.postgresql_17;
|
2021-07-29 12:05:51 +02:00
|
|
|
};
|
|
|
|
})
|
2021-07-29 12:44:42 +02:00
|
|
|
|
|
|
|
# Taken from the manual
|
|
|
|
(lib.mkIf cfg.upgradeScript {
|
|
|
|
environment.systemPackages =
|
|
|
|
let
|
2024-05-07 13:10:29 +02:00
|
|
|
pgCfg = config.services.postgresql;
|
2024-12-16 00:20:18 +01:00
|
|
|
newPackage' = pkgs.postgresql_17;
|
2024-05-07 13:10:29 +02:00
|
|
|
|
|
|
|
oldPackage = if pgCfg.enableJIT then pgCfg.package.withJIT else pgCfg.package;
|
|
|
|
oldData = pgCfg.dataDir;
|
2024-12-16 00:19:31 +01:00
|
|
|
oldBin = "${if pgCfg.extensions == [] then oldPackage else oldPackage.withPackages pgCfg.extensions}/bin";
|
2024-05-07 13:10:29 +02:00
|
|
|
|
|
|
|
newPackage = if pgCfg.enableJIT then newPackage'.withJIT else newPackage';
|
|
|
|
newData = "/var/lib/postgresql/${newPackage.psqlSchema}";
|
2024-12-16 00:19:31 +01:00
|
|
|
newBin = "${if pgCfg.extensions == [] then newPackage else newPackage.withPackages pgCfg.extensions}/bin";
|
2021-07-29 12:44:42 +02:00
|
|
|
in
|
|
|
|
[
|
|
|
|
(pkgs.writeScriptBin "upgrade-pg-cluster" ''
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2024-05-07 13:10:29 +02:00
|
|
|
set -eux
|
|
|
|
export OLDDATA="${oldData}"
|
|
|
|
export NEWDATA="${newData}"
|
|
|
|
export OLDBIN="${oldBin}"
|
|
|
|
export NEWBIN="${newBin}"
|
2021-07-29 12:44:42 +02:00
|
|
|
|
|
|
|
if [ "$OLDDATA" -ef "$NEWDATA" ]; then
|
|
|
|
echo "Cannot migrate to same data directory" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
install -d -m 0700 -o postgres -g postgres "$NEWDATA"
|
|
|
|
cd "$NEWDATA"
|
2024-05-07 13:10:29 +02:00
|
|
|
sudo -u postgres "$NEWBIN/initdb" -D "$NEWDATA"
|
2021-07-29 12:44:42 +02:00
|
|
|
|
|
|
|
systemctl stop postgresql # old one
|
|
|
|
|
2024-05-07 13:10:29 +02:00
|
|
|
sudo -u postgres "$NEWBIN/pg_upgrade" \
|
2021-07-29 12:44:42 +02:00
|
|
|
--old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \
|
2024-05-07 13:10:29 +02:00
|
|
|
--old-bindir "$OLDBIN" --new-bindir "$NEWBIN" \
|
2021-07-29 12:44:42 +02:00
|
|
|
"$@"
|
2024-05-07 13:20:40 +02:00
|
|
|
|
|
|
|
cat << EOF
|
|
|
|
Run the following commands after setting:
|
|
|
|
services.postgresql.package = pkgs.postgresql_${lib.versions.major newPackage.version}
|
|
|
|
sudo -u postgres vacuumdb --all --analyze-in-stages
|
|
|
|
${newData}/delete_old_cluster.sh
|
|
|
|
EOF
|
2021-07-29 12:44:42 +02:00
|
|
|
'')
|
|
|
|
];
|
|
|
|
})
|
2021-07-29 12:05:51 +02:00
|
|
|
];
|
|
|
|
}
|