Skip to content

Commit

Permalink
added use-fake-zero flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aminiun committed Sep 9, 2023
1 parent 2b1578c commit 09eaef5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django-zeromigrations
version = 0.2.1
version = 0.3.0
description = Django reset migrations with `zeromigrations` command with backup feature
long_description = file:README.rst
url = https://github.com/aminiun/django-zeromigrations
Expand Down
24 changes: 16 additions & 8 deletions zero_migrations/management/commands/zeromigrations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import site
from typing import List, NoReturn
from functools import lru_cache

from django.apps import apps
from django.core.management import BaseCommand, call_command

from zero_migrations.utils import BackupDir, AppMigrationsDir
from zero_migrations.utils import BackupDir, AppMigrationsDir, Migration
from zero_migrations.utils.backup import MigrationsTableBackup, MigrationFilesBackup
from zero_migrations.utils.restore import MigrationFilesRestore, MigrationsTableRestore

Expand All @@ -25,8 +24,16 @@ def add_arguments(self, parser):
"--backup-path",
help="Backup path to save backup files in it.",
)
parser.add_argument(
"--use-fake-zero",
action="store_true",
required=False,
help="Use django --fake zero command for migration deletion from DB.",
)

def handle(self, *args, **options):
use_fake_zero = options.get("use-fake-zero")

choice = int(input(
self.style.WARNING(
"I suggest to make a backups from both your "
Expand All @@ -41,7 +48,7 @@ def handle(self, *args, **options):
if choice == self.RESTORE_LAST_BACKUP:
self.restore()
if choice == self.PROCEED:
self.zero_migrations()
self.zero_migrations(use_fake_zero=use_fake_zero)

def make_backup(self) -> NoReturn:
"""
Expand All @@ -63,7 +70,7 @@ def make_backup(self) -> NoReturn:
if proceed_perm.lower() == "y":
self.zero_migrations()

def zero_migrations(self):
def zero_migrations(self, use_fake_zero=None):
"""
Settings migrations zero.
This process includes of 4 steps:
Expand All @@ -77,7 +84,10 @@ def zero_migrations(self):
self.stdout.write(self.style.WARNING("Migrate zero each app:"))
for app in self.get_apps():
self.stdout.write(self.style.WARNING(f"App name: {app}"))
call_command("migrate", "--fake", app, "zero", force_color=True)
if use_fake_zero:
call_command("migrate", "--fake", app, "zero", force_color=True)
else:
Migration.objects.filter(app=app).delete()

self.stdout.write(self.style.WARNING("Removing migrations:"))
for app in self.get_apps():
Expand Down Expand Up @@ -147,8 +157,6 @@ def get_apps(self) -> List[str]:
set third-party packages migrations zero.
:return: List of user apps names.
"""
installed_app_path = site.getsitepackages()[0]
return [
app.name for app in apps.get_app_configs()
if not str(app.path).startswith(str(installed_app_path))
app.name.split(".")[-1] for app in apps.get_app_configs()
]
9 changes: 3 additions & 6 deletions zero_migrations/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import abc
import argparse
import json
import os
import sys
Expand Down Expand Up @@ -76,11 +75,9 @@ def __init__(self, *dir_names):
self._dir_names = dir_names

def _extract_backup_path(self) -> Path:
if len(sys.argv) == 4:
return Path(sys.argv[3]) / self.BACKUP_DIR_NAME

if len(sys.argv) == 3:
return Path(sys.argv[2].split("=")[1]) / self.BACKUP_DIR_NAME
for arg in sys.argv:
if arg.startswith("--backup-path"):
return Path(arg.split("=")[1]) / self.BACKUP_DIR_NAME

return self.app_dir_path / self.BACKUP_DIR_NAME

Expand Down

0 comments on commit 09eaef5

Please sign in to comment.