cli.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import click
  2. from flask import g
  3. from flask.cli import with_appcontext
  4. from flask_migrate import list_templates as _list_templates
  5. from flask_migrate import init as _init
  6. from flask_migrate import revision as _revision
  7. from flask_migrate import migrate as _migrate
  8. from flask_migrate import edit as _edit
  9. from flask_migrate import merge as _merge
  10. from flask_migrate import upgrade as _upgrade
  11. from flask_migrate import downgrade as _downgrade
  12. from flask_migrate import show as _show
  13. from flask_migrate import history as _history
  14. from flask_migrate import heads as _heads
  15. from flask_migrate import branches as _branches
  16. from flask_migrate import current as _current
  17. from flask_migrate import stamp as _stamp
  18. from flask_migrate import check as _check
  19. @click.group()
  20. @click.option('-d', '--directory', default=None,
  21. help=('Migration script directory (default is "migrations")'))
  22. @click.option('-x', '--x-arg', multiple=True,
  23. help='Additional arguments consumed by custom env.py scripts')
  24. @with_appcontext
  25. def db(directory, x_arg):
  26. """Perform database migrations."""
  27. g.directory = directory
  28. g.x_arg = x_arg # these will be picked up by Migrate.get_config()
  29. @db.command()
  30. @with_appcontext
  31. def list_templates():
  32. """List available templates."""
  33. _list_templates()
  34. @db.command()
  35. @click.option('-d', '--directory', default=None,
  36. help=('Migration script directory (default is "migrations")'))
  37. @click.option('--multidb', is_flag=True,
  38. help=('Support multiple databases'))
  39. @click.option('-t', '--template', default=None,
  40. help=('Repository template to use (default is "flask")'))
  41. @click.option('--package', is_flag=True,
  42. help=('Write empty __init__.py files to the environment and '
  43. 'version locations'))
  44. @with_appcontext
  45. def init(directory, multidb, template, package):
  46. """Creates a new migration repository."""
  47. _init(directory or g.directory, multidb, template, package)
  48. @db.command()
  49. @click.option('-d', '--directory', default=None,
  50. help=('Migration script directory (default is "migrations")'))
  51. @click.option('-m', '--message', default=None, help='Revision message')
  52. @click.option('--autogenerate', is_flag=True,
  53. help=('Populate revision script with candidate migration '
  54. 'operations, based on comparison of database to model'))
  55. @click.option('--sql', is_flag=True,
  56. help=('Don\'t emit SQL to database - dump to standard output '
  57. 'instead'))
  58. @click.option('--head', default='head',
  59. help=('Specify head revision or <branchname>@head to base new '
  60. 'revision on'))
  61. @click.option('--splice', is_flag=True,
  62. help=('Allow a non-head revision as the "head" to splice onto'))
  63. @click.option('--branch-label', default=None,
  64. help=('Specify a branch label to apply to the new revision'))
  65. @click.option('--version-path', default=None,
  66. help=('Specify specific path from config for version file'))
  67. @click.option('--rev-id', default=None,
  68. help=('Specify a hardcoded revision id instead of generating '
  69. 'one'))
  70. @with_appcontext
  71. def revision(directory, message, autogenerate, sql, head, splice, branch_label,
  72. version_path, rev_id):
  73. """Create a new revision file."""
  74. _revision(directory or g.directory, message, autogenerate, sql, head,
  75. splice, branch_label, version_path, rev_id)
  76. @db.command()
  77. @click.option('-d', '--directory', default=None,
  78. help=('Migration script directory (default is "migrations")'))
  79. @click.option('-m', '--message', default=None, help='Revision message')
  80. @click.option('--sql', is_flag=True,
  81. help=('Don\'t emit SQL to database - dump to standard output '
  82. 'instead'))
  83. @click.option('--head', default='head',
  84. help=('Specify head revision or <branchname>@head to base new '
  85. 'revision on'))
  86. @click.option('--splice', is_flag=True,
  87. help=('Allow a non-head revision as the "head" to splice onto'))
  88. @click.option('--branch-label', default=None,
  89. help=('Specify a branch label to apply to the new revision'))
  90. @click.option('--version-path', default=None,
  91. help=('Specify specific path from config for version file'))
  92. @click.option('--rev-id', default=None,
  93. help=('Specify a hardcoded revision id instead of generating '
  94. 'one'))
  95. @click.option('-x', '--x-arg', multiple=True,
  96. help='Additional arguments consumed by custom env.py scripts')
  97. @with_appcontext
  98. def migrate(directory, message, sql, head, splice, branch_label, version_path,
  99. rev_id, x_arg):
  100. """Autogenerate a new revision file (Alias for
  101. 'revision --autogenerate')"""
  102. _migrate(directory or g.directory, message, sql, head, splice,
  103. branch_label, version_path, rev_id, x_arg or g.x_arg)
  104. @db.command()
  105. @click.option('-d', '--directory', default=None,
  106. help=('Migration script directory (default is "migrations")'))
  107. @click.argument('revision', default='head')
  108. @with_appcontext
  109. def edit(directory, revision):
  110. """Edit a revision file"""
  111. _edit(directory or g.directory, revision)
  112. @db.command()
  113. @click.option('-d', '--directory', default=None,
  114. help=('Migration script directory (default is "migrations")'))
  115. @click.option('-m', '--message', default=None, help='Merge revision message')
  116. @click.option('--branch-label', default=None,
  117. help=('Specify a branch label to apply to the new revision'))
  118. @click.option('--rev-id', default=None,
  119. help=('Specify a hardcoded revision id instead of generating '
  120. 'one'))
  121. @click.argument('revisions', nargs=-1)
  122. @with_appcontext
  123. def merge(directory, message, branch_label, rev_id, revisions):
  124. """Merge two revisions together, creating a new revision file"""
  125. _merge(directory or g.directory, revisions, message, branch_label, rev_id)
  126. @db.command()
  127. @click.option('-d', '--directory', default=None,
  128. help=('Migration script directory (default is "migrations")'))
  129. @click.option('--sql', is_flag=True,
  130. help=('Don\'t emit SQL to database - dump to standard output '
  131. 'instead'))
  132. @click.option('--tag', default=None,
  133. help=('Arbitrary "tag" name - can be used by custom env.py '
  134. 'scripts'))
  135. @click.option('-x', '--x-arg', multiple=True,
  136. help='Additional arguments consumed by custom env.py scripts')
  137. @click.argument('revision', default='head')
  138. @with_appcontext
  139. def upgrade(directory, sql, tag, x_arg, revision):
  140. """Upgrade to a later version"""
  141. _upgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)
  142. @db.command()
  143. @click.option('-d', '--directory', default=None,
  144. help=('Migration script directory (default is "migrations")'))
  145. @click.option('--sql', is_flag=True,
  146. help=('Don\'t emit SQL to database - dump to standard output '
  147. 'instead'))
  148. @click.option('--tag', default=None,
  149. help=('Arbitrary "tag" name - can be used by custom env.py '
  150. 'scripts'))
  151. @click.option('-x', '--x-arg', multiple=True,
  152. help='Additional arguments consumed by custom env.py scripts')
  153. @click.argument('revision', default='-1')
  154. @with_appcontext
  155. def downgrade(directory, sql, tag, x_arg, revision):
  156. """Revert to a previous version"""
  157. _downgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)
  158. @db.command()
  159. @click.option('-d', '--directory', default=None,
  160. help=('Migration script directory (default is "migrations")'))
  161. @click.argument('revision', default='head')
  162. @with_appcontext
  163. def show(directory, revision):
  164. """Show the revision denoted by the given symbol."""
  165. _show(directory or g.directory, revision)
  166. @db.command()
  167. @click.option('-d', '--directory', default=None,
  168. help=('Migration script directory (default is "migrations")'))
  169. @click.option('-r', '--rev-range', default=None,
  170. help='Specify a revision range; format is [start]:[end]')
  171. @click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
  172. @click.option('-i', '--indicate-current', is_flag=True,
  173. help=('Indicate current version (Alembic 0.9.9 or greater is '
  174. 'required)'))
  175. @with_appcontext
  176. def history(directory, rev_range, verbose, indicate_current):
  177. """List changeset scripts in chronological order."""
  178. _history(directory or g.directory, rev_range, verbose, indicate_current)
  179. @db.command()
  180. @click.option('-d', '--directory', default=None,
  181. help=('Migration script directory (default is "migrations")'))
  182. @click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
  183. @click.option('--resolve-dependencies', is_flag=True,
  184. help='Treat dependency versions as down revisions')
  185. @with_appcontext
  186. def heads(directory, verbose, resolve_dependencies):
  187. """Show current available heads in the script directory"""
  188. _heads(directory or g.directory, verbose, resolve_dependencies)
  189. @db.command()
  190. @click.option('-d', '--directory', default=None,
  191. help=('Migration script directory (default is "migrations")'))
  192. @click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
  193. @with_appcontext
  194. def branches(directory, verbose):
  195. """Show current branch points"""
  196. _branches(directory or g.directory, verbose)
  197. @db.command()
  198. @click.option('-d', '--directory', default=None,
  199. help=('Migration script directory (default is "migrations")'))
  200. @click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
  201. @with_appcontext
  202. def current(directory, verbose):
  203. """Display the current revision for each database."""
  204. _current(directory or g.directory, verbose)
  205. @db.command()
  206. @click.option('-d', '--directory', default=None,
  207. help=('Migration script directory (default is "migrations")'))
  208. @click.option('--sql', is_flag=True,
  209. help=('Don\'t emit SQL to database - dump to standard output '
  210. 'instead'))
  211. @click.option('--tag', default=None,
  212. help=('Arbitrary "tag" name - can be used by custom env.py '
  213. 'scripts'))
  214. @click.option('--purge', is_flag=True,
  215. help=('Delete the version in the alembic_version table before '
  216. 'stamping'))
  217. @click.argument('revision', default='head')
  218. @with_appcontext
  219. def stamp(directory, sql, tag, revision, purge):
  220. """'stamp' the revision table with the given revision; don't run any
  221. migrations"""
  222. _stamp(directory or g.directory, revision, sql, tag, purge)
  223. @db.command()
  224. @click.option('-d', '--directory', default=None,
  225. help=('Migration script directory (default is "migrations")'))
  226. @with_appcontext
  227. def check(directory):
  228. """Check if there are any new operations to migrate"""
  229. _check(directory or g.directory)