Python alembic.op 模块,batch_alter_table() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用alembic.op.batch_alter_table()

项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'environment_hierarchy_level_value'
    with op.batch_alter_table(table_name) as batch:
        batch.drop_column('parent_id')

        batch.drop_constraint(
            table_name + '_level_id_fkey',
            type_='foreignkey'
        )
        batch.create_foreign_key(
            table_name + '_level_id_fkey',
            table_prefix + 'environment_hierarchy_level',
            ['level_id'], ['id'], ondelete='CASCADE'
        )

        batch.create_unique_constraint(
            table_name + '_level_id_value_unique',
            ['level_id', 'value']
        )
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    op.drop_table(table_prefix + 'template')
    table_name = table_prefix + 'environment_schema_values'
    with op.batch_alter_table(table_name) as batch:
        batch.drop_constraint(table_name + '_schema_id_fkey', 'foreignkey')
        batch.alter_column(
            'schema_id',
            new_column_name='resource_definition_id',
            existing_type=sa.Integer(),
        )
    op.rename_table(table_name, table_prefix + 'resource_values')
    op.rename_table(table_prefix + 'schema',
                    table_prefix + 'resource_definition')
    with op.batch_alter_table(table_prefix + 'resource_definition') as batch:
        batch.drop_column('namespace_id')
    op.drop_table(table_prefix + 'namespace')
    table_name = table_prefix + 'resource_values'
    with op.batch_alter_table(table_name) as batch:
        batch.create_foreign_key(
            table_name + '_resource_definition_id_fkey',
            table_prefix + 'resource_definition',
            ['resource_definition_id'],
            ['id'],
        )
项目:flask-celery3-boilerplate    作者:sdg32    | 项目源码 | 文件源码
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('schedule_info',
    sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
    sa.Column('last_changed_at', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('schedule_meta',
    sa.Column('parent_id', sa.Integer(), nullable=False),
    sa.Column('last_run_at', sa.DateTime(), nullable=True),
    sa.Column('total_run_count', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['parent_id'], ['schedule_task.id'], ),
    sa.PrimaryKeyConstraint('parent_id')
    )
    with op.batch_alter_table('schedule_task') as batch_op:
        batch_op.drop_column('total_run_count')
        batch_op.drop_column('last_run_at')
    # ### end Alembic commands ###
项目:Callandtext    作者:iaora    | 项目源码 | 文件源码
def create_foreign_key(
            self, name, referent, local_cols, remote_cols, **kw):
        """Issue a "create foreign key" instruction using the
        current batch migration context.

        The batch form of this call omits the ``source`` and ``source_schema``
        arguments from the call.

        e.g.::

            with batch_alter_table("address") as batch_op:
                batch_op.create_foreign_key(
                            "fk_user_address",
                            "user", ["user_id"], ["id"])

        .. seealso::

            :meth:`.Operations.create_foreign_key`

        """
        return super(BatchOperations, self).create_foreign_key(
            name, self.impl.table_name, referent, local_cols, remote_cols,
            source_schema=self.impl.schema, **kw)
项目:cci-demo-flask    作者:circleci    | 项目源码 | 文件源码
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'last_name')
    op.drop_column('users', 'first_name')
    ### end Alembic commands ###
    with op.batch_alter_table("notes") as batch_op:
        batch_op.drop_constraint(
            "notes_author_id_fkey", type_="foreignkey")
    with op.batch_alter_table("notebooks") as batch_op:
        batch_op.drop_constraint(
            "notebooks_author_id_fkey", type_="foreignkey")
    op.create_foreign_key(
        "notes_author_id_fkey", "notes", "users", 
        ["author_id"], ["id"], ondelete="CASCADE")
    op.create_foreign_key(
        "notebooks_author_id_fkey", "notebooks", "users", 
        ["author_id"], ["id"], ondelete="CASCADE")
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'component'
    with op.batch_alter_table(table_name) as batch:
        batch.create_unique_constraint(
            table_name + '_component_name_unique',
            ['name'],
        )
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def downgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'component'
    with op.batch_alter_table(table_name) as batch:
        batch.drop_constraint(
            table_name + '_component_name_unique',
            type_='unique',
        )
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'resource_values'
    with op.batch_alter_table(table_name) as batch:
        batch.alter_column(
            'values',
            server_default='{}',
            existing_type=tuning_box.db.Json(),
        )
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def downgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'resource_values'
    with op.batch_alter_table(table_name) as batch:
        batch.alter_column(
            'values',
            server_default=None,
            existing_type=tuning_box.db.Json(),
        )
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'environment_hierarchy_level'

    with op.batch_alter_table(table_name) as batch:
        batch.drop_constraint(
            table_prefix + 'environment_hierarchy_level_environment_id_fkey',
            type_='foreignkey'
        )
        batch.create_foreign_key(
            table_prefix + 'environment_hierarchy_level_environment_id_fkey',
            table_prefix + 'environment',
            ['environment_id'], ['id'], ondelete='CASCADE'
        )
项目:tuning-box    作者:openstack    | 项目源码 | 文件源码
def downgrade():
    table_prefix = context.config.get_main_option('table_prefix')
    table_name = table_prefix + 'environment_hierarchy_level'
    with op.batch_alter_table(table_name) as batch:
        batch.drop_constraint(
            table_prefix + 'environment_hierarchy_level_environment_id_fkey',
            type_='foreignkey'
        )
        batch.create_foreign_key(
            table_prefix + 'environment_hierarchy_level_environment_id_fkey',
            table_prefix + 'environment',
            ['environment_id'], ['id']
        )
项目:akamatsu    作者:rmed    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('pages') as batch_op:
        batch_op.drop_column('is_html')
        batch_op.drop_column('use_layout_header')
项目:akamatsu    作者:rmed    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('pages') as batch_op:
            batch_op.add_column(
                sa.Column('is_html', sa.Boolean(), nullable=True)
            )
            batch_op.add_column(
                sa.Column('use_layout_header', sa.Boolean(), nullable=True)
            )
项目:league    作者:massgo    | 项目源码 | 文件源码
def upgrade():
    """Upgrade database."""
    # The following is a ridiculous hack to force table recreation for SQLite to
    # enable the use of a default timestamp.
    recreate = 'auto'
    migrate_context = context.get_context()
    sqlite_dialect_class = None
    if getattr(sa.dialects, 'sqlite', False):
        sqlite_dialect_class = (sa.dialects.sqlite.pysqlite
                                .SQLiteDialect_pysqlite)
    if migrate_context.dialect.__class__ == sqlite_dialect_class:
        recreate = 'always'
    with op.batch_alter_table('games', recreate=recreate) as batch_op:
        batch_op.add_column(sa.Column('played_at', sa.DateTime(),
                            nullable=False, server_default=sa.func.now()))
项目:league    作者:massgo    | 项目源码 | 文件源码
def downgrade():
    """Downgrade database."""
    with op.batch_alter_table('games') as batch_op:
        batch_op.drop_column('played_at')
项目:league    作者:massgo    | 项目源码 | 文件源码
def upgrade():
    """Upgrade database."""
    # The following is a ridiculous hack to force table recreation for SQLite to
    # enable the use of a default timestamp.
    recreate = 'auto'
    migrate_context = context.get_context()
    sqlite_dialect_class = None
    if getattr(sa.dialects, 'sqlite', False):
        sqlite_dialect_class = (sa.dialects.sqlite.pysqlite
                                .SQLiteDialect_pysqlite)
    if migrate_context.dialect.__class__ == sqlite_dialect_class:
        recreate = 'always'
    with op.batch_alter_table('games', recreate=recreate) as batch_op:
        batch_op.add_column(sa.Column('last_modified_at', sa.DateTime(),
                            nullable=False, server_default=sa.func.now()))
项目:league    作者:massgo    | 项目源码 | 文件源码
def downgrade():
    """Downgrade database."""
    with op.batch_alter_table('games') as batch_op:
        batch_op.drop_column('last_modified_at')
项目:league    作者:massgo    | 项目源码 | 文件源码
def upgrade():
    """Upgrade database."""
    # The following is a ridiculous hack to force table recreation for SQLite to
    # enable the use of a default timestamp.
    recreate = 'auto'
    migrate_context = context.get_context()
    sqlite_dialect_class = None
    if getattr(sa.dialects, 'sqlite', False):
        sqlite_dialect_class = (sa.dialects.sqlite.pysqlite
                                .SQLiteDialect_pysqlite)
    if migrate_context.dialect.__class__ == sqlite_dialect_class:
        recreate = 'always'
    with op.batch_alter_table('games', recreate=recreate) as batch_op:
        batch_op.add_column(sa.Column('created_at', sa.DateTime(),
                            nullable=False, server_default=sa.func.now()))
项目:zun    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('container', schema=None) as batch_op:
        batch_op.add_column(sa.Column('interactive', sa.Boolean(),
                                      nullable=True))
        batch_op.drop_column('tty')
        batch_op.drop_column('stdin_open')
项目:mensa-tracker    作者:annyanich    | 项目源码 | 文件源码
def upgrade():
    # batch_alter_table() is necessary here because SQLite does not support
    # the SQL ALTER statement which would normally be emitted here.

    # What this function does is basically cause all the data in the old
    # version of the table to be copied into a new table, then delete the old
    # table at the end if everything works as planned.
    with op.batch_alter_table('menu_entries') as batch_op:
        batch_op.create_unique_constraint(
            constraint_name="unique_menu_entry_date_mensa_category_description",
            columns=['date_valid', 'mensa', 'category', 'description'])
项目:mensa-tracker    作者:annyanich    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('menu_entries') as batch_op:
        batch_op.drop_constraint(
            constraint_name="unique_menu_entry_date_mensa_category_description",
            type_='unique')
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def upgrade():
    # There can be data truncation here as LargeBinary can be smaller than the pickle 
    # type.

    # use batch_alter_table to support SQLite workaround
    with op.batch_alter_table("xcom") as batch_op:
        batch_op.alter_column('value', type_=sa.LargeBinary())
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def downgrade():
    # use batch_alter_table to support SQLite workaround
    with op.batch_alter_table("xcom") as batch_op:
        batch_op.alter_column('value', type_=sa.PickleType(pickler=dill))
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
def upgrade():
    # use batch_alter_table to support SQLite workaround
    with op.batch_alter_table("task_instance") as batch_op:
        batch_op.alter_column('duration',
                              existing_type=mysql.INTEGER(display_width=11),
                              type_=sa.Float(),
                              existing_nullable=True)
项目:Adventure-Insecure    作者:colinnewell    | 项目源码 | 文件源码
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.add_column(sa.Column('ldap_auth', sa.Boolean(), server_default=sa.text('false'), nullable=False))

    ### end Alembic commands ###
项目:Adventure-Insecure    作者:colinnewell    | 项目源码 | 文件源码
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.drop_column('ldap_auth')

    ### end Alembic commands ###
项目:Adventure-Insecure    作者:colinnewell    | 项目源码 | 文件源码
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('menus', schema=None) as batch_op:
        batch_op.drop_column('location')

    ### end Alembic commands ###
项目:Adventure-Insecure    作者:colinnewell    | 项目源码 | 文件源码
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('menus', schema=None) as batch_op:
        batch_op.add_column(sa.Column('location', sa.VARCHAR(length=256), nullable=False))

    ### end Alembic commands ###
项目:quark    作者:openstack    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table(t1_name) as batch_op:
        batch_op.add_column(sa.Column('enabled',
                                      sa.Boolean(),
                                      nullable=False,
                                      server_default='1'))

    with op.batch_alter_table(t2_name) as batch_op:
        batch_op.add_column(sa.Column('do_not_use',
                                      sa.Boolean(),
                                      nullable=False,
                                      server_default='0'))
项目:quark    作者:openstack    | 项目源码 | 文件源码
def downgrade():
    """alexm: i believe this method is never called"""
    with op.batch_alter_table(t2_name) as batch_op:
        batch_op.drop_column('do_not_use')

    with op.batch_alter_table(t1_name) as batch_op:
        batch_op.drop_column('enabled')
项目:ltd-keeper    作者:lsst-sqre    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('editions', schema=None) as batch_op:
        batch_op.add_column(sa.Column('mode', sa.Integer(), nullable=True))
项目:ltd-keeper    作者:lsst-sqre    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('editions', schema=None) as batch_op:
        batch_op.drop_column('mode')
项目:ltd-keeper    作者:lsst-sqre    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('products', schema=None) as batch_op:
        batch_op.add_column(sa.Column('surrogate_key',
                                      sa.String(length=32),
                                      nullable=True))
项目:flask-react-redux-demo    作者:eddowh    | 项目源码 | 文件源码
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('restaurants_menuitem', schema=None) as batch_op:
        batch_op.alter_column('price',
               existing_type=sa.NUMERIC(precision=2, scale=0),
               type_=sa.Integer(),
               existing_nullable=False)

    ### end Alembic commands ###
项目:flask-react-redux-demo    作者:eddowh    | 项目源码 | 文件源码
def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('restaurants_menuitem', schema=None) as batch_op:
        batch_op.alter_column('price',
               existing_type=sa.Integer(),
               type_=sa.NUMERIC(precision=2, scale=0),
               existing_nullable=False)

    ### end Alembic commands ###
项目:airflow    作者:apache-airflow    | 项目源码 | 文件源码
def upgrade():
    # use batch_alter_table to support SQLite workaround
    with op.batch_alter_table("task_instance") as batch_op:
        batch_op.alter_column('duration',
                              existing_type=mysql.INTEGER(display_width=11),
                              type_=sa.Float(),
                              existing_nullable=True)
项目:todoist_bot    作者:ihoru    | 项目源码 | 文件源码
def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user') as batch_op:
        batch_op.drop_column('state')
        batch_op.drop_column('last_active_at')
        batch_op.drop_column('is_active')
        # ### end Alembic commands ###
项目:collectors    作者:opentrials    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('fda_dap') as batch_op:
        batch_op.add_column(sa.Column('drug_name', sa.Text))
        batch_op.add_column(sa.Column('active_ingredients', sa.Text))
        batch_op.add_column(sa.Column('company', sa.Text))
项目:collectors    作者:opentrials    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('fda_dap') as batch_op:
        batch_op.drop_column('drug_name')
        batch_op.drop_column('active_ingredients')
        batch_op.drop_column('company')
项目:kostyor    作者:Mirantis    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('upgrade_tasks') as batch_op:
        batch_op.add_column(
            sa.Column('status', sa.Enum(*constants.UPGRADE_STATUSES)))
项目:kostyor    作者:Mirantis    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('upgrade_tasks') as batch_op:
        batch_op.drop_column('status')
项目:kostyor    作者:Mirantis    | 项目源码 | 文件源码
def upgrade():
    op.create_table(
        'hosts_services',
        sa.Column('host_id', sa.String(36), sa.ForeignKey('hosts.id')),
        sa.Column('service_id', sa.String(36), sa.ForeignKey('services.id')),
        sa.UniqueConstraint('host_id', 'service_id'))

    with op.batch_alter_table('services') as batch_op:
        batch_op.drop_constraint('fk_services_host_id_hosts')
        batch_op.drop_column('host_id')
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def upgrade():
    connection = op.get_bind()
    # spam_threshold is a X/15 based value, we're converting it to percent.
    for user in connection.execute(user_table.select()):
         connection.execute(
            user_table.update().where(
                user_table.c.email == user.email
            ).values(
                spam_threshold=int(100. * float(user.spam_threshold or 0.) / 15.)
            )
         )
    # set default to 80%
    with op.batch_alter_table('user') as batch:
        batch.alter_column('spam_threshold', default=80.)
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def downgrade():
    connection = op.get_bind()
    # spam_threshold is a X/15 based value, we're converting it from percent.
    for user in connection.execute(user_table.select()):
         connection.execute(
            user_table.update().where(
                user_table.c.email == user.email
            ).values(
                spam_threshold=int(15. * float(user.spam_threshold or 0.) / 100.)
            )
         )
    # set default to 10/15
    with op.batch_alter_table('user') as batch:
        batch.alter_column('spam_threshold', default=10.)
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('domain') as batch:
        batch.drop_column('max_quota_bytes')
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('user') as batch:
        batch.add_column(sa.Column('forward_keep', sa.Boolean(), nullable=False, server_default=sa.sql.expression.true()))
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('user') as batch:
        batch.drop_column('forward_keep')
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def upgrade():
    with op.batch_alter_table('user') as batch:
        batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE"))
    with op.batch_alter_table('alias') as batch:
        batch.alter_column('email', type_=sa.String(length=255, collation="NOCASE"))
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('user') as batch:
        batch.alter_column('email', type_=sa.String(length=255))
    with op.batch_alter_table('alias') as batch:
        batch.alter_column('email', type_=sa.String(length=255))
项目:Mailu    作者:Mailu    | 项目源码 | 文件源码
def downgrade():
    with op.batch_alter_table('fetch') as batch:
        batch.drop_column('keep')