cluster/services/patroni: take over existing databases and users

This commit is contained in:
Max Headroom 2024-08-15 00:19:26 +02:00
parent 340383f160
commit c5a8cfe852

View file

@ -49,7 +49,9 @@ in
user = {
destroyAfterDays = 0;
create = user: psqlSecret "${genPassword} ${user}" ''
CREATE USER ${user} PASSWORD '@SECRET@';
SELECT 'CREATE USER ${user}'
WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${user}')\gexec
ALTER USER ${user} PASSWORD '@SECRET@';
'';
destroy = psqlSecret "printenv OBJECT" ''
DROP USER @SECRET@;
@ -59,7 +61,11 @@ in
destroyAfterDays = 30;
deps = [ "user" ];
create = db: psql ''
CREATE DATABASE ${db} OWNER ${cfg.databases.${db}.owner};
SELECT 'CREATE DATABASE ${db} OWNER ${cfg.databases.${db}.owner}'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${db}')\gexec
'';
change = db: psql ''
ALTER DATABASE ${db} OWNER TO ${cfg.databases.${db}.owner};
'';
destroy = psqlSecret "printenv OBJECT" ''
DROP DATABASE @SECRET@;