-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (169 loc) · 5.66 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from os import getcwd
from os.path import join
from sys import argv
from argparse import ArgumentParser, Namespace
from migration import execute_sql, get_migration
commandHelpMap={
'diff': 'Print DDL statements that should be executed to reach the desired schema.',
'apply': 'Generate DDL statements and execute them after confirmation.',
'save': 'Save DDL statements into a file.',
'execute': 'Execute SQL statements from a file.',
}
class color:
BLUE = '\033[94m'
GREEN = '\033[92m'
RED = '\033[91m'
END = '\033[0m'
class DsmToolCli:
def __init__(self) -> None:
usage='dsm <command> [<args>]\n\nAvailable commands:\n'
for command in commandHelpMap:
usage += ' %-10s' '%s\n' % (command, commandHelpMap[command])
parser = ArgumentParser(
description='Declarative schema management tool.',
usage=usage,
)
parser.add_argument('command', help='Subcommand to run.')
args = parser.parse_args(argv[1:2])
if args.command not in commandHelpMap or not hasattr(self, args.command):
print('Incorrect command.')
parser.print_help()
exit(1)
getattr(self, args.command)()
def diff(self) -> None:
parser = ArgumentParser(description=commandHelpMap['diff'])
args = self.parse_diff_arguments(parser)
statements = get_migration(args)
if statements:
print(statements)
def apply(self) -> None:
parser = ArgumentParser(description=commandHelpMap['apply'])
parser.add_argument(
'--no-confirmation',
dest='confirmation',
action='store_false',
help='Execute the generated DDL statements without confirmation.',
)
args = self.parse_diff_arguments(parser)
statements = get_migration(args)
if statements:
if args.confirmation:
print('%sPending changes:%s' % (color.BLUE, color.END), end='\n\n')
print(statements, end='\n\n')
valid = {'y': True, 'n': False}
while True:
print('%sDo you want to execute them? [y/n] %s' % (color.BLUE, color.END), end='')
choice = input().lower()
if choice in valid:
if valid[choice]:
execute_sql(args.uri, statements)
print('%sDone.%s' % (color.GREEN, color.END))
exit(0)
else:
print('%sNot confirmed.%s' % (color.RED, color.END))
exit(1)
else:
print('Please respond with "y" or "n".')
else:
execute_sql(args.uri, statements)
print('%sDone.%s' % (color.GREEN, color.END))
else:
self.print_no_changes()
def save(self) -> None:
parser = ArgumentParser(description=commandHelpMap['save'])
parser.add_argument(
'--file',
dest='file',
default='/schema/pending.sql',
help='The path to the file where the generated DDL statements will be stored.',
)
args = self.parse_diff_arguments(parser)
statements = get_migration(args)
if statements:
file_path = join(getcwd(), args.file)
with open(file_path, 'w') as f:
f.write(statements)
print('%sSaved the file "%s" with the following changes:%s' % (color.GREEN, file_path, color.END), end='\n\n')
print(statements)
else:
self.print_no_changes()
def execute(self) -> None:
parser = ArgumentParser(description=commandHelpMap['execute'])
parser.add_argument(
'--uri',
dest='uri',
default=None,
required=True,
help='The connection URI to the target database.',
)
parser.add_argument(
'--file',
dest='file',
default='/schema/pending.sql',
help='The path to the file with SQL statements that should be executed to reach the desired schema.',
)
args = parser.parse_args(argv[2:])
file_path = join(getcwd(), args.file)
with open(file_path, 'r') as f:
statements = f.read().strip()
execute_sql(args.uri, statements)
print('%sThe following changes were made:%s' % (color.GREEN, color.END), end='\n\n')
print(statements)
def parse_diff_arguments(self, parser: ArgumentParser) -> Namespace:
parser.add_argument(
'--uri',
dest='uri',
default=None,
required=True,
help='The connection URI to the target database.',
)
parser.add_argument(
'--temp-uri',
dest='temp_uri',
default=None,
required=True,
help='The connection URI to the temporary database (must be empty).',
)
parser.add_argument(
'--to',
dest='to',
default=None,
required=True,
action='append',
help='The path to a file or directory with multiple files with your schema (e.g. one file is one table).',
)
parser.add_argument(
'--unsafe',
dest='unsafe',
action='store_true',
help="Don't throw an exception if DROP statements are generated.",
)
parser.add_argument(
'--schema',
dest='schema',
default=None,
help='Generate DDL statements only for the specified schema.',
)
parser.add_argument(
'--exclude-schema',
dest='exclude_schema',
default=None,
help='Generate DDL statements for all schemas except the specified one.',
)
parser.add_argument(
'--ignore-extension-versions',
dest='ignore_extension_versions',
action='store_true',
help='Ignore versions when comparing extensions.',
)
parser.add_argument(
'--with-privileges',
dest='with_privileges',
action='store_true',
help='Include permission-related change statements (GRANT, REVOKE).',
)
return parser.parse_args(argv[2:])
def print_no_changes(self) -> None:
print('%sNo changes.%s' % (color.GREEN, color.END))
if __name__ == '__main__':
DsmToolCli()