-- =============================================
-- Nettoyage de la BDD avant installation.
-- (Supprimer le contenu au bout de 3 mois)    
--   - Suppression de tables 
--   - Suppression de champs
--   - Suppression de vues/fonctions SQL
--   - Suppression ou modification de séquences
-- =============================================

DROP VIEW IF EXISTS activite.v_mouvements_sejour_3 CASCADE;

DROP INDEX IF EXISTS i_expert_sejour_controle_4;

SELECT base.cti_execute('ALTER TABLE ' || nspname || '.' || relname || ' DROP CONSTRAINT ' || conname, 1)
FROM pg_constraint
         JOIN pg_namespace ON pg_namespace.oid = pg_constraint.connamespace
         JOIN pg_catalog.pg_class ON pg_class.oid = pg_constraint.conrelid
WHERE conname = 't_lits_etage_id_key' AND
    nspname like 'activite%'
;

SELECT base.cti_execute('DROP INDEX IF EXISTS ' || nspname || '.' || pg_class.relname, 1)
FROM pg_class
         JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
WHERE
    pg_namespace.nspname LIKE 'activite%' AND
    pg_class.relname = 't_lits_etage_id_key'
;

DROP TABLE IF EXISTS activite.t_sejours_ignores;

ALTER TABLE activite.t_services_facturation
    DROP COLUMN IF EXISTS unite_medicale_default_id
    ,DROP COLUMN IF EXISTS unite_medicale_except1_etage_id
    ,DROP COLUMN IF EXISTS unite_medicale_except1_id
    ,DROP COLUMN IF EXISTS unite_medicale_except2_activite_id
    ,DROP COLUMN IF EXISTS unite_medicale_except2_etage_id
    ,DROP COLUMN IF EXISTS unite_medicale_except2_id
    ,DROP COLUMN IF EXISTS unite_medicale_except3_activite_id
    ,DROP COLUMN IF EXISTS unite_medicale_except1_activite_id
    ,DROP COLUMN IF EXISTS unite_medicale_except3_etage_id
    ,DROP COLUMN IF EXISTS unite_medicale_except3_id
;

ALTER TABLE activite.t_lieux_c DROP COLUMN IF EXISTS etage_nb_cp CASCADE;
ALTER TABLE activite.t_lieux_c DROP COLUMN IF EXISTS etage_nb_lits CASCADE;
ALTER TABLE activite.t_lieux_c DROP COLUMN IF EXISTS service_facturation_nb_cp CASCADE;
ALTER TABLE activite.t_lieux_c DROP COLUMN IF EXISTS service_facturation_nb_lits CASCADE;
ALTER TABLE activite.t_lieux_c DROP COLUMN IF EXISTS etage_section_nb_cp CASCADE;
ALTER TABLE activite.t_lieux_c DROP COLUMN IF EXISTS etage_section_nb_lits CASCADE;

ALTER TABLE activite.t_medecins_administratifs
	DROP COLUMN IF EXISTS is_medecin_executant
	,DROP COLUMN IF EXISTS is_medecin_sejour
	,DROP COLUMN IF EXISTS statut
;

ALTER TABLE activite.t_forme_activite_rule
	DROP COLUMN IF EXISTS liste_activite
	,DROP COLUMN IF EXISTS liste_sauf_prestation
	,DROP COLUMN IF EXISTS liste_sauf_rubrique
;

ALTER TABLE activite.p_factures_encours
	DROP COLUMN IF EXISTS est_od
;

ALTER TABLE activite.p_expert_sejour_controle
    DROP COLUMN IF EXISTS borne_alerte
    ,DROP COLUMN IF EXISTS code_groupe
    ,DROP COLUMN IF EXISTS coefficient_attendu
    ,DROP COLUMN IF EXISTS cumul_attendu
    ,DROP COLUMN IF EXISTS cumul_base
    ,DROP COLUMN IF EXISTS cumul_realise
    ,DROP COLUMN IF EXISTS etat
    ,DROP COLUMN IF EXISTS numero_editeur_activite
;

ALTER TABLE activite.p_sejour_pmsi DROP COLUMN IF EXISTS rss_id_array
;

DROP FUNCTION IF EXISTS activite.cti_reorganize_codes_postaux_c();

ALTER TABLE activite.p_sejours
    DROP COLUMN IF EXISTS cm_id
    ,DROP COLUMN IF EXISTS etab_destination_id
    ,DROP COLUMN IF EXISTS etab_provenance_id
    ,DROP COLUMN IF EXISTS gmd_id
    ,DROP COLUMN IF EXISTS gn_id
    ,DROP COLUMN IF EXISTS medecin_prescripteur_id
    ,DROP COLUMN IF EXISTS rgme_id
    ,DROP COLUMN IF EXISTS rss_id
    ,DROP COLUMN IF EXISTS tiers_payant_12_id
;

DROP VIEW IF EXISTS activite.v_lignes_oap_1 CASCADE
;

DROP VIEW IF EXISTS activite.v_lignes_oap_2 CASCADE
;

ALTER TABLE activite.t_ghm_c
    DROP COLUMN IF EXISTS pole_oap_texte
    ,DROP COLUMN IF EXISTS pole_oap_id
	,DROP COLUMN IF EXISTS pole_oap_code
	,DROP COLUMN IF EXISTS ligne_oap_id
	,DROP COLUMN IF EXISTS ligne_oap_texte
	,DROP COLUMN IF EXISTS ligne_oap_code
;
DROP TABLE IF EXISTS activite.p_sejours_checksum
;

ALTER TABLE activite.t_ghs_c
DROP COLUMN IF EXISTS pole_oap_texte
    ,DROP COLUMN IF EXISTS pole_oap_id
	,DROP COLUMN IF EXISTS pole_oap_code
	,DROP COLUMN IF EXISTS ligne_oap_id
	,DROP COLUMN IF EXISTS ligne_oap_texte
	,DROP COLUMN IF EXISTS ligne_oap_code
;

SELECT base.cti_execute('DROP INDEX IF EXISTS ' || nspname || '.' || pg_class.relname, 1)
FROM pg_class
JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
WHERE
    pg_namespace.nspname LIKE 'activite_%' AND 
    pg_class.relname = 'p_sejours_groupes_smr_ukey'
;

SELECT base.cti_execute('DROP INDEX IF EXISTS ' || nspname || '.' || pg_class.relname, 1)
FROM pg_class
JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
WHERE
    pg_namespace.nspname LIKE 'activite_%' AND
    pg_class.relname = 'p_sejours_groupes_smr_pkey'
;