Initial commit
This commit is contained in:
266
venv/Lib/site-packages/flask_migrate/__init__.py
Normal file
266
venv/Lib/site-packages/flask_migrate/__init__.py
Normal file
@@ -0,0 +1,266 @@
|
||||
import argparse
|
||||
from functools import wraps
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from flask import current_app, g
|
||||
from alembic import __version__ as __alembic_version__
|
||||
from alembic.config import Config as AlembicConfig
|
||||
from alembic import command
|
||||
from alembic.util import CommandError
|
||||
|
||||
alembic_version = tuple([int(v) for v in __alembic_version__.split('.')[0:3]])
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class _MigrateConfig(object):
|
||||
def __init__(self, migrate, db, **kwargs):
|
||||
self.migrate = migrate
|
||||
self.db = db
|
||||
self.directory = migrate.directory
|
||||
self.configure_args = kwargs
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
"""
|
||||
Backwards compatibility, in old releases app.extensions['migrate']
|
||||
was set to db, and env.py accessed app.extensions['migrate'].metadata
|
||||
"""
|
||||
return self.db.metadata
|
||||
|
||||
|
||||
class Config(AlembicConfig):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.template_directory = kwargs.pop('template_directory', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_template_directory(self):
|
||||
if self.template_directory:
|
||||
return self.template_directory
|
||||
package_dir = os.path.abspath(os.path.dirname(__file__))
|
||||
return os.path.join(package_dir, 'templates')
|
||||
|
||||
|
||||
class Migrate(object):
|
||||
def __init__(self, app=None, db=None, directory='migrations', command='db',
|
||||
compare_type=True, render_as_batch=True, **kwargs):
|
||||
self.configure_callbacks = []
|
||||
self.db = db
|
||||
self.command = command
|
||||
self.directory = str(directory)
|
||||
self.alembic_ctx_kwargs = kwargs
|
||||
self.alembic_ctx_kwargs['compare_type'] = compare_type
|
||||
self.alembic_ctx_kwargs['render_as_batch'] = render_as_batch
|
||||
if app is not None and db is not None:
|
||||
self.init_app(app, db, directory)
|
||||
|
||||
def init_app(self, app, db=None, directory=None, command=None,
|
||||
compare_type=None, render_as_batch=None, **kwargs):
|
||||
self.db = db or self.db
|
||||
self.command = command or self.command
|
||||
self.directory = str(directory or self.directory)
|
||||
self.alembic_ctx_kwargs.update(kwargs)
|
||||
if compare_type is not None:
|
||||
self.alembic_ctx_kwargs['compare_type'] = compare_type
|
||||
if render_as_batch is not None:
|
||||
self.alembic_ctx_kwargs['render_as_batch'] = render_as_batch
|
||||
if not hasattr(app, 'extensions'):
|
||||
app.extensions = {}
|
||||
app.extensions['migrate'] = _MigrateConfig(
|
||||
self, self.db, **self.alembic_ctx_kwargs)
|
||||
|
||||
from flask_migrate.cli import db as db_cli_group
|
||||
app.cli.add_command(db_cli_group, name=self.command)
|
||||
|
||||
def configure(self, f):
|
||||
self.configure_callbacks.append(f)
|
||||
return f
|
||||
|
||||
def call_configure_callbacks(self, config):
|
||||
for f in self.configure_callbacks:
|
||||
config = f(config)
|
||||
return config
|
||||
|
||||
def get_config(self, directory=None, x_arg=None, opts=None):
|
||||
if directory is None:
|
||||
directory = self.directory
|
||||
directory = str(directory)
|
||||
config = Config(os.path.join(directory, 'alembic.ini'))
|
||||
config.set_main_option('script_location', directory)
|
||||
if config.cmd_opts is None:
|
||||
config.cmd_opts = argparse.Namespace()
|
||||
for opt in opts or []:
|
||||
setattr(config.cmd_opts, opt, True)
|
||||
if not hasattr(config.cmd_opts, 'x'):
|
||||
setattr(config.cmd_opts, 'x', [])
|
||||
for x in getattr(g, 'x_arg', []):
|
||||
config.cmd_opts.x.append(x)
|
||||
if x_arg is not None:
|
||||
if isinstance(x_arg, list) or isinstance(x_arg, tuple):
|
||||
for x in x_arg:
|
||||
config.cmd_opts.x.append(x)
|
||||
else:
|
||||
config.cmd_opts.x.append(x_arg)
|
||||
return self.call_configure_callbacks(config)
|
||||
|
||||
|
||||
def catch_errors(f):
|
||||
@wraps(f)
|
||||
def wrapped(*args, **kwargs):
|
||||
try:
|
||||
f(*args, **kwargs)
|
||||
except (CommandError, RuntimeError) as exc:
|
||||
log.error('Error: ' + str(exc))
|
||||
sys.exit(1)
|
||||
return wrapped
|
||||
|
||||
|
||||
@catch_errors
|
||||
def list_templates():
|
||||
"""List available templates."""
|
||||
config = Config()
|
||||
config.print_stdout("Available templates:\n")
|
||||
for tempname in sorted(os.listdir(config.get_template_directory())):
|
||||
with open(
|
||||
os.path.join(config.get_template_directory(), tempname, "README")
|
||||
) as readme:
|
||||
synopsis = next(readme).strip()
|
||||
config.print_stdout("%s - %s", tempname, synopsis)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def init(directory=None, multidb=False, template=None, package=False):
|
||||
"""Creates a new migration repository"""
|
||||
if directory is None:
|
||||
directory = current_app.extensions['migrate'].directory
|
||||
template_directory = None
|
||||
if template is not None and ('/' in template or '\\' in template):
|
||||
template_directory, template = os.path.split(template)
|
||||
config = Config(template_directory=template_directory)
|
||||
config.set_main_option('script_location', directory)
|
||||
config.config_file_name = os.path.join(directory, 'alembic.ini')
|
||||
config = current_app.extensions['migrate'].\
|
||||
migrate.call_configure_callbacks(config)
|
||||
if multidb and template is None:
|
||||
template = 'flask-multidb'
|
||||
elif template is None:
|
||||
template = 'flask'
|
||||
command.init(config, directory, template=template, package=package)
|
||||
|
||||
|
||||
@catch_errors
|
||||
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."""
|
||||
opts = ['autogenerate'] if autogenerate else None
|
||||
config = current_app.extensions['migrate'].migrate.get_config(
|
||||
directory, opts=opts)
|
||||
command.revision(config, message, autogenerate=autogenerate, sql=sql,
|
||||
head=head, splice=splice, branch_label=branch_label,
|
||||
version_path=version_path, rev_id=rev_id)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def migrate(directory=None, message=None, sql=False, head='head', splice=False,
|
||||
branch_label=None, version_path=None, rev_id=None, x_arg=None):
|
||||
"""Alias for 'revision --autogenerate'"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(
|
||||
directory, opts=['autogenerate'], x_arg=x_arg)
|
||||
command.revision(config, message, autogenerate=True, sql=sql,
|
||||
head=head, splice=splice, branch_label=branch_label,
|
||||
version_path=version_path, rev_id=rev_id)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def edit(directory=None, revision='current'):
|
||||
"""Edit current revision."""
|
||||
if alembic_version >= (0, 8, 0):
|
||||
config = current_app.extensions['migrate'].migrate.get_config(
|
||||
directory)
|
||||
command.edit(config, revision)
|
||||
else:
|
||||
raise RuntimeError('Alembic 0.8.0 or greater is required')
|
||||
|
||||
|
||||
@catch_errors
|
||||
def merge(directory=None, revisions='', message=None, branch_label=None,
|
||||
rev_id=None):
|
||||
"""Merge two revisions together. Creates a new migration file"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.merge(config, revisions, message=message,
|
||||
branch_label=branch_label, rev_id=rev_id)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def upgrade(directory=None, revision='head', sql=False, tag=None, x_arg=None):
|
||||
"""Upgrade to a later version"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory,
|
||||
x_arg=x_arg)
|
||||
command.upgrade(config, revision, sql=sql, tag=tag)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def downgrade(directory=None, revision='-1', sql=False, tag=None, x_arg=None):
|
||||
"""Revert to a previous version"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory,
|
||||
x_arg=x_arg)
|
||||
if sql and revision == '-1':
|
||||
revision = 'head:-1'
|
||||
command.downgrade(config, revision, sql=sql, tag=tag)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def show(directory=None, revision='head'):
|
||||
"""Show the revision denoted by the given symbol."""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.show(config, revision)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def history(directory=None, rev_range=None, verbose=False,
|
||||
indicate_current=False):
|
||||
"""List changeset scripts in chronological order."""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
if alembic_version >= (0, 9, 9):
|
||||
command.history(config, rev_range, verbose=verbose,
|
||||
indicate_current=indicate_current)
|
||||
else:
|
||||
command.history(config, rev_range, verbose=verbose)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def heads(directory=None, verbose=False, resolve_dependencies=False):
|
||||
"""Show current available heads in the script directory"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.heads(config, verbose=verbose,
|
||||
resolve_dependencies=resolve_dependencies)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def branches(directory=None, verbose=False):
|
||||
"""Show current branch points"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.branches(config, verbose=verbose)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def current(directory=None, verbose=False):
|
||||
"""Display the current revision for each database."""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.current(config, verbose=verbose)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def stamp(directory=None, revision='head', sql=False, tag=None, purge=False):
|
||||
"""'stamp' the revision table with the given revision; don't run any
|
||||
migrations"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.stamp(config, revision, sql=sql, tag=tag, purge=purge)
|
||||
|
||||
|
||||
@catch_errors
|
||||
def check(directory=None):
|
||||
"""Check if there are any new operations to migrate"""
|
||||
config = current_app.extensions['migrate'].migrate.get_config(directory)
|
||||
command.check(config)
|
||||
Binary file not shown.
Binary file not shown.
261
venv/Lib/site-packages/flask_migrate/cli.py
Normal file
261
venv/Lib/site-packages/flask_migrate/cli.py
Normal file
@@ -0,0 +1,261 @@
|
||||
import click
|
||||
from flask import g
|
||||
from flask.cli import with_appcontext
|
||||
from flask_migrate import list_templates as _list_templates
|
||||
from flask_migrate import init as _init
|
||||
from flask_migrate import revision as _revision
|
||||
from flask_migrate import migrate as _migrate
|
||||
from flask_migrate import edit as _edit
|
||||
from flask_migrate import merge as _merge
|
||||
from flask_migrate import upgrade as _upgrade
|
||||
from flask_migrate import downgrade as _downgrade
|
||||
from flask_migrate import show as _show
|
||||
from flask_migrate import history as _history
|
||||
from flask_migrate import heads as _heads
|
||||
from flask_migrate import branches as _branches
|
||||
from flask_migrate import current as _current
|
||||
from flask_migrate import stamp as _stamp
|
||||
from flask_migrate import check as _check
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-x', '--x-arg', multiple=True,
|
||||
help='Additional arguments consumed by custom env.py scripts')
|
||||
@with_appcontext
|
||||
def db(directory, x_arg):
|
||||
"""Perform database migrations."""
|
||||
g.directory = directory
|
||||
g.x_arg = x_arg # these will be picked up by Migrate.get_config()
|
||||
|
||||
|
||||
@db.command()
|
||||
@with_appcontext
|
||||
def list_templates():
|
||||
"""List available templates."""
|
||||
_list_templates()
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('--multidb', is_flag=True,
|
||||
help=('Support multiple databases'))
|
||||
@click.option('-t', '--template', default=None,
|
||||
help=('Repository template to use (default is "flask")'))
|
||||
@click.option('--package', is_flag=True,
|
||||
help=('Write empty __init__.py files to the environment and '
|
||||
'version locations'))
|
||||
@with_appcontext
|
||||
def init(directory, multidb, template, package):
|
||||
"""Creates a new migration repository."""
|
||||
_init(directory or g.directory, multidb, template, package)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-m', '--message', default=None, help='Revision message')
|
||||
@click.option('--autogenerate', is_flag=True,
|
||||
help=('Populate revision script with candidate migration '
|
||||
'operations, based on comparison of database to model'))
|
||||
@click.option('--sql', is_flag=True,
|
||||
help=('Don\'t emit SQL to database - dump to standard output '
|
||||
'instead'))
|
||||
@click.option('--head', default='head',
|
||||
help=('Specify head revision or <branchname>@head to base new '
|
||||
'revision on'))
|
||||
@click.option('--splice', is_flag=True,
|
||||
help=('Allow a non-head revision as the "head" to splice onto'))
|
||||
@click.option('--branch-label', default=None,
|
||||
help=('Specify a branch label to apply to the new revision'))
|
||||
@click.option('--version-path', default=None,
|
||||
help=('Specify specific path from config for version file'))
|
||||
@click.option('--rev-id', default=None,
|
||||
help=('Specify a hardcoded revision id instead of generating '
|
||||
'one'))
|
||||
@with_appcontext
|
||||
def revision(directory, message, autogenerate, sql, head, splice, branch_label,
|
||||
version_path, rev_id):
|
||||
"""Create a new revision file."""
|
||||
_revision(directory or g.directory, message, autogenerate, sql, head,
|
||||
splice, branch_label, version_path, rev_id)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-m', '--message', default=None, help='Revision message')
|
||||
@click.option('--sql', is_flag=True,
|
||||
help=('Don\'t emit SQL to database - dump to standard output '
|
||||
'instead'))
|
||||
@click.option('--head', default='head',
|
||||
help=('Specify head revision or <branchname>@head to base new '
|
||||
'revision on'))
|
||||
@click.option('--splice', is_flag=True,
|
||||
help=('Allow a non-head revision as the "head" to splice onto'))
|
||||
@click.option('--branch-label', default=None,
|
||||
help=('Specify a branch label to apply to the new revision'))
|
||||
@click.option('--version-path', default=None,
|
||||
help=('Specify specific path from config for version file'))
|
||||
@click.option('--rev-id', default=None,
|
||||
help=('Specify a hardcoded revision id instead of generating '
|
||||
'one'))
|
||||
@click.option('-x', '--x-arg', multiple=True,
|
||||
help='Additional arguments consumed by custom env.py scripts')
|
||||
@with_appcontext
|
||||
def migrate(directory, message, sql, head, splice, branch_label, version_path,
|
||||
rev_id, x_arg):
|
||||
"""Autogenerate a new revision file (Alias for
|
||||
'revision --autogenerate')"""
|
||||
_migrate(directory or g.directory, message, sql, head, splice,
|
||||
branch_label, version_path, rev_id, x_arg or g.x_arg)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.argument('revision', default='head')
|
||||
@with_appcontext
|
||||
def edit(directory, revision):
|
||||
"""Edit a revision file"""
|
||||
_edit(directory or g.directory, revision)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-m', '--message', default=None, help='Merge revision message')
|
||||
@click.option('--branch-label', default=None,
|
||||
help=('Specify a branch label to apply to the new revision'))
|
||||
@click.option('--rev-id', default=None,
|
||||
help=('Specify a hardcoded revision id instead of generating '
|
||||
'one'))
|
||||
@click.argument('revisions', nargs=-1)
|
||||
@with_appcontext
|
||||
def merge(directory, message, branch_label, rev_id, revisions):
|
||||
"""Merge two revisions together, creating a new revision file"""
|
||||
_merge(directory or g.directory, revisions, message, branch_label, rev_id)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('--sql', is_flag=True,
|
||||
help=('Don\'t emit SQL to database - dump to standard output '
|
||||
'instead'))
|
||||
@click.option('--tag', default=None,
|
||||
help=('Arbitrary "tag" name - can be used by custom env.py '
|
||||
'scripts'))
|
||||
@click.option('-x', '--x-arg', multiple=True,
|
||||
help='Additional arguments consumed by custom env.py scripts')
|
||||
@click.argument('revision', default='head')
|
||||
@with_appcontext
|
||||
def upgrade(directory, sql, tag, x_arg, revision):
|
||||
"""Upgrade to a later version"""
|
||||
_upgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('--sql', is_flag=True,
|
||||
help=('Don\'t emit SQL to database - dump to standard output '
|
||||
'instead'))
|
||||
@click.option('--tag', default=None,
|
||||
help=('Arbitrary "tag" name - can be used by custom env.py '
|
||||
'scripts'))
|
||||
@click.option('-x', '--x-arg', multiple=True,
|
||||
help='Additional arguments consumed by custom env.py scripts')
|
||||
@click.argument('revision', default='-1')
|
||||
@with_appcontext
|
||||
def downgrade(directory, sql, tag, x_arg, revision):
|
||||
"""Revert to a previous version"""
|
||||
_downgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.argument('revision', default='head')
|
||||
@with_appcontext
|
||||
def show(directory, revision):
|
||||
"""Show the revision denoted by the given symbol."""
|
||||
_show(directory or g.directory, revision)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-r', '--rev-range', default=None,
|
||||
help='Specify a revision range; format is [start]:[end]')
|
||||
@click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
|
||||
@click.option('-i', '--indicate-current', is_flag=True,
|
||||
help=('Indicate current version (Alembic 0.9.9 or greater is '
|
||||
'required)'))
|
||||
@with_appcontext
|
||||
def history(directory, rev_range, verbose, indicate_current):
|
||||
"""List changeset scripts in chronological order."""
|
||||
_history(directory or g.directory, rev_range, verbose, indicate_current)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
|
||||
@click.option('--resolve-dependencies', is_flag=True,
|
||||
help='Treat dependency versions as down revisions')
|
||||
@with_appcontext
|
||||
def heads(directory, verbose, resolve_dependencies):
|
||||
"""Show current available heads in the script directory"""
|
||||
_heads(directory or g.directory, verbose, resolve_dependencies)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
|
||||
@with_appcontext
|
||||
def branches(directory, verbose):
|
||||
"""Show current branch points"""
|
||||
_branches(directory or g.directory, verbose)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('-v', '--verbose', is_flag=True, help='Use more verbose output')
|
||||
@with_appcontext
|
||||
def current(directory, verbose):
|
||||
"""Display the current revision for each database."""
|
||||
_current(directory or g.directory, verbose)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@click.option('--sql', is_flag=True,
|
||||
help=('Don\'t emit SQL to database - dump to standard output '
|
||||
'instead'))
|
||||
@click.option('--tag', default=None,
|
||||
help=('Arbitrary "tag" name - can be used by custom env.py '
|
||||
'scripts'))
|
||||
@click.option('--purge', is_flag=True,
|
||||
help=('Delete the version in the alembic_version table before '
|
||||
'stamping'))
|
||||
@click.argument('revision', default='head')
|
||||
@with_appcontext
|
||||
def stamp(directory, sql, tag, revision, purge):
|
||||
"""'stamp' the revision table with the given revision; don't run any
|
||||
migrations"""
|
||||
_stamp(directory or g.directory, revision, sql, tag, purge)
|
||||
|
||||
|
||||
@db.command()
|
||||
@click.option('-d', '--directory', default=None,
|
||||
help=('Migration script directory (default is "migrations")'))
|
||||
@with_appcontext
|
||||
def check(directory):
|
||||
"""Check if there are any new operations to migrate"""
|
||||
_check(directory or g.directory)
|
||||
@@ -0,0 +1 @@
|
||||
Multi-database configuration for aioflask.
|
||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic,flask_migrate
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
@@ -0,0 +1,202 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import MetaData
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
USE_TWOPHASE = False
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
|
||||
def get_engine(bind_key=None):
|
||||
try:
|
||||
# this works with Flask-SQLAlchemy<3 and Alchemical
|
||||
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
|
||||
except (TypeError, AttributeError):
|
||||
# this works with Flask-SQLAlchemy>=3
|
||||
return current_app.extensions['migrate'].db.engines.get(bind_key)
|
||||
|
||||
|
||||
def get_engine_url(bind_key=None):
|
||||
try:
|
||||
return get_engine(bind_key).url.render_as_string(
|
||||
hide_password=False).replace('%', '%%')
|
||||
except AttributeError:
|
||||
return str(get_engine(bind_key).url).replace('%', '%%')
|
||||
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option('sqlalchemy.url', get_engine_url())
|
||||
bind_names = []
|
||||
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
|
||||
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
|
||||
else:
|
||||
get_bind_names = getattr(current_app.extensions['migrate'].db,
|
||||
'bind_names', None)
|
||||
if get_bind_names:
|
||||
bind_names = get_bind_names()
|
||||
for bind in bind_names:
|
||||
context.config.set_section_option(
|
||||
bind, "sqlalchemy.url", get_engine_url(bind_key=bind))
|
||||
target_db = current_app.extensions['migrate'].db
|
||||
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_metadata(bind):
|
||||
"""Return the metadata for a bind."""
|
||||
if bind == '':
|
||||
bind = None
|
||||
if hasattr(target_db, 'metadatas'):
|
||||
return target_db.metadatas[bind]
|
||||
|
||||
# legacy, less flexible implementation
|
||||
m = MetaData()
|
||||
for t in target_db.metadata.tables.values():
|
||||
if t.info.get('bind_key') == bind:
|
||||
t.tometadata(m)
|
||||
return m
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
# for the --sql use case, run migrations for each URL into
|
||||
# individual files.
|
||||
|
||||
engines = {
|
||||
'': {
|
||||
'url': context.config.get_main_option('sqlalchemy.url')
|
||||
}
|
||||
}
|
||||
for name in bind_names:
|
||||
engines[name] = rec = {}
|
||||
rec['url'] = context.config.get_section_option(name, "sqlalchemy.url")
|
||||
|
||||
for name, rec in engines.items():
|
||||
logger.info("Migrating database %s" % (name or '<default>'))
|
||||
file_ = "%s.sql" % name
|
||||
logger.info("Writing output to %s" % file_)
|
||||
with open(file_, 'w') as buffer:
|
||||
context.configure(
|
||||
url=rec['url'],
|
||||
output_buffer=buffer,
|
||||
target_metadata=get_metadata(name),
|
||||
literal_binds=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations(engine_name=name)
|
||||
|
||||
|
||||
def do_run_migrations(_, engines):
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if len(script.upgrade_ops_list) >= len(bind_names) + 1:
|
||||
empty = True
|
||||
for upgrade_ops in script.upgrade_ops_list:
|
||||
if not upgrade_ops.is_empty():
|
||||
empty = False
|
||||
if empty:
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
conf_args = current_app.extensions['migrate'].configure_args
|
||||
if conf_args.get("process_revision_directives") is None:
|
||||
conf_args["process_revision_directives"] = process_revision_directives
|
||||
|
||||
for name, rec in engines.items():
|
||||
rec['sync_connection'] = conn = rec['connection']._sync_connection()
|
||||
if USE_TWOPHASE:
|
||||
rec['transaction'] = conn.begin_twophase()
|
||||
else:
|
||||
rec['transaction'] = conn.begin()
|
||||
|
||||
try:
|
||||
for name, rec in engines.items():
|
||||
logger.info("Migrating database %s" % (name or '<default>'))
|
||||
context.configure(
|
||||
connection=rec['sync_connection'],
|
||||
upgrade_token="%s_upgrades" % name,
|
||||
downgrade_token="%s_downgrades" % name,
|
||||
target_metadata=get_metadata(name),
|
||||
**conf_args
|
||||
)
|
||||
context.run_migrations(engine_name=name)
|
||||
|
||||
if USE_TWOPHASE:
|
||||
for rec in engines.values():
|
||||
rec['transaction'].prepare()
|
||||
|
||||
for rec in engines.values():
|
||||
rec['transaction'].commit()
|
||||
except: # noqa: E722
|
||||
for rec in engines.values():
|
||||
rec['transaction'].rollback()
|
||||
raise
|
||||
finally:
|
||||
for rec in engines.values():
|
||||
rec['sync_connection'].close()
|
||||
|
||||
|
||||
async def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# for the direct-to-DB use case, start a transaction on all
|
||||
# engines, then run all migrations, then commit all transactions.
|
||||
engines = {
|
||||
'': {'engine': get_engine()}
|
||||
}
|
||||
for name in bind_names:
|
||||
engines[name] = rec = {}
|
||||
rec['engine'] = get_engine(bind_key=name)
|
||||
|
||||
for name, rec in engines.items():
|
||||
engine = rec['engine']
|
||||
rec['connection'] = await engine.connect().start()
|
||||
|
||||
await engines['']['connection'].run_sync(do_run_migrations, engines)
|
||||
|
||||
for rec in engines.values():
|
||||
await rec['connection'].close()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
asyncio.get_event_loop().run_until_complete(run_migrations_online())
|
||||
@@ -0,0 +1,53 @@
|
||||
<%!
|
||||
import re
|
||||
|
||||
%>"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade(engine_name):
|
||||
globals()["upgrade_%s" % engine_name]()
|
||||
|
||||
|
||||
def downgrade(engine_name):
|
||||
globals()["downgrade_%s" % engine_name]()
|
||||
|
||||
<%
|
||||
from flask import current_app
|
||||
bind_names = []
|
||||
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
|
||||
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
|
||||
else:
|
||||
get_bind_names = getattr(current_app.extensions['migrate'].db, 'bind_names', None)
|
||||
if get_bind_names:
|
||||
bind_names = get_bind_names()
|
||||
db_names = [''] + bind_names
|
||||
%>
|
||||
|
||||
## generate an "upgrade_<xyz>() / downgrade_<xyz>()" function
|
||||
## for each database name in the ini file.
|
||||
|
||||
% for db_name in db_names:
|
||||
|
||||
def upgrade_${db_name}():
|
||||
${context.get("%s_upgrades" % db_name, "pass")}
|
||||
|
||||
|
||||
def downgrade_${db_name}():
|
||||
${context.get("%s_downgrades" % db_name, "pass")}
|
||||
|
||||
% endfor
|
||||
@@ -0,0 +1 @@
|
||||
Single-database configuration for aioflask.
|
||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic,flask_migrate
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
118
venv/Lib/site-packages/flask_migrate/templates/aioflask/env.py
Normal file
118
venv/Lib/site-packages/flask_migrate/templates/aioflask/env.py
Normal file
@@ -0,0 +1,118 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
|
||||
def get_engine():
|
||||
try:
|
||||
# this works with Flask-SQLAlchemy<3 and Alchemical
|
||||
return current_app.extensions['migrate'].db.get_engine()
|
||||
except (TypeError, AttributeError):
|
||||
# this works with Flask-SQLAlchemy>=3
|
||||
return current_app.extensions['migrate'].db.engine
|
||||
|
||||
|
||||
def get_engine_url():
|
||||
try:
|
||||
return get_engine().url.render_as_string(hide_password=False).replace(
|
||||
'%', '%%')
|
||||
except AttributeError:
|
||||
return str(get_engine().url).replace('%', '%%')
|
||||
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option('sqlalchemy.url', get_engine_url())
|
||||
target_db = current_app.extensions['migrate'].db
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_metadata():
|
||||
if hasattr(target_db, 'metadatas'):
|
||||
return target_db.metadatas[None]
|
||||
return target_db.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url, target_metadata=get_metadata(), literal_binds=True
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection):
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
conf_args = current_app.extensions['migrate'].configure_args
|
||||
if conf_args.get("process_revision_directives") is None:
|
||||
conf_args["process_revision_directives"] = process_revision_directives
|
||||
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=get_metadata(),
|
||||
**conf_args
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
connectable = get_engine()
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
asyncio.get_event_loop().run_until_complete(run_migrations_online())
|
||||
@@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1 @@
|
||||
Multi-database configuration for Flask.
|
||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic,flask_migrate
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
@@ -0,0 +1,191 @@
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import MetaData
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
USE_TWOPHASE = False
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
|
||||
def get_engine(bind_key=None):
|
||||
try:
|
||||
# this works with Flask-SQLAlchemy<3 and Alchemical
|
||||
return current_app.extensions['migrate'].db.get_engine(bind=bind_key)
|
||||
except (TypeError, AttributeError):
|
||||
# this works with Flask-SQLAlchemy>=3
|
||||
return current_app.extensions['migrate'].db.engines.get(bind_key)
|
||||
|
||||
|
||||
def get_engine_url(bind_key=None):
|
||||
try:
|
||||
return get_engine(bind_key).url.render_as_string(
|
||||
hide_password=False).replace('%', '%%')
|
||||
except AttributeError:
|
||||
return str(get_engine(bind_key).url).replace('%', '%%')
|
||||
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option('sqlalchemy.url', get_engine_url())
|
||||
bind_names = []
|
||||
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
|
||||
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
|
||||
else:
|
||||
get_bind_names = getattr(current_app.extensions['migrate'].db,
|
||||
'bind_names', None)
|
||||
if get_bind_names:
|
||||
bind_names = get_bind_names()
|
||||
for bind in bind_names:
|
||||
context.config.set_section_option(
|
||||
bind, "sqlalchemy.url", get_engine_url(bind_key=bind))
|
||||
target_db = current_app.extensions['migrate'].db
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_metadata(bind):
|
||||
"""Return the metadata for a bind."""
|
||||
if bind == '':
|
||||
bind = None
|
||||
if hasattr(target_db, 'metadatas'):
|
||||
return target_db.metadatas[bind]
|
||||
|
||||
# legacy, less flexible implementation
|
||||
m = MetaData()
|
||||
for t in target_db.metadata.tables.values():
|
||||
if t.info.get('bind_key') == bind:
|
||||
t.tometadata(m)
|
||||
return m
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
# for the --sql use case, run migrations for each URL into
|
||||
# individual files.
|
||||
|
||||
engines = {
|
||||
'': {
|
||||
'url': context.config.get_main_option('sqlalchemy.url')
|
||||
}
|
||||
}
|
||||
for name in bind_names:
|
||||
engines[name] = rec = {}
|
||||
rec['url'] = context.config.get_section_option(name, "sqlalchemy.url")
|
||||
|
||||
for name, rec in engines.items():
|
||||
logger.info("Migrating database %s" % (name or '<default>'))
|
||||
file_ = "%s.sql" % name
|
||||
logger.info("Writing output to %s" % file_)
|
||||
with open(file_, 'w') as buffer:
|
||||
context.configure(
|
||||
url=rec['url'],
|
||||
output_buffer=buffer,
|
||||
target_metadata=get_metadata(name),
|
||||
literal_binds=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations(engine_name=name)
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if len(script.upgrade_ops_list) >= len(bind_names) + 1:
|
||||
empty = True
|
||||
for upgrade_ops in script.upgrade_ops_list:
|
||||
if not upgrade_ops.is_empty():
|
||||
empty = False
|
||||
if empty:
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
conf_args = current_app.extensions['migrate'].configure_args
|
||||
if conf_args.get("process_revision_directives") is None:
|
||||
conf_args["process_revision_directives"] = process_revision_directives
|
||||
|
||||
# for the direct-to-DB use case, start a transaction on all
|
||||
# engines, then run all migrations, then commit all transactions.
|
||||
engines = {
|
||||
'': {'engine': get_engine()}
|
||||
}
|
||||
for name in bind_names:
|
||||
engines[name] = rec = {}
|
||||
rec['engine'] = get_engine(bind_key=name)
|
||||
|
||||
for name, rec in engines.items():
|
||||
engine = rec['engine']
|
||||
rec['connection'] = conn = engine.connect()
|
||||
|
||||
if USE_TWOPHASE:
|
||||
rec['transaction'] = conn.begin_twophase()
|
||||
else:
|
||||
rec['transaction'] = conn.begin()
|
||||
|
||||
try:
|
||||
for name, rec in engines.items():
|
||||
logger.info("Migrating database %s" % (name or '<default>'))
|
||||
context.configure(
|
||||
connection=rec['connection'],
|
||||
upgrade_token="%s_upgrades" % name,
|
||||
downgrade_token="%s_downgrades" % name,
|
||||
target_metadata=get_metadata(name),
|
||||
**conf_args
|
||||
)
|
||||
context.run_migrations(engine_name=name)
|
||||
|
||||
if USE_TWOPHASE:
|
||||
for rec in engines.values():
|
||||
rec['transaction'].prepare()
|
||||
|
||||
for rec in engines.values():
|
||||
rec['transaction'].commit()
|
||||
except: # noqa: E722
|
||||
for rec in engines.values():
|
||||
rec['transaction'].rollback()
|
||||
raise
|
||||
finally:
|
||||
for rec in engines.values():
|
||||
rec['connection'].close()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,53 @@
|
||||
<%!
|
||||
import re
|
||||
|
||||
%>"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade(engine_name):
|
||||
globals()["upgrade_%s" % engine_name]()
|
||||
|
||||
|
||||
def downgrade(engine_name):
|
||||
globals()["downgrade_%s" % engine_name]()
|
||||
|
||||
<%
|
||||
from flask import current_app
|
||||
bind_names = []
|
||||
if current_app.config.get('SQLALCHEMY_BINDS') is not None:
|
||||
bind_names = list(current_app.config['SQLALCHEMY_BINDS'].keys())
|
||||
else:
|
||||
get_bind_names = getattr(current_app.extensions['migrate'].db, 'bind_names', None)
|
||||
if get_bind_names:
|
||||
bind_names = get_bind_names()
|
||||
db_names = [''] + bind_names
|
||||
%>
|
||||
|
||||
## generate an "upgrade_<xyz>() / downgrade_<xyz>()" function
|
||||
## for each database name in the ini file.
|
||||
|
||||
% for db_name in db_names:
|
||||
|
||||
def upgrade_${db_name}():
|
||||
${context.get("%s_upgrades" % db_name, "pass")}
|
||||
|
||||
|
||||
def downgrade_${db_name}():
|
||||
${context.get("%s_downgrades" % db_name, "pass")}
|
||||
|
||||
% endfor
|
||||
@@ -0,0 +1 @@
|
||||
Single-database configuration for Flask.
|
||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic,flask_migrate
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
113
venv/Lib/site-packages/flask_migrate/templates/flask/env.py
Normal file
113
venv/Lib/site-packages/flask_migrate/templates/flask/env.py
Normal file
@@ -0,0 +1,113 @@
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
|
||||
def get_engine():
|
||||
try:
|
||||
# this works with Flask-SQLAlchemy<3 and Alchemical
|
||||
return current_app.extensions['migrate'].db.get_engine()
|
||||
except (TypeError, AttributeError):
|
||||
# this works with Flask-SQLAlchemy>=3
|
||||
return current_app.extensions['migrate'].db.engine
|
||||
|
||||
|
||||
def get_engine_url():
|
||||
try:
|
||||
return get_engine().url.render_as_string(hide_password=False).replace(
|
||||
'%', '%%')
|
||||
except AttributeError:
|
||||
return str(get_engine().url).replace('%', '%%')
|
||||
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option('sqlalchemy.url', get_engine_url())
|
||||
target_db = current_app.extensions['migrate'].db
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_metadata():
|
||||
if hasattr(target_db, 'metadatas'):
|
||||
return target_db.metadatas[None]
|
||||
return target_db.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url, target_metadata=get_metadata(), literal_binds=True
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
conf_args = current_app.extensions['migrate'].configure_args
|
||||
if conf_args.get("process_revision_directives") is None:
|
||||
conf_args["process_revision_directives"] = process_revision_directives
|
||||
|
||||
connectable = get_engine()
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=get_metadata(),
|
||||
**conf_args
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
Reference in New Issue
Block a user