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 = {
|
2021-07-29 13:03:10 +02:00
|
|
|
package = pkgs.postgresql_13;
|
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;
|
|
|
|
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";
|
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
|
|
|
"$@"
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
})
|
2021-07-29 12:05:51 +02:00
|
|
|
];
|
|
|
|
}
|