Fix race condition

This commit is contained in:
soraefir
2026-05-06 02:05:52 +02:00
parent dd192d2983
commit f81ba27e56

View File

@@ -41,17 +41,23 @@ in {
description = "Set DB passwords and ownership after Postgres is ready";
after = [ "postgresql.service" ];
requires = [ "postgresql.service" ];
partOf = [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
User = "postgres"; # Runs as postgres user directly
RemainAfterExit = true;
ExecStartPre = "${pkgs.coreutils}/bin/sleep 2";
};
script = ''
PSQL="${pkgs.postgresql}/bin/psql"
${lib.concatMapStringsSep "\n" (name: ''
until $PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname = '${name}_user'" | grep -q 1; do
echo "Waiting for user ${name}_user..."
sleep 1
done
$PSQL -tAc "ALTER DATABASE ${name}_db OWNER TO ${name}_user;"
if [ -f "${config.sops.secrets."${name}_pass".path}" ]; then