From e7d7b80c09baa6dd7ba747f5223f29ea0473d1c9 Mon Sep 17 00:00:00 2001 From: nvbn Date: Thu, 21 May 2015 00:49:56 +0300 Subject: [PATCH] Add rule for django south ghost migrations --- README.md | 1 + tests/rules/test_django_south_ghost.py | 53 ++++++++++++++++++++++++++ thefuck/rules/django_south_ghost.py | 8 ++++ 3 files changed, 62 insertions(+) create mode 100644 tests/rules/test_django_south_ghost.py create mode 100644 thefuck/rules/django_south_ghost.py diff --git a/README.md b/README.md index 7b89f107..3e1e413f 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `cp_omitting_directory` – adds `-a` when you `cp` directory; * `cpp11` – add missing `-std=c++11` to `g++` or `clang++`; * `dry` – fix repetitions like "git git push"; +* `django_south_ghost` – adds `--delete-ghost-migrations` to failed because ghosts django south migration; * `fix_alt_space` – replaces Alt+Space with Space character; * `git_add` – fix *"Did you forget to 'git add'?"*; * `git_checkout` – creates the branch before checking-out; diff --git a/tests/rules/test_django_south_ghost.py b/tests/rules/test_django_south_ghost.py new file mode 100644 index 00000000..af87eef5 --- /dev/null +++ b/tests/rules/test_django_south_ghost.py @@ -0,0 +1,53 @@ +import pytest +from thefuck.rules.django_south_ghost import match, get_new_command +from tests.utils import Command + + +@pytest.fixture +def stderr(): + return '''Traceback (most recent call last): + File "/home/nvbn/work/.../bin/python", line 42, in + exec(compile(__file__f.read(), __file__, "exec")) + File "/home/nvbn/work/.../app/manage.py", line 34, in + execute_from_command_line(sys.argv) + File "/home/nvbn/work/.../lib/django/core/management/__init__.py", line 443, in execute_from_command_line + utility.execute() + File "/home/nvbn/work/.../lib/django/core/management/__init__.py", line 382, in execute + self.fetch_command(subcommand).run_from_argv(self.argv) + File "/home/nvbn/work/.../lib/django/core/management/base.py", line 196, in run_from_argv + self.execute(*args, **options.__dict__) + File "/home/nvbn/work/.../lib/django/core/management/base.py", line 232, in execute + output = self.handle(*args, **options) + File "/home/nvbn/work/.../app/lib/south/management/commands/migrate.py", line 108, in handle + ignore_ghosts = ignore_ghosts, + File "/home/nvbn/work/.../app/lib/south/migration/__init__.py", line 193, in migrate_app + applied_all = check_migration_histories(applied_all, delete_ghosts, ignore_ghosts) + File "/home/nvbn/work/.../app/lib/south/migration/__init__.py", line 88, in check_migration_histories + raise exceptions.GhostMigrations(ghosts) +south.exceptions.GhostMigrations: + + ! These migrations are in the database but not on disk: + + + + + + + + ! I'm not trusting myself; either fix this yourself by fiddling + ! with the south_migrationhistory table, or pass --delete-ghost-migrations + ! to South to have it delete ALL of these records (this may not be good). +''' + + +def test_match(stderr): + assert match(Command('./manage.py migrate', stderr=stderr), None) + assert match(Command('python manage.py migrate', stderr=stderr), None) + assert not match(Command('./manage.py migrate'), None) + assert not match(Command('app migrate', stderr=stderr), None) + assert not match(Command('./manage.py test', stderr=stderr), None) + + +def test_get_new_command(): + assert get_new_command(Command('./manage.py migrate auth'), None)\ + == './manage.py migrate auth --delete-ghost-migrations' diff --git a/thefuck/rules/django_south_ghost.py b/thefuck/rules/django_south_ghost.py new file mode 100644 index 00000000..d3290c4a --- /dev/null +++ b/thefuck/rules/django_south_ghost.py @@ -0,0 +1,8 @@ +def match(command, settings): + return 'manage.py' in command.script and \ + 'migrate' in command.script \ + and 'or pass --delete-ghost-migrations' in command.stderr + + +def get_new_command(command, settings): + return u'{} --delete-ghost-migrations'.format(command.script)