-- PGPASSWORD=${DOLI_DB_PASSWORD} psql -U ${DOLI_DB_USER} -h ${DOLI_DB_HOST} -p ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME}
DO $$
DECLARE
    current_schema_owner VARCHAR;      -- Propriétaire actuel du schéma public
BEGIN

    -- Obtenir le nom du propriétaire actuel du schéma 'public'
    SELECT tableowner INTO current_schema_owner
    FROM pg_tables
    WHERE schemaname = 'public'
    LIMIT 1;

    -- Si le propriétaire actuel est différent de {{ .Values.db.ownerRole }}
    IF current_schema_owner <> '{{ .Values.db.ownerRole }}' THEN
        -- Construire et exécuter la requête REASSIGN OWNED BY
        EXECUTE format('REASSIGN OWNED BY %I TO %I', current_schema_owner, '{{ .Values.db.ownerRole }}');
        RAISE NOTICE 'Ownership of all objects in schema "public" has been reassigned from % to %', current_schema_owner, '{{ .Values.db.ownerRole }}';
    ELSE
        RAISE NOTICE 'No change needed; the owner of schema "public" is already %', '{{ .Values.db.ownerRole }}';
    END IF;
END $$;
