From 2ae98b15e083b4994e1bfcf900891e142de9ee44 Mon Sep 17 00:00:00 2001 From: amandine-sahl Date: Mon, 14 Mar 2022 11:10:16 +0100 Subject: [PATCH] Ajout option --keep-cdnom pour empecher la suppression des cd_noms manquants - cf #306 --- apptax/migrations/taxref/commands.py | 16 +++++++++++----- apptax/migrations/taxref/utils.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/apptax/migrations/taxref/commands.py b/apptax/migrations/taxref/commands.py index 45c901b8..5f4f4a03 100644 --- a/apptax/migrations/taxref/commands.py +++ b/apptax/migrations/taxref/commands.py @@ -44,9 +44,13 @@ def import_taxref_v15(): @routes.cli.command() -def test_changes_detection(): +@click.option("--keep-cdnom", is_flag=True) +def test_changes_detection(keep_cdnom): """Analyse des répercussions de changement de taxref + :param keep-cdnom: Indique si l'on souhaite concerver les cd_noms manquant au lieu de les supprimer + :type keep-cdnom: boolean + 3 étapes : - Detection des cd_noms manquants - Création d'une copie de travail de bib_noms @@ -54,15 +58,16 @@ def test_changes_detection(): de leur répercussion sur les attributs et medias de taxhub """ # Analyse des changements à venir - analyse_taxref_changes(without_substitution=False) + analyse_taxref_changes(without_substitution=False, keep_missing_cd_nom=keep_cdnom) @routes.cli.command() @click.option("--keep-oldtaxref", is_flag=True) @click.option("--keep-oldbdc", is_flag=True) +@click.option("--keep-cdnom", is_flag=True) @click.option("--script_predetection", type=click.Path(exists=True)) @click.option("--script_postdetection", type=click.Path(exists=True)) -def apply_changes(keep_oldtaxref, keep_oldbdc, script_predetection, script_postdetection): +def apply_changes(keep_oldtaxref, keep_oldbdc, keep_cdnom, script_predetection, script_postdetection): """Procédure de migration de taxref Taxref v14 vers v15 Application des changements import des données dans les tables taxref et bdc_status @@ -72,14 +77,15 @@ def apply_changes(keep_oldtaxref, keep_oldbdc, script_predetection, script_postd :type keep-oldtaxref: boolean :param keep-oldbdc: Indique si l'on souhaite concerver l'ancienne version du referentiel bdc_status :type keep-oldbdc: boolean + :param keep-cdnom: Indique si l'on souhaite concerver les cd_noms manquant au lieu de les supprimer + :type keep-cdnom: boolean :param script_predetection: Emplacement d'un fichier sql de correction avant la detection des changements :type script_predetection: Path :param script_postdetection: Emplacement d'un fichier sql de correction après la detection des changements :type script_postdetection: Path """ - # Analyse des changements à venir - analyse_taxref_changes(without_substitution=False) + analyse_taxref_changes(without_substitution=False, keep_missing_cd_nom=keep_cdnom) # Save taxref and bdc_status data save_data(14, keep_oldtaxref, keep_oldbdc) diff --git a/apptax/migrations/taxref/utils.py b/apptax/migrations/taxref/utils.py index 25d13858..fece40b6 100644 --- a/apptax/migrations/taxref/utils.py +++ b/apptax/migrations/taxref/utils.py @@ -19,7 +19,7 @@ ) -def analyse_taxref_changes(without_substitution=True): +def analyse_taxref_changes(without_substitution=True, keep_missing_cd_nom=False): """ Analyse des répercussions de changement de taxref @@ -30,13 +30,13 @@ def analyse_taxref_changes(without_substitution=True): de leur répercussion sur les attributs et medias de taxhub """ # test if deleted cd_nom can be correct without manual intervention - if test_missing_cd_nom(without_substitution): + # And keep_missing_cd_nom is not set + if test_missing_cd_nom(without_substitution) and not keep_missing_cd_nom: logger.error("Some cd_nom will disappear without substitute. You can't continue migration. Analyse exports files") - # TODO ??? Force exit or not ??? https://github.com/PnX-SI/TaxHub/issues/306 exit() # Test missing cd_nom - create_copy_bib_noms() + create_copy_bib_noms(keep_missing_cd_nom) # Change detection and repport nb_of_conflict = detect_changes() @@ -46,7 +46,7 @@ def analyse_taxref_changes(without_substitution=True): exit() -def create_copy_bib_noms(): +def create_copy_bib_noms(keep_missing_cd_nom=False): """ Création d'une table copie de bib_noms """ @@ -59,6 +59,10 @@ def create_copy_bib_noms(): ) ) db.session.execute(query) + if keep_missing_cd_nom: + db.session.execute(text(""" + UPDATE taxonomie.tmp_bib_noms_copy tbnc SET deleted = FALSE WHERE deleted = TRUE; + """)) db.session.commit()