"""
Management command: اجرای یک‌بار rename اپ label از soil_data به location_data در DB.
این دستور را یک بار قبل از اجرای migrate اجرا کنید:
  python manage.py rename_soil_data_label
  python manage.py migrate
"""
from django.core.management.base import BaseCommand
from django.db import connection


class Command(BaseCommand):
    help = "Rename app label from soil_data to location_data in django_migrations and django_content_type"

    def handle(self, *args, **options):
        with connection.cursor() as cursor:
            cursor.execute(
                "UPDATE django_migrations SET app = %s WHERE app = %s",
                ["location_data", "soil_data"],
            )
            migrations_updated = cursor.rowcount
            cursor.execute(
                "UPDATE django_content_type SET app_label = %s WHERE app_label = %s",
                ["location_data", "soil_data"],
            )
            content_types_updated = cursor.rowcount

        self.stdout.write(
            self.style.SUCCESS(
                f"Done. django_migrations rows updated: {migrations_updated}, "
                f"django_content_type rows updated: {content_types_updated}"
            )
        )
