57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
"""
|
|
Management command: export_project_members
|
|
==========================================
|
|
Exporte la table `projects_projectmember` vers un fichier JSON,
|
|
avant d'appliquer la migration de suppression (0021_remove_projectmember).
|
|
|
|
Usage :
|
|
python manage.py export_project_members
|
|
python manage.py export_project_members --output /chemin/vers/fichier.json
|
|
"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from django.db import connection
|
|
|
|
DEFAULT_OUTPUT = Path("project_members_export.json")
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Exporte les ProjectMember vers un fichier JSON avant suppression du modèle."
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
'--output', '-o',
|
|
type=Path,
|
|
default=DEFAULT_OUTPUT,
|
|
help=f"Chemin du fichier de sortie (défaut : {DEFAULT_OUTPUT})",
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
output: Path = options['output']
|
|
table_name = 'projects_projectmember'
|
|
|
|
if table_name not in connection.introspection.table_names():
|
|
raise CommandError(
|
|
f"La table '{table_name}' n'existe pas en base. "
|
|
"La migration de suppression a peut-être déjà été appliquée."
|
|
)
|
|
|
|
with connection.cursor() as cursor:
|
|
cursor.execute(
|
|
f"SELECT id, project_id, member_id, role FROM {table_name} ORDER BY id"
|
|
)
|
|
rows = cursor.fetchall()
|
|
columns = [col[0] for col in cursor.description]
|
|
|
|
data = [dict(zip(columns, row)) for row in rows]
|
|
|
|
output.write_text(json.dumps(data, indent=2), encoding='utf-8')
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f"{len(data)} enregistrement(s) exporté(s) vers '{output}'."
|
|
)
|
|
)
|