-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
119 lines (88 loc) · 3.51 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import argparse
def parse() -> argparse.ArgumentParser:
"""
Parse the terminal argument(s)
:return: ArgumentParser
"""
_ = argparse.ArgumentParser(description='Manage your DB migrations',
allow_abbrev=False)
_.add_argument("-v", "--version", action="version",
version="sqlraw version 1.0.10")
_.add_argument('-p', action='append', dest='collection', default=[],
help='print sql to terminal')
_.add_argument("-k", '--keyword', action='store', type=str,
help="checks to see if the supplied word is a keyword")
_.add_argument("-b", '--by_index', action='store', type=int,
help="print sql to terminal by number- start at 1")
_.add_argument("-y", '--format_by_index', action='store', type=int,
help="format sql and print to terminal"
" by number- start at 1")
_.add_argument("-n", "--by_number", action="store_true",
help="list all the migration files by number")
_.add_argument("-s", "--status", action="store_true",
help="list all migrations registered on the DB")
_.add_argument("-f", "--files", action="store_true",
help="list all the migration files")
_.add_argument("-r", "--regex", action="store", type=str,
help="search migration files with RegEx")
_.add_argument("-i", "--db_initialise", action="store_true",
help="create the migrations table")
_.add_argument("-m", "--db_migrate", action="store_true",
help="create sql file for DB")
_.add_argument("-u", "--db_upgrade", action="store_true",
help="run migration for DB")
_.add_argument("-d", '--db_downgrade', action='store', type=int,
help="downgrade the DB")
return _
def which_module():
"""
Decides on which module to run based on the scheme supplied
:return: Object
"""
import sqlraw.mysql
import sqlraw.postgresql
import sqlraw.sqlite
from sqlraw.psql_support import DB_URL
if DB_URL.scheme == 'postgres':
return sqlraw.postgresql
if DB_URL.scheme == 'mysql':
return sqlraw.mysql
if DB_URL.scheme == 'sqlite':
return sqlraw.sqlite
def calls():
"""
The decision box of this application
:return: None
"""
parser = parse()
arguments = parser.parse_args()
from sqlraw import keyword, by_index, format_by_index, display_sql, \
regex, migration_files, files_by_number
module = which_module()
if arguments.db_initialise:
getattr(module, 'db_initialise')()
if arguments.db_migrate:
getattr(module, 'db_migrate')()
if arguments.db_upgrade:
getattr(module, 'db_upgrade')()
if arguments.keyword:
print(keyword(arguments.keyword))
if arguments.db_downgrade:
getattr(module, 'db_downgrade')(arguments.db_downgrade)
if arguments.by_index:
print(by_index(arguments.by_index))
if arguments.format_by_index:
print(format_by_index(arguments.format_by_index))
if arguments.status:
print(getattr(module, 'status')())
if arguments.files:
print(migration_files())
if arguments.by_number:
print(files_by_number())
if arguments.regex:
print(regex(arguments.regex))
if arguments.collection:
for _ in arguments.collection:
print(display_sql(_))
if __name__ == '__main__':
calls()