From 84c8e901de5dbb77cc90d4eb8ce7a91372446e46 Mon Sep 17 00:00:00 2001 From: asyncnomi Date: Sat, 2 Aug 2025 23:25:16 +0200 Subject: [PATCH] simplify the master<->slave first synchronisation --- shared/db/postgres.nix | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/shared/db/postgres.nix b/shared/db/postgres.nix index db30377..c59a635 100644 --- a/shared/db/postgres.nix +++ b/shared/db/postgres.nix @@ -62,24 +62,12 @@ in }; }; # This preStart script sync the slaves to the master - # systemd.services..preStart has a mergeable type - systemd.services.postgresql.preStart = lib.mkIf (builtins.elem myName mapping.db.slaves) '' - if test -e ${cfg.dataDir}/.first_startup; then + # systemd.services..preStart has a mergeable type, + # this code will be prepend to the default one + systemd.services.postgresql.preStart = lib.mkIf (builtins.elem myName mapping.db.slaves) (lib.mkBefore '' + if ! test -e ${cfg.dataDir}/PG_VERSION; then echo "Setting up PostgreSQL slave replication..." - # This is a sl that's defined by the default preStart script - # We need an empty dataDir for the pg_basebackup - # And there is no easy ways that I'm aware of - # to get the hash of that file without recomputing it - PSQL_CONF_PATH="${cfg.dataDir}/postgresql.conf" - PSQL_CONF_TARGET=$(readlink "$PSQL_CONF_PATH") - - # Remove data dir - if [ -d "${cfg.dataDir}" ]; then - echo "Deleting postgres data dir: ${cfg.dataDir}" - rm -rf "${cfg.dataDir}" - fi - # Perform base backup from master echo "Starting base backup from master: ${masterIP}" ${cfg.package}/bin/pg_basebackup \ @@ -91,12 +79,9 @@ in -Xs \ -R - # Symlink back the psql configFile - ln -sf "$PSQL_CONF_TARGET" "$PSQL_CONF_PATH" - echo "PostgreSQL slave setup completed successfully" else echo "PostgreSQL standby already configured (standby.signal exists), skipping base backup" fi - ''; + ''); } \ No newline at end of file