本文整理汇总了Python中alembic.command.upgrade方法的典型用法代码示例。如果您正苦于以下问题:Python command.upgrade方法的具体用法?Python command.upgrade怎么用?Python command.upgrade使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类alembic.command
的用法示例。
在下文中一共展示了command.upgrade方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_development_data
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def init_development_data(context, upgrade_db=True, skip_on_failure=False):
"""
Fill a database with development data like default users.
"""
if upgrade_db:
context.invoke_execute(context, 'app.db.upgrade')
log.info("Initializing development data...")
from migrations import initial_development_data
try:
initial_development_data.init()
except AssertionError as exception:
if not skip_on_failure:
log.error("%s", exception)
else:
log.debug(
"The following error was ignored due to the `skip_on_failure` flag: %s",
exception
)
log.info("Initializing development data step is skipped.")
else:
log.info("Fixtures have been successfully applied.")
示例2: _test_005_nextrev
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def _test_005_nextrev(self):
script = env.generate_revision(
def_, "this is the next rev", refresh=True
)
assert os.access(
os.path.join(
env.dir, "versions", "%s_this_is_the_next_rev.py" % def_
),
os.F_OK,
)
eq_(script.revision, def_)
eq_(script.down_revision, abc)
eq_(env.get_revision(abc).nextrev, set([def_]))
assert script.module.down_revision == abc
assert callable(script.module.upgrade)
assert callable(script.module.downgrade)
eq_(env.get_heads(), [def_])
eq_(env.get_base(), abc)
示例3: test_renders_added_directives_no_autogen
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def test_renders_added_directives_no_autogen(self):
m = sa.MetaData()
def process_revision_directives(context, rev, generate_revisions):
generate_revisions[0].upgrade_ops.ops.append(
ops.CreateIndexOp("some_index", "some_table", ["a", "b"])
)
with self._env_fixture(process_revision_directives, m):
rev = command.revision(
self.cfg, message="some message", head="model1@head", sql=True
)
with mock.patch.object(rev.module, "op") as op_mock:
rev.module.upgrade()
eq_(
op_mock.mock_calls,
[
mock.call.create_index(
"some_index", "some_table", ["a", "b"], unique=False
)
],
)
示例4: test_create_rev_autogen_db_not_up_to_date_multi_heads
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def test_create_rev_autogen_db_not_up_to_date_multi_heads(self):
self._env_fixture()
command.revision(self.cfg)
rev2 = command.revision(self.cfg)
rev3a = command.revision(self.cfg)
command.revision(self.cfg, head=rev2.revision, splice=True)
command.upgrade(self.cfg, "heads")
command.revision(self.cfg, head=rev3a.revision)
assert_raises_message(
util.CommandError,
"Target database is not up to date.",
command.revision,
self.cfg,
autogenerate=True,
)
示例5: test_create_rev_plain_db_not_up_to_date_multi_heads
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def test_create_rev_plain_db_not_up_to_date_multi_heads(self):
self._env_fixture()
command.revision(self.cfg)
rev2 = command.revision(self.cfg)
rev3a = command.revision(self.cfg)
command.revision(self.cfg, head=rev2.revision, splice=True)
command.upgrade(self.cfg, "heads")
command.revision(self.cfg, head=rev3a.revision)
assert_raises_message(
util.CommandError,
"Multiple heads are present; please specify the head revision "
"on which the new revision should be based, or perform a merge.",
command.revision,
self.cfg,
)
示例6: test_create_rev_autogen_need_to_select_head
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def test_create_rev_autogen_need_to_select_head(self):
self._env_fixture()
command.revision(self.cfg)
rev2 = command.revision(self.cfg)
command.revision(self.cfg)
command.revision(self.cfg, head=rev2.revision, splice=True)
command.upgrade(self.cfg, "heads")
# there's multiple heads present
assert_raises_message(
util.CommandError,
"Multiple heads are present; please specify the head revision "
"on which the new revision should be based, or perform a merge.",
command.revision,
self.cfg,
autogenerate=True,
)
示例7: test_err_correctly_raised_on_dupe_rows_no_pk
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def test_err_correctly_raised_on_dupe_rows_no_pk(self):
self._env_fixture(version_table_pk=False)
command.revision(self.cfg)
r2 = command.revision(self.cfg)
db = _sqlite_file_db()
command.upgrade(self.cfg, "head")
with db.connect() as conn:
conn.execute(
text("insert into alembic_version values ('%s')" % r2.revision)
)
assert_raises_message(
util.CommandError,
"Online migration expected to match one row when "
"updating .* in 'alembic_version'; 2 found",
command.downgrade,
self.cfg,
"-1",
)
示例8: _inline_enum_script
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def _inline_enum_script(self):
write_script(
self.script,
self.rid,
"""
revision = '%s'
down_revision = None
from alembic import op
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy import Column
def upgrade():
op.create_table("sometable",
Column("data", ENUM("one", "two", "three", name="pgenum"))
)
def downgrade():
op.drop_table("sometable")
"""
% self.rid,
)
示例9: _reset_database
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def _reset_database(request, app):
db_name = "test_freight"
session.close_all_sessions()
# 9.1 does not support --if-exists
if subprocess.call(f"psql -l | grep '{db_name}'", shell=True) == 0:
engine = db.engine
engine.connect().close()
engine.dispose()
subprocess.check_call(f"dropdb {db_name}", shell=True)
subprocess.check_call(f"createdb -E utf-8 {db_name}", shell=True)
command.upgrade(ALEMBIC_CONFIG, "head")
return lambda: reset_database(request, app)
示例10: create_all_or_upgrade_db
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def create_all_or_upgrade_db(engine, db_url):
# alembic add a lot of import time, so we lazy import
from alembic import command
from alembic.config import Config
from sqlalchemy import inspect
alembic_config_file = os.path.join(os.path.dirname(__file__), 'alembic.ini')
alembic_config = Config(alembic_config_file)
alembic_config.set_main_option('sqlalchemy.url', db_url)
inspector = inspect(engine)
tables = inspector.get_table_names()
if 'deployments' not in tables and 'bentos' not in tables:
logger.debug('Creating tables')
Base.metadata.create_all(engine)
command.stamp(alembic_config, 'head')
else:
logger.debug('Upgrading tables to the latest revision')
command.upgrade(alembic_config, 'heads')
示例11: upgrade
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def upgrade(self, nocreate=False):
from alembic import command
from alembic import migration
cfg = self._get_alembic_config()
cfg.conf = self.conf
if nocreate:
command.upgrade(cfg, "head")
else:
with self.facade.writer_connection() as connection:
ctxt = migration.MigrationContext.configure(connection)
current_version = ctxt.get_current_revision()
if current_version is None:
Base.metadata.create_all(connection)
command.stamp(cfg, "head")
else:
command.upgrade(cfg, "head")
try:
with self.facade.writer() as session:
session.add(
ResourceType(
name="generic",
tablename="generic",
state="active",
attributes=resource_type.ResourceTypeAttributes()))
except exception.DBDuplicateEntry:
pass
# NOTE(jd) We can have deadlock errors either here or later in
# map_and_create_tables(). We can't decorate create_resource_type()
# directly or each part might retry later on its own and cause a
# duplicate. And it seems there's no way to use the same session for
# both adding the resource_type in our table and calling
# map_and_create_tables() :-(
示例12: oneshot
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def oneshot(cli_ctx, alembic_config):
'''
Set up your database with one-shot schema migration instead of
iterating over multiple revisions if there is no existing database.
It uses alembic.ini to configure database connection.
Reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
#building-an-up-to-date-database-from-scratch
'''
with cli_ctx.logger:
alembic_cfg = Config(alembic_config)
sa_url = alembic_cfg.get_main_option('sqlalchemy.url')
engine = sa.create_engine(sa_url)
engine.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')
with engine.begin() as connection:
context = MigrationContext.configure(connection)
current_rev = context.get_current_revision()
if current_rev is None:
# For a fresh clean database, create all from scratch.
# (it will raise error if tables already exist.)
log.info('Detected a fresh new database.')
log.info('Creating tables...')
with engine.begin() as connection:
alembic_cfg.attributes['connection'] = connection
metadata.create_all(engine, checkfirst=False)
log.info('Stamping alembic version to head...')
command.stamp(alembic_cfg, 'head')
else:
# If alembic version info is already available, perform incremental upgrade.
log.info('Detected an existing database.')
log.info('Performing schema upgrade to head...')
with engine.begin() as connection:
alembic_cfg.attributes['connection'] = connection
command.upgrade(alembic_cfg, 'head')
log.info("If you don't need old migrations, delete them and set "
"\"down_revision\" value in the earliest migration to \"None\".")
示例13: upgrade
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
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)
示例14: upgrade_tables
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def upgrade_tables(ctx):
""" Alias for 'alembic upgrade head'.
Upgrade to latest model version
"""
current_session = get_current_session()
command.upgrade(setup_alembic_config(url=current_session.url),
revision='head', sql=False, tag=None)
示例15: upgrade
# 需要导入模块: from alembic import command [as 别名]
# 或者: from alembic.command import upgrade [as 别名]
def upgrade(config, revision: str, sql: bool, tag: Optional[str]):
"""Upgrade to a later version."""
bot = Bot(config)
directory = os.path.join('yui', 'migrations')
c = Config(os.path.join(directory, 'alembic.ini'))
c.set_main_option('script_location', directory)
c.set_main_option('sqlalchemy.url', bot.config.DATABASE_URL)
c.attributes['Base'] = bot.orm_base
command.upgrade(c, revision, sql=sql, tag=tag)