Skip to content

Commit

Permalink
new commands and options introduced with Alembic 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 1, 2014
1 parent 71ff1d7 commit d848cb4
Showing 1 changed file with 195 additions and 53 deletions.
248 changes: 195 additions & 53 deletions flask_migrate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import os
from flask import current_app
from flask.ext.script import Manager
from alembic import __version__ as __alembic_version__
from alembic.config import Config as AlembicConfig
from alembic import command


alembic_version = tuple([int(v) for v in __alembic_version__.split('.')])


class _MigrateConfig(object):
def __init__(self, db, directory):
self.db = db
Expand Down Expand Up @@ -45,9 +49,10 @@ def _get_config(directory):


@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="migration script directory (default is 'migrations')")
help=("migration script directory (default is "
"'migrations')"))
def init(directory=None):
"Generates a new migration"
"""Generates a new migration"""
if directory is None:
directory = current_app.extensions['migrate'].directory
config = Config()
Expand All @@ -56,93 +61,230 @@ def init(directory=None):
command.init(config, directory, 'flask')


@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
def current(directory=None):
"Display the current revision for each database."
config = _get_config(directory)
command.current(config)


@MigrateCommand.option('-r', '--rev-range', dest='rev_range', default=None,
help="Specify a revision range; format is [start]:[end]")
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
def history(directory=None, rev_range=None):
"List changeset scripts in chronological order."
config = _get_config(directory)
command.history(config, rev_range)


@MigrateCommand.option('--rev-id', dest='rev_id', default=None,
help=('Specify a hardcoded revision id instead of '
'generating one'))
@MigrateCommand.option('--version-path', dest='version_path', default=None,
help=('Specify specific path from config for version '
'file'))
@MigrateCommand.option('--branch-label', dest='branch_label', default=None,
help=('Specify a branch label to apply to the new '
'revision'))
@MigrateCommand.option('--splice', dest='splice', action='store_true',
default=False,
help=('Allow a non-head revision as the "head" to '
'splice onto'))
@MigrateCommand.option('--head', dest='head', default='head',
help=('Specify head revision or <branchname>@head to '
'base new revision on'))
@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False,
help="Don't emit SQL to database - dump to standard output instead")
help=("Don't emit SQL to database - dump to standard "
"output instead"))
@MigrateCommand.option('--autogenerate', dest='autogenerate',
action='store_true', default=False,
help="Populate revision script with andidate migration operatons, based on comparison of database to model")
help=('Populate revision script with andidate migration '
'operatons, based on comparison of database to '
'model'))
@MigrateCommand.option('-m', '--message', dest='message', default=None)
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
def revision(directory=None, message=None, autogenerate=False, sql=False):
"Create a new revision file."
help=("migration script directory (default is "
"'migrations')"))
def revision(directory=None, message=None, autogenerate=False, sql=False,
head='head', splice=False, branch_label=None, version_path=None,
rev_id=None):
"""Create a new revision file."""
config = _get_config(directory)
command.revision(config, message, autogenerate=autogenerate, sql=sql)
if alembic_version >= (0, 7, 0):
command.revision(config, message, autogenerate=autogenerate, sql=sql,
head=head, splice=splice, branch_label=branch_label,
version_path=version_path, rev_id=rev_id)
else:
command.revision(config, message, autogenerate=autogenerate, sql=sql)


@MigrateCommand.option('--rev-id', dest='rev_id', default=None,
help=('Specify a hardcoded revision id instead of '
'generating one'))
@MigrateCommand.option('--version-path', dest='version_path', default=None,
help=('Specify specific path from config for version '
'file'))
@MigrateCommand.option('--branch-label', dest='branch_label', default=None,
help=('Specify a branch label to apply to the new '
'revision'))
@MigrateCommand.option('--splice', dest='splice', action='store_true',
default=False,
help=('Allow a non-head revision as the "head" to '
'splice onto'))
@MigrateCommand.option('--head', dest='head', default='head',
help=('Specify head revision or <branchname>@head to '
'base new revision on'))
@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False,
help="Don't emit SQL to database - dump to standard output instead")
help=("Don't emit SQL to database - dump to standard "
"output instead"))
@MigrateCommand.option('-m', '--message', dest='message', default=None)
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
def migrate(directory=None, message=None, sql=False):
"Alias for 'revision --autogenerate'"
help=("migration script directory (default is "
"'migrations')"))
def migrate(directory=None, message=None, sql=False, head='head', splice=False,
branch_label=None, version_path=None, rev_id=None):
"""Alias for 'revision --autogenerate'"""
config = _get_config(directory)
command.revision(config, message, autogenerate=True, sql=sql)
if alembic_version >= (0, 7, 0):
command.revision(config, message, autogenerate=True, sql=sql, head=head,
splice=splice, branch_label=branch_label,
version_path=version_path, rev_id=rev_id)
else:
command.revision(config, message, autogenerate=True, sql=sql)


@MigrateCommand.option('--tag', dest='tag', default=None,
help="Arbitrary 'tag' name - can be used by custom env.py scripts")
@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False,
help="Don't emit SQL to database - dump to standard output instead")
@MigrateCommand.option('revision', default=None, help="revision identifier")
@MigrateCommand.option('--rev-id', dest='rev_id', default=None,
help=('Specify a hardcoded revision id instead of '
'generating one'))
@MigrateCommand.option('--branch-label', dest='branch_label', default=None,
help=('Specify a branch label to apply to the new '
'revision'))
@MigrateCommand.option('-m', '--message', dest='message', default=None)
@MigrateCommand.option('revisions',
help='one or more revisions, or "heads" for all heads')
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
def stamp(directory=None, revision='head', sql=False, tag=None):
"'stamp' the revision table with the given revision; don't run any migrations"
config = _get_config(directory)
command.stamp(config, revision, sql=sql, tag=tag)
help=("migration script directory (default is "
"'migrations')"))
def merge(directory=None, revisions='', message=None, branch_label=None,
rev_id=None):
"""Merge two revisions together. Creates a new migration file"""
if alembic_version >= (0, 7, 0):
config = _get_config(directory)
command.merge(config, revisions, message=message,
branch_label=branch_label, rev_id=rev_id)
else:
raise RuntimeError('Alembic 0.7.0 or greater is required')


@MigrateCommand.option('--tag', dest='tag', default=None,
help="Arbitrary 'tag' name - can be used by custom env.py scripts")
help=("Arbitrary 'tag' name - can be used by custom "
"env.py scripts"))
@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False,
help="Don't emit SQL to database - dump to standard output instead")
help=("Don't emit SQL to database - dump to standard "
"output instead"))
@MigrateCommand.option('revision', nargs='?', default='head',
help="revision identifier")
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
help=("migration script directory (default is "
"'migrations')"))
def upgrade(directory=None, revision='head', sql=False, tag=None):
"Upgrade to a later version"
"""Upgrade to a later version"""
config = _get_config(directory)
command.upgrade(config, revision, sql=sql, tag=tag)


@MigrateCommand.option('--tag', dest='tag', default=None,
help="Arbitrary 'tag' name - can be used by custom env.py scripts")
help=("Arbitrary 'tag' name - can be used by custom "
"env.py scripts"))
@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False,
help="Don't emit SQL to database - dump to standard output instead")
help=("Don't emit SQL to database - dump to standard "
"output instead"))
@MigrateCommand.option('revision', nargs='?', default="-1",
help="revision identifier")
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
help=("migration script directory (default is "
"'migrations')"))
def downgrade(directory=None, revision='-1', sql=False, tag=None):
"Revert to a previous version"
"""Revert to a previous version"""
config = _get_config(directory)
command.downgrade(config, revision, sql=sql, tag=tag)


@MigrateCommand.option('revision', nargs='?', default="head",
help="revision identifier")
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
def show(directory=None, revision='head'):
"""Show the revision denoted by the given symbol."""
if alembic_version >= (0, 7, 0):
config = _get_config(directory)
command.show(config, revision)
else:
raise RuntimeError('Alembic 0.7.0 or greater is required')


@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='Use more verbose output')
@MigrateCommand.option('-r', '--rev-range', dest='rev_range', default=None,
help='Specify a revision range; format is [start]:[end]')
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
def history(directory=None, rev_range=None, verbose=False):
"""List changeset scripts in chronological order."""
config = _get_config(directory)
if alembic_version >= (0, 7, 0):
command.history(config, rev_range, verbose=verbose)
else:
command.history(config, rev_range)


@MigrateCommand.option('--resolve-dependencies', dest='resolve_dependencies',
action='store_true', default=False,
help='Treat dependency versions as down revisions')
@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='Use more verbose output')
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
def heads(directory=None, verbose=False, resolve_dependencies=False):
"""Show current available heads in the script directory"""
if alembic_version >= (0, 7, 0):
config = _get_config(directory)
command.heads(config, verbose=verbose,
resolve_dependencies=resolve_dependencies)
else:
raise RuntimeError('Alembic 0.7.0 or greater is required')


@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='Use more verbose output')
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
def branches(directory=None, verbose=False):
"""Show current branch points"""
config = _get_config(directory)
if alembic_version >= (0, 7, 0):
command.branches(config, verbose=verbose)
else:
command.branches(config)


@MigrateCommand.option('--head-only', dest='head_only', action='store_true',
default=False,
help='Deprecated. Use --verbose for additional output')
@MigrateCommand.option('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='Use more verbose output')
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help="Migration script directory (default is 'migrations')")
def branches(directory=None):
"Lists revisions that have broken the source tree into two versions representing two independent sets of changes"
help=("migration script directory (default is "
"'migrations')"))
def current(directory=None, verbose=False, head_only=False):
"""Display the current revision for each database."""
config = _get_config(directory)
command.branches(config)
if alembic_version >= (0, 7, 0):
command.current(config, verbose=verbose, head_only=head_only)
else:
command.current(config)


@MigrateCommand.option('--tag', dest='tag', default=None,
help=("Arbitrary 'tag' name - can be used by custom "
"env.py scripts"))
@MigrateCommand.option('--sql', dest='sql', action='store_true', default=False,
help=("Don't emit SQL to database - dump to standard "
"output instead"))
@MigrateCommand.option('revision', default=None, help="revision identifier")
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
def stamp(directory=None, revision='head', sql=False, tag=None):
"""'stamp' the revision table with the given revision; don't run any
migrations"""
config = _get_config(directory)
command.stamp(config, revision, sql=sql, tag=tag)

0 comments on commit d848cb4

Please sign in to comment.