I made a little pl/pgsql script to rename some sequences (prefixes adding) and set their schema to 'public'. However I don't understand why but my 'ELSE' instructions are executed only once in the loop, which is not logical because I have many rows whose value of 'nspname' is other than 'docuprocess' :
CREATE OR REPLACE FUNCTION move_schemas_to_public(target_schemas text[]) RETURNS integer AS $procedure$
DECLARE
rec RECORD;
sql text;
newname text;
nbreq integer := 0;
tabsize integer := array_length(target_schemas, 1);
i integer := 1;
debug boolean := false;
BEGIN
-- [...]
FOR rec in
select nspname, c.relname
from pg_class c
inner join pg_namespace ns
on (c.relnamespace = ns.oid)
where c.relkind = 'S'
and ns.nspname = any(target_schemas)
order by 1, 2
LOOP
IF rec.nspname = 'docuprocess' THEN
newname := rec.relname;
ELSE
-- Why these instructions are executed only once : -----
newname := rec.nspname||'_'||rec.relname;
sql := 'ALTER SEQUENCE '||rec.nspname||'.'||rec.relname||' RENAME TO '||newname;
RAISE NOTICE '%', sql;
IF debug is not true THEN
EXECUTE sql;
END IF;
nbreq := nbreq + 1;
--------------------------------------------------------
END IF;
sql := 'ALTER SEQUENCE '||rec.nspname||'.'||newname||' SET SCHEMA public';
RAISE NOTICE '%', sql;
IF debug is not true THEN
EXECUTE sql;
END IF;
nbreq := nbreq + 1;
END LOOP;
-- [...]
RETURN nbreq;
END;
select move_schemas_to_public(
-- schemas list
ARRAY[
'docufacture',
'docuprocess',
'formulaire',
'notification'
]
);
Here is the result for the loop's SQL query :
[nspname];[relname]
"docufacture";"exportdoc_idexportdoc_seq"
"docufacture";"tableau_idcolonne_seq"
"docuprocess";"dp_action_champsdocuged_seq"
"docuprocess";"dp_action_commentaire_seq"
"docuprocess";"dp_action_docuged_seq"
"docuprocess";"dp_action_email_id_seq"
"docuprocess";"dp_action_formulaire_seq"
"docuprocess";"dp_action_id_seq"
"docuprocess";"dp_action_imprimer_id_seq"
"docuprocess";"dp_action_lancer_processus_id_seq"
"docuprocess";"dp_action_lancer_programme_id_seq"
"docuprocess";"dp_action_seq"
"docuprocess";"dp_action_transfert_fichier_id_seq"
"docuprocess";"dp_deroulement_etape_seq"
"docuprocess";"dp_deroulement_processus_seq"
"docuprocess";"dp_etape_seq"
"docuprocess";"dp_indisponibilite_seq"
"docuprocess";"dp_intervenant_seq"
"docuprocess";"dp_processus_seq"
"docuprocess";"dp_type_action_seq"
"formulaire";"champ_id_seq"
"formulaire";"fond_id_seq"
"formulaire";"formulaire_id_seq"
"formulaire";"modele_id_seq"
"notification";"notification_id_seq"
Thanks in advance for precious help.