"""
Drop a table that isn't supposed to be there from the database
"""

from django.core.management.base import BaseCommand
from django.db import connection, transaction


class Command(BaseCommand):
    help = __doc__

    def add_arguments(self, parser):
        parser.add_argument("tablename", help="The name of the table to drop")
        parser.add_argument(
            "--no-dry-run", action="store_true", help="Really commit to the database"
        )

    def handle(self, *args, **options):
        with transaction.atomic():
            with connection.cursor() as cursor:
                cursor.execute("DROP TABLE %(tablename)s;" % options)
                if not options["no_dry_run"]:
                    self.stdout.write("Rolling back because --no-dry-run wasn't set")
                    transaction.set_rollback(True)
                else:
                    self.stdout.write("Done")
