Skip to content

Instantly share code, notes, and snippets.

@Farhaduneci
Created August 2, 2023 05:33
Show Gist options
  • Save Farhaduneci/79858b3447ce913c8b5b6e3e8a0dc03d to your computer and use it in GitHub Desktop.
Save Farhaduneci/79858b3447ce913c8b5b6e3e8a0dc03d to your computer and use it in GitHub Desktop.
A custom Django management command to clean up (reset/remvoe) migration files, excluding `__init__.py` files, within the apps. This command is intended for use in development environments only and should not be used in production.
import subprocess
from django.core.management.base import BaseCommand
from django.conf import settings
class Command(BaseCommand):
help = "Deletes migration files, excluding __init__.py, from apps"
def handle(self, *args, **options):
base_dir = settings.BASE_DIR
apps_dir = base_dir / "apps"
find_command = [
"find",
apps_dir,
"-path",
"*/migrations/*.py",
"-not",
"-name",
"__init__.py",
"-delete",
]
try:
subprocess.run(find_command, check=True)
self.stdout.write(self.style.SUCCESS("Successfully deleted migration files."))
except subprocess.CalledProcessError as e:
self.stderr.write(self.style.ERROR(f"Error deleting migration files: {e}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment