|
7 | 7 | # Usage: |
8 | 8 | # |
9 | 9 | # $ python3 manage.py --help |
10 | | -# usage: manage.py [-h] {shell,db,runserver} ... |
11 | | -# |
12 | | -# positional arguments: |
13 | | -# {shell,db,runserver} |
14 | | -# shell Runs a Python shell inside Flask application context. |
15 | | -# db Perform database migrations |
16 | | -# runserver Runs the Flask development server i.e. app.run() |
| 10 | +# usage: manage.py action |
17 | 11 | # |
18 | 12 | # optional arguments: |
19 | 13 | # -h, --help show this help message and exit |
20 | 14 | # |
21 | 15 | # For DB management: |
22 | | -# $ python3 manage.py db --help |
| 16 | +# $ python manage.py upgrade |
23 | 17 | # usage: Perform database migrations |
24 | 18 | # |
25 | | -# positional arguments: |
| 19 | +# action arguments: |
26 | 20 | # {upgrade,migrate,current,stamp,init,downgrade,history,revision} |
27 | 21 | # upgrade Upgrade to a later version |
28 | 22 | # migrate Alias for 'revision --autogenerate' |
|
37 | 31 | # optional arguments: |
38 | 32 | # -h, --help show this help message and exit |
39 | 33 |
|
| 34 | +import sys |
| 35 | +import os |
40 | 36 | from flask_migrate import (Migrate, upgrade, downgrade, current, |
41 | 37 | migrate, history, revision) |
42 | 38 | from init import App |
43 | | -import sys |
44 | 39 |
|
45 | 40 | DB = App.get_db() |
46 | 41 | APP = App.get_app() |
47 | | - |
48 | | -Migrate(APP, DB) |
| 42 | +workdir_path = os.path.dirname(__file__) |
| 43 | +migrations_path = os.path.join(workdir_path, 'migrations') |
| 44 | +Migrate(APP, DB, directory=migrations_path) |
| 45 | +ACTIONS = ['current', 'upgrade', 'downgrade', 'migrate', 'history', 'revision'] |
49 | 46 |
|
50 | 47 | if __name__ == '__main__': |
51 | | - action = '' |
52 | | - if len(sys.argv) > 1: |
53 | | - action = sys.argv[1] |
| 48 | + if len(sys.argv) < 1 or sys.argv[1] not in ACTIONS: |
| 49 | + print(f'Invalid action, valid values: {ACTIONS}') |
| 50 | + sys.exit(1) |
| 51 | + |
| 52 | + # Valid action name |
| 53 | + action = sys.argv[1] |
| 54 | + |
| 55 | + os.chdir(workdir_path) |
54 | 56 |
|
55 | 57 | with APP.app_context(): |
56 | 58 | if action == 'current': |
|
0 commit comments