Skip to content

Commit 0df07b2

Browse files
committed
add path argument for both commands
1 parent 2e0277a commit 0df07b2

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Created by .ignore support plugin (hsz.mobi)
22
.idea
33
manage.py
4-
django-auto-rollback
4+
django-rollback
55
__pycache__
66
db.sqlite3

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ This package allow to easy manage apps migrations based on GIT repository (it us
33
So you can return to any previous state that is saved in DB by one command.
44

55
## Version
6-
Current version is `0.2.0`
6+
Current version is `0.2.1`
77

88
It works with:
99
- django == 1.11
@@ -14,7 +14,7 @@ It works with:
1414

1515
First you need to install package with pip:
1616
```bash
17-
pip install git+https://github.com/freenoth/[email protected].0
17+
pip install git+https://github.com/freenoth/[email protected].1
1818
```
1919

2020
Then install it to your `INSTALLED_APPS`
@@ -28,12 +28,23 @@ INSTALLED_APPS = [
2828
You are also should run `./manage.py migrate` before using additional management commands.
2929

3030
## Using
31-
There are two commands to manage migrations state:
31+
There are two commands to manage migrations state.
32+
33+
For both commands you can pass the `PATH` argument to specify path to git repository directory (local). Default path is current dir : `'.'`.
3234

3335
### Saving current state
3436
```bash
3537
./manage.py save_migrations_state
3638
```
39+
Help message below:
40+
```bash
41+
usage: manage.py save_migrations_state [-p PATH]
42+
43+
Save migrations state for current commit.
44+
45+
optional arguments:
46+
-p PATH, --path PATH Git repository path.
47+
```
3748
This command used to save apps migrations state of current commit to DB (it create new or update existing state).
3849

3950
Successful output example below:
@@ -48,7 +59,7 @@ Successfully created for commit "84e47461a95fa325d9e933bbe8cca8c52bbea203".
4859
```
4960
Help message below:
5061
```bash
51-
usage: manage.py rollback_migrations [-t TAG] [-c COMMIT] [--fake]
62+
usage: manage.py rollback_migrations [-t TAG] [-c COMMIT] [-p PATH] [--fake]
5263

5364
Rollback migrations state of all django apps to chosen tag or commit if
5465
previously saved.
@@ -57,6 +68,7 @@ optional arguments:
5768
-t TAG, --tag TAG Git tag to which to rollback migrations.
5869
-c COMMIT, --commit COMMIT
5970
Git commit hash to which to rollback migrations.
71+
-p PATH, --path PATH Git repository path.
6072
--fake It allow to only print info about processed actions
6173
without execution (no changes for DB).
6274

django_rollback/management/commands/rollback_migrations.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from django_rollback.models import AppsState
99

10+
DEFAULT_REPO_PATH = '.'
1011
COMMIT_MAX_LENGTH = 40
1112
MIGRATE_COMMAND = 'migrate'
1213

@@ -19,13 +20,16 @@ class Command(BaseCommand):
1920
def add_arguments(self, parser):
2021
parser.add_argument('-t', '--tag', type=str, help='Git tag to which to rollback migrations.')
2122
parser.add_argument('-c', '--commit', type=str, help='Git commit hash to which to rollback migrations.')
23+
parser.add_argument('-p', '--path', type=str, help='Git repository path.')
2224
parser.add_argument('--fake', action='store_true',
2325
help='It allow to only print info about processed actions without execution '
2426
'(no changes for DB).')
2527

2628
def handle(self, *args, **options):
27-
other_commit = self.get_commit_from_options(options)
28-
current_commit = self.get_current_commit()
29+
repo_path = options.get('path', DEFAULT_REPO_PATH)
30+
31+
other_commit = self.get_commit_from_options(options, path=repo_path)
32+
current_commit = self.get_current_commit(path=repo_path)
2933

3034
current_data = self.get_migrations_data_from_commit(current_commit)
3135
other_data = self.get_migrations_data_from_commit(other_commit)
@@ -36,7 +40,7 @@ def handle(self, *args, **options):
3640

3741
self.stdout.write(self.style.SUCCESS('Rollback successfully finished.'))
3842

39-
def get_commit_from_options(self, options):
43+
def get_commit_from_options(self, options, path):
4044
if not options['tag'] and not options['commit']:
4145
raise CommandError('Tag or commit should be described by -t or -c arguments.')
4246

@@ -48,7 +52,7 @@ def get_commit_from_options(self, options):
4852

4953
tag = options['tag']
5054
try:
51-
repo = git.Repo('.')
55+
repo = git.Repo(path)
5256
if tag not in repo.tags:
5357
raise CommandError(f'Can not find tag `{tag}` in git repository.')
5458

@@ -61,9 +65,9 @@ def get_commit_from_options(self, options):
6165
self.stdout.write(self.style.ERROR(f'WARNING: an error occurred while working with git repo!'))
6266
raise CommandError(err)
6367

64-
def get_current_commit(self):
68+
def get_current_commit(self, path):
6569
try:
66-
repo = git.Repo('.')
70+
repo = git.Repo(path)
6771
return repo.head.commit.hexsha
6872

6973
except ValueError as err:

django_rollback/management/commands/save_migrations_state.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
from django_rollback.models import AppsState
88
from django_rollback.sql import MIGRATIONS_STATE_SQL
99

10+
DEFAULT_REPO_PATH = '.'
11+
1012

1113
class Command(BaseCommand):
1214
help = 'Save migrations state for current commit.'
1315

16+
def add_arguments(self, parser):
17+
parser.add_argument('-p', '--path', type=str, help='Git repository path.')
18+
1419
def handle(self, *args, **options):
15-
commit = self.get_current_commit()
20+
commit = self.get_current_commit(path=options.get('path', DEFAULT_REPO_PATH))
1621
state_data = self.get_current_migrations_state()
1722

1823
obj, created = AppsState.objects.update_or_create(commit=commit,
@@ -22,9 +27,9 @@ def handle(self, *args, **options):
2227
f'Successfully {"created" if created else "updated"} '
2328
f'for commit "{commit}".\n'))
2429

25-
def get_current_commit(self):
30+
def get_current_commit(self, path):
2631
try:
27-
repo = git.Repo('.')
32+
repo = git.Repo(path)
2833
return repo.head.commit.hexsha
2934
except ValueError as err:
3035
self.stdout.write(self.style.ERROR(f'WARNING: an error occurred while working with git repo!'))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='django_rollback',
8-
version='0.2.0',
8+
version='0.2.1',
99
author='Aleksey Yakovlev',
1010
author_email='[email protected]',
1111
description='Package to automate django rollback',

0 commit comments

Comments
 (0)