本文整理匯總了Python中alembic.operations.Operations方法的典型用法代碼示例。如果您正苦於以下問題:Python operations.Operations方法的具體用法?Python operations.Operations怎麽用?Python operations.Operations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類alembic.operations
的用法示例。
在下文中一共展示了operations.Operations方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def __init__(self, registry):
self.withoutautomigration = registry.withoutautomigration
self.conn = registry.connection()
self.loaded_views = registry.loaded_views
self.metadata = registry.declarativebase.metadata
self.ddl_compiler = self.conn.dialect.ddl_compiler(
self.conn.dialect, None)
opts = {
'include_schemas': True,
'compare_server_default': True,
'render_item': self.render_item,
'compare_type': self.compare_type,
}
self.context = MigrationContext.configure(self.conn, opts=opts)
self.operation = Operations(self.context)
self.reinit_all = Configuration.get('reinit_all', False)
self.reinit_tables = Configuration.get('reinit_tables', False)
self.reinit_columns = Configuration.get('reinit_columns', False)
self.reinit_indexes = Configuration.get('reinit_indexes', False)
self.reinit_constraints = Configuration.get(
'reinit_constraints', False)
示例2: setUp
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def setUp(self):
self.conn = config.db.connect()
self.metadata = MetaData()
t1 = Table(
"foo",
self.metadata,
Column("id", Integer, primary_key=True),
Column("data", String(50)),
Column("x", Integer),
mysql_engine="InnoDB",
)
t1.create(self.conn)
self.conn.execute(
t1.insert(),
[
{"id": 1, "data": "d1", "x": 5},
{"id": 2, "data": "22", "x": 6},
{"id": 3, "data": "8.5", "x": 7},
{"id": 4, "data": "9.46", "x": 8},
{"id": 5, "data": "d5", "x": 9},
],
)
context = MigrationContext.configure(self.conn)
self.op = Operations(context)
示例3: recheck_alembic_table
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def recheck_alembic_table(conn):
"""check and update alembic version table.
Should check current alembic version table against conf and rename the
existing table if the two values don't match.
"""
conf_table = getattr(CONF, 'version_table')
conf_table_version = get_table_version(conn, conf_table)
current_table, default_table = get_db_tables(conn)
if current_table[0]:
if current_table[0] != conf_table:
context = alembic_migration.MigrationContext.configure(conn)
op = Operations(context)
if conf_table and not conf_table_version:
# make sure there is not present-but-empty table
# that will prevent us from renaming the current table
op.drop_table(conf_table)
op.rename_table(current_table[0], conf_table)
示例4: __init__
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def __init__(self, migration_context, impl=None):
"""Construct a new :class:`.Operations`
:param migration_context: a :class:`.MigrationContext`
instance.
"""
self.migration_context = migration_context
if impl is None:
self.impl = migration_context.impl
else:
self.impl = impl
self.schema_obj = schemaobj.SchemaObjects(migration_context)
示例5: context
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def context(cls, migration_context):
op = Operations(migration_context)
op._install_proxy()
yield op
op._remove_proxy()
示例6: invoke
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def invoke(self, operation):
"""Given a :class:`.MigrateOperation`, invoke it in terms of
this :class:`.Operations` instance.
.. versionadded:: 0.8.0
"""
fn = self._to_impl.dispatch(
operation, self.migration_context.impl.__dialect__)
return fn(self, operation)
示例7: fix
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def fix(self):
"""Uses Alembic batch operations to alter the column datatype in the
table.
"""
context = MigrationContext.configure(self.parent.engine.connect())
op = Operations(context)
for table in self.parent.base.metadata.sorted_tables:
if table.name == self.table:
for column in table.columns:
if column.name == self.column['name']:
with op.batch_alter_table(table.name) as batch_op:
batch_op.alter_column(column.name,
type_=column.type)
return
示例8: invoke
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def invoke(self, operation):
"""Given a :class:`.MigrateOperation`, invoke it in terms of
this :class:`.Operations` instance.
.. versionadded:: 0.8.0
"""
fn = self._to_impl.dispatch(
operation, self.migration_context.impl.__dialect__
)
return fn(self, operation)
示例9: _fixture
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def _fixture(self):
self.metadata = MetaData()
self.table = Table(
"foo",
self.metadata,
Column("id", Integer, primary_key=True),
Column("data", String(50)),
Column("x", Integer),
)
context = op_fixture(dialect="sqlite", as_sql=True)
self.op = Operations(context)
return context
示例10: test_standalone_op
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def test_standalone_op(self):
eng, buf = capture_db()
env = MigrationContext.configure(eng)
op = Operations(env)
op.alter_column("t", "c", nullable=True)
eq_(buf, ["ALTER TABLE t ALTER COLUMN c DROP NOT NULL"])
示例11: setUp
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def setUp(self):
self.conn = self.bind.connect()
ctx = MigrationContext.configure(self.conn)
self.op = Operations(ctx)
示例12: op
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def op(self):
"""Get an alembic operations context."""
ctx = MigrationContext.configure(self.executable)
return Operations(ctx)
示例13: f
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def f(self, name):
"""Indicate a string name that has already had a naming convention
applied to it.
This feature combines with the SQLAlchemy ``naming_convention`` feature
to disambiguate constraint names that have already had naming
conventions applied to them, versus those that have not. This is
necessary in the case that the ``"%(constraint_name)s"`` token
is used within a naming convention, so that it can be identified
that this particular name should remain fixed.
If the :meth:`.Operations.f` is used on a constraint, the naming
convention will not take effect::
op.add_column('t', 'x', Boolean(name=op.f('ck_bool_t_x')))
Above, the CHECK constraint generated will have the name
``ck_bool_t_x`` regardless of whether or not a naming convention is
in use.
Alternatively, if a naming convention is in use, and 'f' is not used,
names will be converted along conventions. If the ``target_metadata``
contains the naming convention
``{"ck": "ck_bool_%(table_name)s_%(constraint_name)s"}``, then the
output of the following:
op.add_column('t', 'x', Boolean(name='x'))
will be::
CONSTRAINT ck_bool_t_x CHECK (x in (1, 0)))
The function is rendered in the output of autogenerate when
a particular constraint name is already converted, for SQLAlchemy
version **0.9.4 and greater only**. Even though ``naming_convention``
was introduced in 0.9.2, the string disambiguation service is new
as of 0.9.4.
.. versionadded:: 0.6.4
"""
if conv:
return conv(name)
else:
raise NotImplementedError(
"op.f() feature requires SQLAlchemy 0.9.4 or greater.")
示例14: f
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def f(self, name):
"""Indicate a string name that has already had a naming convention
applied to it.
This feature combines with the SQLAlchemy ``naming_convention`` feature
to disambiguate constraint names that have already had naming
conventions applied to them, versus those that have not. This is
necessary in the case that the ``"%(constraint_name)s"`` token
is used within a naming convention, so that it can be identified
that this particular name should remain fixed.
If the :meth:`.Operations.f` is used on a constraint, the naming
convention will not take effect::
op.add_column('t', 'x', Boolean(name=op.f('ck_bool_t_x')))
Above, the CHECK constraint generated will have the name
``ck_bool_t_x`` regardless of whether or not a naming convention is
in use.
Alternatively, if a naming convention is in use, and 'f' is not used,
names will be converted along conventions. If the ``target_metadata``
contains the naming convention
``{"ck": "ck_bool_%(table_name)s_%(constraint_name)s"}``, then the
output of the following:
op.add_column('t', 'x', Boolean(name='x'))
will be::
CONSTRAINT ck_bool_t_x CHECK (x in (1, 0)))
The function is rendered in the output of autogenerate when
a particular constraint name is already converted, for SQLAlchemy
version **0.9.4 and greater only**. Even though ``naming_convention``
was introduced in 0.9.2, the string disambiguation service is new
as of 0.9.4.
.. versionadded:: 0.6.4
"""
if conv:
return conv(name)
else:
raise NotImplementedError(
"op.f() feature requires SQLAlchemy 0.9.4 or greater."
)
示例15: downgrade
# 需要導入模塊: from alembic import operations [as 別名]
# 或者: from alembic.operations import Operations [as 別名]
def downgrade(engine, desired_version):
"""Downgrades the assets db at the given engine to the desired version.
Parameters
----------
engine : Engine
An SQLAlchemy engine to the assets database.
desired_version : int
The desired resulting version for the assets database.
"""
# Check the version of the db at the engine
conn = engine.connect()
metadata = sa.MetaData(conn)
metadata.reflect(bind=engine)
version_info_table = metadata.tables['version_info']
starting_version = sa.select((version_info_table.c.version,)).scalar()
# Check for accidental upgrade
if starting_version < desired_version:
raise AssetDBImpossibleDowngrade(db_version=starting_version,
desired_version=desired_version)
# Check if the desired version is already the db version
if starting_version == desired_version:
# No downgrade needed
return
# Create alembic context
ctx = MigrationContext.configure(conn)
op = Operations(ctx)
# Integer keys of downgrades to run
# E.g.: [5, 4, 3, 2] would downgrade v6 to v2
downgrade_keys = range(desired_version, starting_version)[::-1]
# Disable foreign keys until all downgrades are complete
_pragma_foreign_keys(conn, False)
# Execute the downgrades in order
for downgrade_key in downgrade_keys:
_downgrade_methods[downgrade_key](op, version_info_table)
# Re-enable foreign keys
_pragma_foreign_keys(conn, True)