当前位置: 首页>>代码示例>>Python>>正文


Python Operations.batch_alter_table方法代码示例

本文整理汇总了Python中alembic.operations.Operations.batch_alter_table方法的典型用法代码示例。如果您正苦于以下问题:Python Operations.batch_alter_table方法的具体用法?Python Operations.batch_alter_table怎么用?Python Operations.batch_alter_table使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在alembic.operations.Operations的用法示例。


在下文中一共展示了Operations.batch_alter_table方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _fixture

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
    def _fixture(self):
        migration_context = mock.Mock(opts={})
        op = Operations(migration_context)
        batch = op.batch_alter_table('tname', recreate='never').__enter__()

        with mock.patch("alembic.operations.sa_schema") as mock_schema:
            yield batch
        batch.impl.flush()
        self.mock_schema = mock_schema
开发者ID:asmundg,项目名称:alembic,代码行数:11,代码来源:test_batch.py

示例2: fix

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
 def fix(self):
     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.name:
                     with op.batch_alter_table(table.name) as batch_op:
                         batch_op.add_column(column.copy())
                     return
开发者ID:wenLiangcan,项目名称:cum,代码行数:12,代码来源:sanity.py

示例3: _fixture

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
    def _fixture(self, schema=None):
        migration_context = mock.Mock(
            opts={}, impl=mock.MagicMock(__dialect__='sqlite'))
        op = Operations(migration_context)
        batch = op.batch_alter_table(
            'tname', recreate='never', schema=schema).__enter__()

        mock_schema = mock.MagicMock()
        with mock.patch("alembic.operations.schemaobj.sa_schema", mock_schema):
            yield batch
        batch.impl.flush()
        self.mock_schema = mock_schema
开发者ID:assembl,项目名称:alembic,代码行数:14,代码来源:test_batch.py

示例4: BatchRoundTripTest

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
class BatchRoundTripTest(TestBase):
    __requires__ = ('sqlalchemy_08', )
    __only_on__ = "sqlite"

    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)

    def _no_pk_fixture(self):
        nopk = Table(
            'nopk', self.metadata,
            Column('a', Integer),
            Column('b', Integer),
            Column('c', Integer),
            mysql_engine='InnoDB'
        )
        nopk.create(self.conn)
        self.conn.execute(
            nopk.insert(),
            [
                {"a": 1, "b": 2, "c": 3},
                {"a": 2, "b": 4, "c": 5},
            ]

        )
        return nopk

    def tearDown(self):
        self.metadata.drop_all(self.conn)
        self.conn.close()

    def _assert_data(self, data, tablename='foo'):
        eq_(
            [dict(row) for row
             in self.conn.execute("select * from %s" % tablename)],
            data
        )

    def test_fk_points_to_me_auto(self):
        self._test_fk_points_to_me("auto")

    # in particular, this tests that the failures
    # on PG and MySQL result in recovery of the batch system,
    # e.g. that the _alembic_batch_temp table is dropped
    @config.requirements.no_referential_integrity
    def test_fk_points_to_me_recreate(self):
        self._test_fk_points_to_me("always")

    def _test_fk_points_to_me(self, recreate):
        bar = Table(
            'bar', self.metadata,
            Column('id', Integer, primary_key=True),
            Column('foo_id', Integer, ForeignKey('foo.id')),
            mysql_engine='InnoDB'
        )
        bar.create(self.conn)
        self.conn.execute(bar.insert(), {'id': 1, 'foo_id': 3})

        with self.op.batch_alter_table("foo", recreate=recreate) as batch_op:
            batch_op.alter_column(
                'data', new_column_name='newdata', existing_type=String(50))

    def test_change_type(self):
        with self.op.batch_alter_table("foo") as batch_op:
            batch_op.alter_column('data', type_=Integer)

        self._assert_data([
            {"id": 1, "data": 0, "x": 5},
            {"id": 2, "data": 22, "x": 6},
            {"id": 3, "data": 8, "x": 7},
            {"id": 4, "data": 9, "x": 8},
            {"id": 5, "data": 0, "x": 9}
        ])

    def test_drop_column(self):
        with self.op.batch_alter_table("foo") as batch_op:
            batch_op.drop_column('data')

        self._assert_data([
#.........这里部分代码省略.........
开发者ID:assembl,项目名称:alembic,代码行数:103,代码来源:test_batch.py

示例5: CopyFromTest

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
class CopyFromTest(TestBase):
    __requires__ = ('sqlalchemy_08', )

    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

    def test_change_type(self):
        context = self._fixture()
        with self.op.batch_alter_table(
                "foo", copy_from=self.table) as batch_op:
            batch_op.alter_column('data', type_=Integer)

        context.assert_(
            'CREATE TABLE _alembic_batch_temp (id INTEGER NOT NULL, '
            'data INTEGER, x INTEGER, PRIMARY KEY (id))',
            'INSERT INTO _alembic_batch_temp (id, data, x) SELECT foo.id, '
            'CAST(foo.data AS INTEGER) AS anon_1, foo.x FROM foo',
            'DROP TABLE foo',
            'ALTER TABLE _alembic_batch_temp RENAME TO foo'
        )

    def test_create_drop_index_w_always(self):
        context = self._fixture()
        with self.op.batch_alter_table(
                "foo", copy_from=self.table, recreate='always') as batch_op:
            batch_op.create_index(
                'ix_data', ['data'], unique=True)

        context.assert_(
            'CREATE TABLE _alembic_batch_temp (id INTEGER NOT NULL, '
            'data VARCHAR(50), '
            'x INTEGER, PRIMARY KEY (id))',
            'CREATE UNIQUE INDEX ix_data ON _alembic_batch_temp (data)',
            'INSERT INTO _alembic_batch_temp (id, data, x) '
            'SELECT foo.id, foo.data, foo.x FROM foo',
            'DROP TABLE foo',
            'ALTER TABLE _alembic_batch_temp RENAME TO foo'
        )

        context.clear_assertions()

        Index('ix_data', self.table.c.data, unique=True)
        with self.op.batch_alter_table(
                "foo", copy_from=self.table, recreate='always') as batch_op:
            batch_op.drop_index('ix_data')

        context.assert_(
            'CREATE TABLE _alembic_batch_temp (id INTEGER NOT NULL, '
            'data VARCHAR(50), x INTEGER, PRIMARY KEY (id))',
            'INSERT INTO _alembic_batch_temp (id, data, x) '
            'SELECT foo.id, foo.data, foo.x FROM foo',
            'DROP TABLE foo',
            'ALTER TABLE _alembic_batch_temp RENAME TO foo'
        )

    def test_create_drop_index_wo_always(self):
        context = self._fixture()
        with self.op.batch_alter_table(
                "foo", copy_from=self.table) as batch_op:
            batch_op.create_index(
                'ix_data', ['data'], unique=True)

        context.assert_(
            'CREATE UNIQUE INDEX ix_data ON foo (data)'
        )

        context.clear_assertions()

        Index('ix_data', self.table.c.data, unique=True)
        with self.op.batch_alter_table(
                "foo", copy_from=self.table) as batch_op:
            batch_op.drop_index('ix_data')

        context.assert_(
            'DROP INDEX ix_data'
        )

    def test_create_drop_index_w_other_ops(self):
        context = self._fixture()
        with self.op.batch_alter_table(
                "foo", copy_from=self.table) as batch_op:
            batch_op.alter_column('data', type_=Integer)
            batch_op.create_index(
                'ix_data', ['data'], unique=True)

        context.assert_(
            'CREATE TABLE _alembic_batch_temp (id INTEGER NOT NULL, '
            'data INTEGER, x INTEGER, PRIMARY KEY (id))',
            'CREATE UNIQUE INDEX ix_data ON _alembic_batch_temp (data)',
            'INSERT INTO _alembic_batch_temp (id, data, x) SELECT foo.id, '
#.........这里部分代码省略.........
开发者ID:assembl,项目名称:alembic,代码行数:103,代码来源:test_batch.py

示例6: BatchRoundTripTest

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
class BatchRoundTripTest(TestBase):
    __requires__ = ('sqlalchemy_08', )
    __only_on__ = "sqlite"

    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)

    def tearDown(self):
        self.metadata.drop_all(self.conn)
        self.conn.close()

    def _assert_data(self, data):
        eq_(
            [dict(row) for row in self.conn.execute("select * from foo")],
            data
        )

    def test_fk_points_to_me_auto(self):
        self._test_fk_points_to_me("auto")

    # in particular, this tests that the failures
    # on PG and MySQL result in recovery of the batch system,
    # e.g. that the _alembic_batch_temp table is dropped
    @config.requirements.no_referential_integrity
    def test_fk_points_to_me_recreate(self):
        self._test_fk_points_to_me("always")

    def _test_fk_points_to_me(self, recreate):
        bar = Table(
            'bar', self.metadata,
            Column('id', Integer, primary_key=True),
            Column('foo_id', Integer, ForeignKey('foo.id')),
            mysql_engine='InnoDB'
        )
        bar.create(self.conn)
        self.conn.execute(bar.insert(), {'id': 1, 'foo_id': 3})

        with self.op.batch_alter_table("foo", recreate=recreate) as batch_op:
            batch_op.alter_column(
                'data', new_column_name='newdata', existing_type=String(50))

    def test_change_type(self):
        with self.op.batch_alter_table("foo") as batch_op:
            batch_op.alter_column('data', type_=Integer)

        self._assert_data([
            {"id": 1, "data": 0, "x": 5},
            {"id": 2, "data": 22, "x": 6},
            {"id": 3, "data": 8, "x": 7},
            {"id": 4, "data": 9, "x": 8},
            {"id": 5, "data": 0, "x": 9}
        ])

    def test_drop_column(self):
        with self.op.batch_alter_table("foo") as batch_op:
            batch_op.drop_column('data')

        self._assert_data([
            {"id": 1, "x": 5},
            {"id": 2, "x": 6},
            {"id": 3, "x": 7},
            {"id": 4, "x": 8},
            {"id": 5, "x": 9}
        ])

    def test_drop_column_fk_recreate(self):
        with self.op.batch_alter_table("foo", recreate='always') as batch_op:
            batch_op.drop_column('data')

        self._assert_data([
            {"id": 1, "x": 5},
            {"id": 2, "x": 6},
            {"id": 3, "x": 7},
            {"id": 4, "x": 8},
            {"id": 5, "x": 9}
        ])

    def test_rename_column(self):
#.........这里部分代码省略.........
开发者ID:asmundg,项目名称:alembic,代码行数:103,代码来源:test_batch.py

示例7: BatchRoundTripTest

# 需要导入模块: from alembic.operations import Operations [as 别名]
# 或者: from alembic.operations.Operations import batch_alter_table [as 别名]
class BatchRoundTripTest(TestBase):
    __requires__ = ('sqlalchemy_08', )
    __only_on__ = "sqlite"

    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)

    @contextmanager
    def _sqlite_referential_integrity(self):
        self.conn.execute("PRAGMA foreign_keys=ON")
        try:
            yield
        finally:
            self.conn.execute("PRAGMA foreign_keys=OFF")

    def _no_pk_fixture(self):
        nopk = Table(
            'nopk', self.metadata,
            Column('a', Integer),
            Column('b', Integer),
            Column('c', Integer),
            mysql_engine='InnoDB'
        )
        nopk.create(self.conn)
        self.conn.execute(
            nopk.insert(),
            [
                {"a": 1, "b": 2, "c": 3},
                {"a": 2, "b": 4, "c": 5},
            ]

        )
        return nopk

    def _table_w_index_fixture(self):
        t = Table(
            't_w_ix', self.metadata,
            Column('id', Integer, primary_key=True),
            Column('thing', Integer),
            Column('data', String(20)),
        )
        Index('ix_thing', t.c.thing)
        t.create(self.conn)
        return t

    def _boolean_fixture(self):
        t = Table(
            'hasbool', self.metadata,
            Column('x', Boolean(create_constraint=True, name='ck1')),
            Column('y', Integer)
        )
        t.create(self.conn)

    def _timestamp_fixture(self):
        t = Table(
            'hasts', self.metadata,
            Column('x', DateTime()),
        )
        t.create(self.conn)
        return t

    def _int_to_boolean_fixture(self):
        t = Table(
            'hasbool', self.metadata,
            Column('x', Integer)
        )
        t.create(self.conn)

    def test_change_type_boolean_to_int(self):
        self._boolean_fixture()
        with self.op.batch_alter_table(
                "hasbool"
        ) as batch_op:
            batch_op.alter_column(
                'x', type_=Integer, existing_type=Boolean(
                    create_constraint=True, name='ck1'))
        insp = Inspector.from_engine(config.db)

#.........这里部分代码省略.........
开发者ID:RazerM,项目名称:alembic,代码行数:103,代码来源:test_batch.py


注:本文中的alembic.operations.Operations.batch_alter_table方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。