Commit 63fa6101 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Suppression du répertoire migration/ obsolète

parent f185aece
Loading
Loading
Loading
Loading

migration/.gitignore

deleted100644 → 0
+0 −2
Changes for migration/.gitignore: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
ora2pg/config/*.conf
ora2pg/data/*.sql
 No newline at end of file

migration/README.md

deleted100644 → 0
+0 −202
Changes for migration/README.md: 0 added lines, 202 removed lines.
Original line number Diff line number Diff line
Migration de la base de données Oracle de SyGAL vers PostgreSQL
===============================================================

Le projet de migration *ora2pg* a été créé dans le sous-répertoire `ora2pg` en suivant la doc du dépôt
[ora2pg](https://git.unicaen.fr/dsi/ora2pg).


Pré-requis
----------

- Se placer dans le répertoire racine du projet SyGAL. 
  Dans la suite, `${PWD}` correspond donc à ce répertoire racine.


Génération des scripts SQL de migration
---------------------------------------

### Structure

1) Lancement de *ora2pg* pour générer les scripts de création du schéma PostgreSQL

```bash
docker run --rm \
-v ${PWD}/migration:/migration \
-w /migration/ora2pg \
ora2pg-image \
./export_schema.sh -c ./config/ora2pg.conf
```

*Les scripts sont générés dans `${PWD}/migration/ora2pg/schema/`.*

Pour effacer les existants :
```bash
sudo rm -f \
migration/ora2pg/reports/* \
migration/ora2pg/schema/dblinks/* \
migration/ora2pg/schema/directories/* \
migration/ora2pg/schema/functions/* \
migration/ora2pg/schema/grants/* \
migration/ora2pg/schema/mviews/* \
migration/ora2pg/schema/packages/* \
migration/ora2pg/schema/partitions/* \
migration/ora2pg/schema/procedures/* \
migration/ora2pg/schema/sequences/* \
migration/ora2pg/schema/synonyms/* \
migration/ora2pg/schema/tables/* \
migration/ora2pg/schema/tablespaces/* \
migration/ora2pg/schema/triggers/* \
migration/ora2pg/schema/types/* \
migration/ora2pg/schema/views/* \
migration/ora2pg/sources/functions/* \
migration/ora2pg/sources/mviews/* \
migration/ora2pg/sources/packages/* \
migration/ora2pg/sources/partitions/* \
migration/ora2pg/sources/procedures/* \
migration/ora2pg/sources/triggers/* \
migration/ora2pg/sources/types/* \
migration/ora2pg/sources/views/*
```

Remarques : 
- il est possible de restriendre la liste des "objets" à prendre en compte en modifiant les variables 
`EXPORT_TYPE` et `SOURCE_TYPE` dans le script `${PWD}/migration/ora2pg/export_schema.sh`.
Exemple pour ne générer que les tables, indexes, constraints et foreign keys : 
```
EXPORT_TYPE="TABLE"
SOURCE_TYPE="TABLE"
```
- il est aussi possible d'exclure certains objets selon leur nom en modifiant le variable `EXCLUDE` dans le fichier
de config `${PWD}/migration/ora2pg/config/ora2pg.conf`. Exemple :
```
EXCLUDE	V_IMPORT_TAB_COLS STR_REDUCE MV_INDICATEUR_.*
```


2) Correction nécessaire des scripts SQL générés

```bash
docker run --rm \
-v ${PWD}/migration:/migration \
-w /migration \
ora2pg-image \
./bin/prepare_schema_scripts.sh
```


### 2/ Données

*ora2pg* génère des scripts d'insertion de données qu'il faudra importer avec la ligne de commande `psql`.

*NB : ça mouline grave pour les tables volumineuses et la barre de progression n'est pas forcément rafraîchie.*

```bash
docker run --rm \
--network "host" \
-v ${PWD}/migration:/migration \
-w /migration/ora2pg \
ora2pg-image \
ora2pg -t COPY -o data.sql -b ./data -c ./config/ora2pg.conf --exclude \
API_LOG,\
IMPORT_LOG,\
INFORMATION_FICHIER_SAV,\
SYNC_LOG,\
SYNCHRO_LOG,\
TMP_.*,\
MV_.*,\
Z_.*
```

Pour ne prendre en compte QUE les tables contenant les données minimales d'installation de l'appli :
```bash
docker run --rm \
--network "host" \
-v ${PWD}/migration:/migration \
-w /migration/ora2pg \
ora2pg-image \
ora2pg -t COPY -o data.sql -b ./data -c ./config/ora2pg.conf --allow \
CATEGORIE_PRIVILEGE,\
DOMAINE_SCIENTIFIQUE,\
IMPORT_OBSERV,\
INFORMATION_LANGUE,\
NATURE_FICHIER,\
PRIVILEGE,\
PROFIL,\
PROFIL_PRIVILEGE,\
SOUTENANCE_CONFIGURATION,\
SOUTENANCE_ETAT,\
SOUTENANCE_QUALITE,\
TYPE_RAPPORT,\
TYPE_STRUCTURE,\
TYPE_VALIDATION,\
VERSION_FICHIER,\
WF_ETAPE
```

Les scripts sont générés dans `${PWD}/migration/ora2pg/data/`.
Pour les effacer :
```bash
sudo rm -rf migration/ora2pg/data/*
```



Import des objets et données dans la base PostgreSQL
----------------------------------------------------

1) Exporter les variables d'environnement requises pour se connecter à la base de données PostgreSQL

```bash
export \
PGHOST=localhost \
PGPORT=5432 \
PGDATABASE=sygal \
PGUSER=ad_sygal \
PGPASSWORD=azerty
```

**Si besoin**, utiliser le script `migration/pg_clean.sql` pour vider entièrement la base de données PostgreSQL.

2) Lancer l'import

```bash
docker run -it --rm \
--network "host" \
-v ${PWD}/migration/ora2pg:/ora2pg \
-v ${PWD}/migration/docker/pg_before_import.sql:/tmp/pg_before_import.sql \
-w /ora2pg \
--env PGDATABASE \
--env PGUSER \
--env PGHOST \
--env PGPORT \
--env PGPASSWORD \
ora2pg-image \
./import_all.sh -f -x -d ${PGDATABASE} -h ${PGHOST} -U ${PGUSER} -o ${PGUSER} -b /tmp/pg_before_import.sql
```

NB : 
- Le script SQL "pg_before_import.sql" sera exécuté juste avant l'import.
- Votre feu vert explicite vous est demandé pour chaque type d'objet à importer. Ajouter `-y` pour éviter ça.
- Les indexes, constraints et foreign keys ne sont créés qu'une fois les données importées.

3) Lancer le script SQL post-import

```bash
docker run --rm \
--network "host" \
-v ${PWD}/migration/docker/pg_after_import.sql:/tmp/pg_after_import.sql \
--env PGDATABASE \
--env PGUSER \
--env PGHOST \
--env PGPORT \
--env PGPASSWORD \
ora2pg-image \
psql -d ${PGDATABASE} -h ${PGHOST} -U ${PGUSER} -f /tmp/pg_after_import.sql
```



The end
-------

:-)
+0 −36
Changes for migration/bin/prepare_schema_scripts.sh: 0 added lines, 36 removed lines.
Original line number Diff line number Diff line
#!/bin/bash

THIS_DIR=$(cd `dirname $0` && pwd)
ROOT_DIR=$(cd ${THIS_DIR}/.. && pwd)

SCHEMA_DIR=${ROOT_DIR}/ora2pg/schema

function processFile() {
  #sed -i '1,8d' $1   # suppression des 8 premières lignes
  sed -i -e 's/"//g' $1   # suppression des guillemets
  sed -i -e 's/NULL * AS TYPE_STRUCTURE_DEPENDANT_ID/NULL::bigint AS TYPE_STRUCTURE_DEPENDANT_ID/g' $1

  sed -i -e 's/EST_ANNEXE = 0/EST_ANNEXE = false/g' $1
  sed -i -e 's/EST_EXPURGE = 0/EST_EXPURGE = false/g' $1
  sed -i -e 's/VERSION_CORRIGEE = 0/VERSION_CORRIGEE = false/g' $1
  sed -i -e 's/est_valide = 0/est_valide = false/g' $1
  sed -i -e 's/EST_VALIDE = 0/EST_VALIDE = false/g' $1
  sed -i -e 's/EST_CONFORME = 0/EST_CONFORME = false/g' $1
  sed -i -e 's/importable = 1/importable = true/g' $1
  sed -i -e 's/VERSION_CORRIGEE = 1/VERSION_CORRIGEE = true/g' $1
  sed -i -e 's/est_valide = 1/est_valide = true/g' $1
  sed -i -e 's/EST_VALIDE = 1/EST_VALIDE = true/g' $1
  sed -i -e 's/EST_CONFORME = 1/EST_CONFORME = true/g' $1
  sed -i -e 's/then 1 else 0 end atteignable/then true else false end atteignable/g' $1
  sed -i -e 's/then 1 else 0 end courante/then true else false end courante/g' $1

  echo "Fichier $f traité."
}

for f in ${SCHEMA_DIR}/functions/*_function.sql ; do processFile $f ; done
#for f in ${SCHEMA_DIR}/mviews/*_mview.sql ;       do processFile $f ; done
for f in ${SCHEMA_DIR}/tables/*_table.sql ;       do processFile $f ; done
for f in ${SCHEMA_DIR}/triggers/*_trigger.sql ;   do processFile $f ; done
for f in ${SCHEMA_DIR}/views/*_view.sql ;         do processFile $f ; done

echo "Préparation des scripts terminée."
+0 −28
Changes for migration/corrections_apres_migration.sql: 0 added lines, 28 removed lines.
Original line number Diff line number Diff line

create or replace function trigger_fct_individu_rech_update() returns trigger
    security definer
    language plpgsql
as
    $$
DECLARE
BEGIN
    IF TG_OP = 'INSERT' THEN
        insert into INDIVIDU_RECH(ID, HAYSTACK)
        values (NEW.ID, individu_haystack(NEW.NOM_USUEL, NEW.NOM_PATRONYMIQUE, NEW.PRENOM1, NEW.EMAIL, NEW.SOURCE_CODE));
    END IF;
    IF TG_OP = 'UPDATE' THEN
        UPDATE INDIVIDU_RECH
        SET HAYSTACK = individu_haystack(NEW.NOM_USUEL, NEW.NOM_PATRONYMIQUE, NEW.PRENOM1, NEW.EMAIL, NEW.SOURCE_CODE)
        where ID = NEW.ID;
    END IF;
    IF TG_OP = 'DELETE' THEN
        delete from INDIVIDU_RECH where id = OLD.ID;
    END IF;
    IF TG_OP = 'DELETE' THEN
        RETURN OLD;
    ELSE
        RETURN NEW;
    END IF;

END
$$;
+0 −139
Changes for migration/docker/pg_after_import.sql: 0 added lines, 139 removed lines.
Original line number Diff line number Diff line
--
-- Correction de fonctions mal traduites par ora2pg.
--
create or replace function trigger_fct_individu_rech_update() returns trigger
    security definer
    language plpgsql
as
$$
DECLARE
BEGIN
    IF TG_OP = 'INSERT' THEN
        insert into INDIVIDU_RECH(ID, HAYSTACK)
        values (NEW.ID, individu_haystack(NEW.NOM_USUEL, NEW.NOM_PATRONYMIQUE, NEW.PRENOM1, NEW.EMAIL, NEW.SOURCE_CODE));
    END IF;
    IF TG_OP = 'UPDATE' THEN
        UPDATE INDIVIDU_RECH
        SET HAYSTACK = individu_haystack(NEW.NOM_USUEL, NEW.NOM_PATRONYMIQUE, NEW.PRENOM1, NEW.EMAIL, NEW.SOURCE_CODE)
        where ID = NEW.ID;
    END IF;
    IF TG_OP = 'DELETE' THEN
        delete from INDIVIDU_RECH where id = OLD.ID;
    END IF;
    IF TG_OP = 'DELETE' THEN
        RETURN OLD;
    ELSE
        RETURN NEW;
    END IF;

END
$$;

--
-- Correction de la vue `src_role` : colonne `these_dep` => booléen
-- NB : la vue v_diff_role peut être supprimée, ele sera recréée automatiquement par l'import.
--
drop view if exists v_diff_role
;
drop view if exists src_role
;
create or replace view src_role
            (id, source_code, source_id, libelle, code, role_id, these_dep, structure_id,
             type_structure_dependant_id) as
SELECT NULL::text                                       AS id,
       tmp.source_code,
       src.id                                           AS source_id,
       tmp.lib_roj                                      AS libelle,
       tmp.id                                           AS code,
       (tmp.lib_roj::text || ' '::text) || s.code::text AS role_id,
       true                                             AS these_dep,
       s.id                                             AS structure_id,
       NULL::bigint                                     AS type_structure_dependant_id
FROM tmp_role tmp
         JOIN structure s ON s.source_code::text = tmp.etablissement_id::text
         JOIN etablissement e ON e.structure_id = s.id
         JOIN source src ON src.code::text = tmp.source_id::text
;


--
-- Cf. migration/docker/pg_before_import.sql
--
-- >>>> On ne peut pas créer cette MV car elle se base sur V_DIFF_INDIVIDU qui est surpprimé/générée à chaque import par unicaen/db-import
--      la solution est d'inclure le source de V_DIFF_INDIVIDU dans cette MV.
--
-- CREATE MATERIALIZED VIEW mv_indicateur_121 AS
-- select * FROM individu
-- where SOURCE_CODE in (
--     SELECT SOURCE_CODE
--     FROM V_DIFF_INDIVIDU
--     WHERE operation = 'insert'
-- )
-- ;

--
-- Peuplement des VM.
--
refresh materialized view mv_recherche_these;
refresh materialized view mv_indicateur_1;
refresh materialized view mv_indicateur_2;
refresh materialized view mv_indicateur_3;
refresh materialized view mv_indicateur_4;
refresh materialized view mv_indicateur_5;
refresh materialized view mv_indicateur_6;
refresh materialized view mv_indicateur_7;
refresh materialized view mv_indicateur_21;
refresh materialized view mv_indicateur_61;
refresh materialized view mv_indicateur_62;
refresh materialized view mv_indicateur_81;
refresh materialized view mv_indicateur_101;
--refresh materialized view mv_indicateur_121;
refresh materialized view mv_indicateur_141;


--
-- Transformation de certains `bigint` inutiles en `int` ou `smallint` quand c'est possible
-- (peut-être faisable en amont dans la config ora2pg ?)
--
alter table categorie_privilege            alter column ordre                          type smallint;
-- alter table etablissement                  alter column est_etab_inscription           type smallint;
alter table faq                            alter column ordre                          type smallint;
alter table fichier                        alter column taille                         type int;
--alter table financement                    alter column annee                          type smallint;
-- alter table information                    alter column est_visible                    type smallint;
alter table information                    alter column priorite                       type smallint;
alter table information_fichier_sav        alter column createur                       type smallint;
-- alter table notif                          alter column enabled                        type smallint;
alter table privilege                      alter column ordre                          type smallint;
alter table profil                         alter column ordre                          type smallint;
alter table profil                         alter column structure_type                 type smallint;
alter table rapport                        alter column annee_univ                     type smallint;
-- alter table role                           alter column is_default                     type smallint;
alter table soutenance_membre              alter column qualite                        type smallint;
-- alter table soutenance_membre              alter column visio                          type smallint;
-- alter table soutenance_proposition         alter column exterieur                      type smallint;
-- alter table soutenance_proposition         alter column huit_clos                      type smallint;
-- alter table soutenance_proposition         alter column label_europeen                 type smallint;
-- alter table soutenance_proposition         alter column manuscrit_anglais              type smallint;
-- alter table soutenance_proposition         alter column soutenance_anglais             type smallint;
--alter table these_annee_univ               alter column annee_univ                     type smallint;
--alter table tmp_these                      alter column annee_univ_1ere_insc           type smallint;
--alter table tmp_these_annee_univ           alter column annee_univ                     type smallint;
alter table utilisateur                    alter column state                          type smallint;
alter table wf_etape                       alter column chemin                         type smallint;
--alter table wf_etape                       alter column ordre                          type smallint;
/******* alter générés avec
with tmp as (
select c.*
from information_schema.columns c
left join information_schema.views v on c.table_name = v.table_name
where v.table_name is null
and c.table_catalog = 'sygal_dev'
and c.table_schema = 'public'
and c.data_type = 'bigint'
and c.column_name not like '%id'
)
select 'alter table '||rpad(table_name, 30)||' alter column '||rpad(column_name, 30)||' type smallint;'
from tmp
order by table_name, column_name;
*********/
Loading