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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# Copyright 2015 Oliver Cope
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import re
from yoyo import (read_migrations,
default_migration_table,
ancestors,
descendants,
)
from yoyo.scripts.main import InvalidArgument, get_backend
from yoyo import utils
def install_argparsers(global_parser, subparsers):
migration_parser = argparse.ArgumentParser(add_help=False)
migration_parser.add_argument('sources',
nargs="*",
help="Source directory of migration scripts")
migration_parser.add_argument("-d",
"--database",
default=None,
help="Database, eg 'sqlite:///path/to/sqlite.db' "
"or 'postgresql://user@host/db'")
migration_parser.add_argument("-m", "--match",
help="Select migrations matching PATTERN (regular expression)",
metavar='PATTERN')
migration_parser.add_argument("-a",
"--all",
dest="all",
action="store_true",
help="Select all migrations, regardless of whether "
"they have been previously applied")
migration_parser.add_argument("-f", "--force", dest="force", action="store_true",
help="Force apply/rollback of steps even if "
"previous steps have failed")
migration_parser.add_argument("-p", "--prompt-password", dest="prompt_password",
action="store_true",
help="Prompt for the database password")
migration_parser.add_argument("--migration-table", dest="migration_table",
action="store",
default=default_migration_table,
help="Name of table to use for storing "
"migration metadata")
migration_parser.add_argument('-r', '--revision',
help="Apply/rollback migration with id "
"REVISION",
metavar='REVISION')
parser_apply = subparsers.add_parser(
'apply',
help="Apply migrations",
parents=[global_parser, migration_parser])
parser_apply.set_defaults(func=apply, command_name='apply')
parser_rollback = subparsers.add_parser(
'rollback',
parents=[global_parser, migration_parser],
help="Rollback migrations")
parser_rollback.set_defaults(func=rollback, command_name='rollback')
parser_reapply = subparsers.add_parser(
'reapply',
parents=[global_parser, migration_parser],
help="Reapply migrations")
parser_reapply.set_defaults(func=reapply, command_name='reapply')
parser_mark = subparsers.add_parser(
'mark',
parents=[global_parser, migration_parser],
help="Mark migrations as applied, without running them")
parser_mark.set_defaults(func=mark, command_name='mark')
parser_unmark = subparsers.add_parser(
'unmark',
parents=[global_parser, migration_parser],
help="Unmark applied migrations, without rolling them back")
parser_unmark.set_defaults(func=unmark, command_name='unmark')
parser_break_lock = subparsers.add_parser(
'break_lock',
parents=[global_parser],
help="Break migration locks")
parser_break_lock.set_defaults(func=break_lock, command_name='break-lock')
def get_migrations(args, backend):
sources = args.sources
dburi = args.database
if not sources:
raise InvalidArgument("Please specify the migration source directory")
migrations = read_migrations(*sources)
if args.match:
migrations = migrations.filter(
lambda m: re.search(args.match, m.id) is not None)
if not args.all:
if args.func in {apply, mark}:
migrations = backend.to_apply(migrations)
elif args.func in {rollback, reapply, unmark}:
migrations = backend.to_rollback(migrations)
if args.revision:
targets = [m for m in migrations if args.revision in m.id]
if len(targets) == 0:
raise InvalidArgument("'{}' doesn't match any revisions."
.format(args.revision))
if len(targets) > 1:
raise InvalidArgument("'{}' matches multiple revisions. "
"Please specify one of {}.".format(
args.revision,
', '.join(m.id for m in targets)))
target = targets[0]
# apply: apply target an all its dependencies
if args.func in {mark, apply}:
deps = ancestors(target, migrations)
target_plus_deps = deps | {target}
migrations = migrations.filter(lambda m: m in target_plus_deps)
# rollback/reapply: rollback target and everything that depends on it
else:
deps = descendants(target, migrations)
target_plus_deps = deps | {target}
migrations = migrations.filter(lambda m: m in target_plus_deps)
if not args.batch_mode and not args.revision:
migrations = prompt_migrations(backend,
migrations,
args.command_name)
if not args.batch_mode and migrations:
prompt = '{} {} to {}'.format(
args.command_name.title(),
utils.plural(len(migrations), " %d migration", " %d migrations"),
dburi)
if not utils.confirm(prompt, default='y'):
return migrations.replace([])
return migrations
def apply(args, config):
backend = get_backend(args, config)
with backend.lock():
migrations = get_migrations(args, backend)
backend.apply_migrations(migrations, args.force)
def reapply(args, config):
backend = get_backend(args, config)
with backend.lock():
migrations = get_migrations(args, backend)
backend.rollback_migrations(migrations, args.force)
backend.apply_migrations(migrations, args.force)
def rollback(args, config):
backend = get_backend(args, config)
with backend.lock():
migrations = get_migrations(args, backend)
backend.rollback_migrations(migrations, args.force)
def mark(args, config):
backend = get_backend(args, config)
with backend.lock():
migrations = get_migrations(args, backend)
backend.mark_migrations(migrations)
def unmark(args, config):
backend = get_backend(args, config)
with backend.lock():
migrations = get_migrations(args, backend)
backend.unmark_migrations(migrations)
def break_lock(args, config):
backend = get_backend(args, config)
backend.break_lock()
def prompt_migrations(backend, migrations, direction):
"""
Iterate through the list of migrations and prompt the user to
apply/rollback each. Return a list of user selected migrations.
direction
one of 'apply' or 'rollback'
"""
class prompted_migration(object):
def __init__(self, migration, default=None):
super(prompted_migration, self).__init__()
self.migration = migration
self.choice = default
to_prompt = [prompted_migration(m) for m in migrations]
position = 0
while position < len(to_prompt):
mig = to_prompt[position]
choice = mig.choice
if choice is None:
is_applied = backend.is_applied(mig.migration)
if direction == 'apply':
choice = 'n' if is_applied else 'y'
else:
choice = 'y' if is_applied else 'n'
options = ''.join(o.upper() if o == choice else o.lower()
for o in 'ynvdaqjk?')
print("")
print('[%s]' % (mig.migration.id,))
response = utils.prompt("Shall I %s this migration?" % (direction,),
options)
if response == '?':
print("")
print("y: %s this migration" % (direction,))
print("n: don't %s it" % (direction,))
print("")
print("v: view this migration in full")
print("")
print("d: %s the selected migrations, skipping any remaining" %
(direction,))
print("a: %s all the remaining migrations" % (direction,))
print("q: cancel without making any changes")
print("")
print("j: skip to next migration")
print("k: back up to previous migration")
print("")
print("?: show this help")
continue
if response in 'yn':
mig.choice = response
position += 1
continue
if response == 'v':
print(mig.migration.source)
continue
if response == 'j':
position = min(len(to_prompt), position + 1)
continue
if response == 'k':
position = max(0, position - 1)
if response == 'd':
break
if response == 'a':
for mig in to_prompt[position:]:
mig.choice = 'y'
break
if response == 'q':
for mig in to_prompt:
mig.choice = 'n'
break
return migrations.replace(m.migration
for m in to_prompt
if m.choice == 'y')
|