Python源码示例:alembic.command()
示例1
def attributes(self):
"""A Python dictionary for storage of additional state.
This is a utility dictionary which can include not just strings but
engines, connections, schema objects, or anything else.
Use this to pass objects into an env.py script, such as passing
a :class:`sqlalchemy.engine.base.Connection` when calling
commands from :mod:`alembic.command` programmatically.
.. versionadded:: 0.7.5
.. seealso::
:ref:`connection_sharing`
:paramref:`.Config.attributes`
"""
return {}
示例2
def main(self, argv, environ):
parser = ArgumentParser(
prog='temboard-migratedb',
description="temBoard schema migrator.",
argument_default=UNDEFINED_ARGUMENT,
)
define_arguments(parser)
args = parser.parse_args(argv)
self.bootstrap(args=args, environ=environ)
versions = inspect_versions()
logger.debug(
"Using Python %s (%s).",
versions['python'], versions['pythonbin'])
logger.debug(
"Using Psycopg2 %s, Alembic %s and SQLAlchemy %s",
versions['psycopg2'], versions['alembic'], versions['sqlalchemy'],
)
command_method = 'command_' + args.command
try:
getattr(self, command_method)(args)
except sqlalchemy.exc.OperationalError as e:
raise UserError("Failed to query Postgres server: %s." % e)
示例3
def define_arguments(parser):
define_core_arguments(parser)
parser.add_argument(
'-V', '--version',
action=VersionAction,
help='show version and exit',
)
sub = parser.add_subparsers(
metavar="COMMAND",
dest="command",
help="Operation to execute on temBoard database.")
sub.add_parser(
"check",
help='Check schema synchronisation status only.'
)
sub.add_parser(
"stamp",
help="Mark database as uptodate without migrating.",
)
sub.add_parser(
"upgrade",
help="Upgrade temBoard database to latest revision.",
)
示例4
def setup_schema(command, conf, vars):
"""Place any commands to setup depotexample here"""
# Load the models
# <websetup.websetup.schema.before.model.import>
from depotexample import model
# <websetup.websetup.schema.after.model.import>
# <websetup.websetup.schema.before.metadata.create_all>
print("Creating tables")
model.metadata.create_all(bind=config['tg.app_globals'].sa_engine)
# <websetup.websetup.schema.after.metadata.create_all>
transaction.commit()
print('Initializing Migrations')
import alembic.config, alembic.command
alembic_cfg = alembic.config.Config()
alembic_cfg.set_main_option("script_location", "migration")
alembic_cfg.set_main_option("sqlalchemy.url", config['sqlalchemy.url'])
alembic.command.stamp(alembic_cfg, "head")
示例5
def do_alembic_command(config, cmd, *args, **kwargs):
try:
getattr(alembic_cmd, cmd)(config, *args, **kwargs)
except alembic_u.CommandError as e:
alembic_u.err(str(e))
示例6
def add_alembic_subparser(sub, cmd):
return sub.add_parser(cmd, help=getattr(alembic_cmd, cmd).__doc__)
示例7
def add_command_parsers(subparsers):
for name in ['current', 'history', 'branches']:
parser = add_alembic_subparser(subparsers, name)
parser.set_defaults(func=do_alembic_command)
help_text = (getattr(alembic_cmd, 'branches').__doc__ +
' and validate head file')
parser = subparsers.add_parser('check_migration', help=help_text)
parser.set_defaults(func=do_check_migration)
parser = add_alembic_subparser(subparsers, 'upgrade')
parser.add_argument('--delta', type=int)
parser.add_argument('--sql', action='store_true')
parser.add_argument('revision', nargs='?')
parser.set_defaults(func=do_upgrade)
parser = subparsers.add_parser(
"upgrade_persistence",
help="Run migrations for persistence backend")
parser.set_defaults(func=do_persistence_upgrade)
parser = subparsers.add_parser('downgrade', help="(No longer supported)")
parser.add_argument('None', nargs='?', help="Downgrade not supported")
parser.set_defaults(func=no_downgrade)
parser = add_alembic_subparser(subparsers, 'stamp')
parser.add_argument('--sql', action='store_true')
parser.add_argument('revision')
parser.set_defaults(func=do_stamp)
parser = add_alembic_subparser(subparsers, 'revision')
parser.add_argument('-m', '--message')
parser.add_argument('--autogenerate', action='store_true')
parser.add_argument('--sql', action='store_true')
parser.set_defaults(func=do_revision)
示例8
def do_alembic_command(config, cmd, *args, **kwargs):
try:
getattr(alembic_command, cmd)(config, *args, **kwargs)
except alembic_util.CommandError as e:
alembic_util.err(str(e))
示例9
def command_stamp(self, args):
logging.getLogger('alembic').setLevel(logging.WARN)
alembic_cfg = build_alembic_config(self.config)
alembic.command.stamp(alembic_cfg, 'head')
logger.info("Database marked as up to date.")
示例10
def command_upgrade(self, args):
alembic_cfg = build_alembic_config(self.config)
alembic.command.upgrade(alembic_cfg, 'head')
logger.info("Database up to date.")
示例11
def add_alembic_command(subparsers, name):
return subparsers.add_parser(
name, help=getattr(alembic_command, name).__doc__)
示例12
def do_alembic_command(config, cmd, *args, **kwargs):
try:
getattr(alembic_command, cmd)(config, *args, **kwargs)
except alembic_util.CommandError as e:
alembic_util.err(str(e))
示例13
def do_alembic_command(config, cmd, *args, **kwargs):
try:
getattr(alembic_cmd, cmd)(config, *args, **kwargs)
except alembic_u.CommandError as e:
alembic_u.err(six.text_type(e))