Skip to content

Commit 0c276b6

Browse files
committed
utilisation de transaction pour gérer l'option dry-run
1 parent 1491ad6 commit 0c276b6

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

qfdmo/management/commands/update_external_ids.py

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import json
33

44
from django.core.management.base import BaseCommand
5+
from django.db import transaction
56

67
from qfdmo.models.acteur import Acteur, ActeurStatus, RevisionActeur
78

8-
CHUNK = 1000
9-
109

1110
class Command(BaseCommand):
1211
help = "Export Ressources using CSV format"
@@ -39,48 +38,54 @@ def handle(self, *args, **options):
3938
with open(mapping_file, "r") as f:
4039
mapping = json.load(f)
4140

42-
for old_id, new_id in mapping.items():
43-
# Check if the ids are not empty
44-
if old_id and new_id:
45-
self.stdout.write(
46-
self.style.SUCCESS(f"Updating `{old_id}` to `{new_id}`")
47-
)
48-
else:
49-
self.stdout.write(
50-
self.style.WARNING(
51-
f"Skipping `{old_id}` to `{new_id}` : one of the ids is empty"
41+
with transaction.atomic():
42+
for old_id, new_id in mapping.items():
43+
# Check if the ids are not empty
44+
if old_id and new_id:
45+
self.stdout.write(
46+
self.style.SUCCESS(f"Mapping `{old_id}` to `{new_id}`")
5247
)
53-
)
54-
continue
55-
56-
# test acteur with this new external id already exists
57-
acteur_from_db = Acteur.objects.filter(
58-
identifiant_externe=new_id, source__code=source_code
59-
).first()
60-
id_index = 0
61-
while acteur_from_db:
62-
id_index += 1
63-
new_id_indexed = f"{new_id}_{id_index}"
64-
acteur_from_db = Acteur.objects.filter(
65-
identifiant_externe=new_id_indexed, source__code=source_code
48+
else:
49+
self.stdout.write(
50+
self.style.WARNING(
51+
f"Skipping `{old_id}` to `{new_id}` : one of the ids is"
52+
" empty"
53+
)
54+
)
55+
continue
56+
57+
# Update acteur if exists
58+
acteur = Acteur.objects.filter(
59+
identifiant_externe=old_id, source__code=source_code
6660
).first()
6761

68-
try:
69-
new_id = new_id_indexed
70-
statut = ActeurStatus.INACTIF
71-
except NameError:
72-
statut = ActeurStatus.ACTIF
62+
if not acteur:
63+
self.stdout.write(self.style.WARNING(f"Acteur {old_id} not found"))
64+
continue
7365

74-
# Update acteur if exists
75-
acteur = Acteur.objects.filter(
76-
identifiant_externe=old_id, source__code=source_code
77-
).first()
66+
# test acteur with this new external id already exists
67+
acteur_from_db = Acteur.objects.filter(
68+
identifiant_externe=new_id, source__code=source_code
69+
).first()
70+
id_index = 0
71+
while acteur_from_db:
72+
self.stdout.write(
73+
self.style.WARNING(
74+
f"Acteur {acteur_from_db.identifiant_externe} already"
75+
" exists, trying to find an unused id"
76+
)
77+
)
78+
id_index += 1
79+
new_id_indexed = f"{new_id}_{id_index}"
80+
acteur_from_db = Acteur.objects.filter(
81+
identifiant_externe=new_id_indexed, source__code=source_code
82+
).first()
7883

79-
if not acteur:
80-
self.stdout.write(self.style.WARNING(f"Acteur {old_id} not found"))
81-
continue
84+
statut = ActeurStatus.ACTIF
85+
if id_index:
86+
new_id = f"{new_id}_{id_index}"
87+
statut = ActeurStatus.INACTIF
8288

83-
if not dry_run:
8489
self.stdout.write(
8590
self.style.SUCCESS(
8691
f"Updating acteur {acteur.identifiant_unique} to {new_id}"
@@ -89,31 +94,23 @@ def handle(self, *args, **options):
8994
acteur.identifiant_externe = new_id
9095
acteur.statut = statut
9196
acteur.save()
92-
else:
93-
self.stdout.write(
94-
self.style.WARNING(
95-
f"Dry run: would update Acteur {old_id} to {new_id}"
96-
)
97-
)
9897

99-
# Update revision acteur if exists
100-
revision_acteur = RevisionActeur.objects.filter(
101-
identifiant_externe=old_id, source__code=source_code
102-
).first()
98+
# Update revision acteur if exists
99+
revision_acteur = RevisionActeur.objects.filter(
100+
identifiant_externe=old_id, source__code=source_code
101+
).first()
103102

104-
if revision_acteur and not dry_run:
105-
self.stdout.write(
106-
self.style.SUCCESS(
107-
f"Updating revision acteur {revision_acteur.identifiant_unique}"
108-
f" to {new_id}"
109-
)
110-
)
111-
revision_acteur.identifiant_externe = new_id
112-
revision_acteur.statut = statut
113-
revision_acteur.save()
114-
if revision_acteur and dry_run:
115-
self.stdout.write(
116-
self.style.WARNING(
117-
f"Dry run: would update RevisionActeur {old_id} to {new_id}"
103+
if revision_acteur:
104+
self.stdout.write(
105+
self.style.SUCCESS(
106+
"Updating revision acteur "
107+
f"{revision_acteur.identifiant_unique} to {new_id}"
108+
)
118109
)
119-
)
110+
revision_acteur.identifiant_externe = new_id
111+
revision_acteur.statut = statut
112+
revision_acteur.save()
113+
114+
if dry_run:
115+
# Rollback the transaction
116+
raise Exception("Rolling back transaction because of dry run")

0 commit comments

Comments
 (0)