zoukankan      html  css  js  c++  java
  • 如何在Alembic upgrade脚本中执行插入和更新数据库记录 [Flask] [Sqlalchemy] [Alembic] [python]

    1. 插入数据

    """empty message
    
    Revision ID: c15fcfd8e40a
    Revises: 35ca68348a8d
    Create Date: 2021-01-05 19:39:22.768992
    
    """
    from alembic import op
    import sqlalchemy as sa
    from sqlalchemy.sql import table, column
    
    # revision identifiers, used by Alembic.
    revision = 'c15fcfd8e40a'
    down_revision = '35ca68348a8d'
    branch_labels = None
    depends_on = None
    
    
    def upgrade():
        # ### commands auto generated by Alembic - please adjust! ###
        op.create_table(
            'users',
            sa.Column('id', sa.String(length=45), nullable=False),
            sa.Column('name', sa.String(length=100), nullable=False),
            sa.PrimaryKeyConstraint('id')
        )
    
        # insert records
        user_table = table(
            'users',
            column('id', sa.String),
            column('name', sa.String)
        )
        op.bulk_insert(
            user_table,
            [
                {
                    "id": "123",
                    "name": "Hello"
                },
                {
                    "id": "234",
                    "name": "World"
                },
            ]
        )
    
    
    def downgrade():
        # ### commands auto generated by Alembic - please adjust! ###
        op.drop_table('users')
        # ### end Alembic commands ###
    
    

    2. 更新数据库中的数据

    """empty message
    
    Revision ID: c15fcfd8e40a
    Revises: 35ca68348a8d
    Create Date: 2021-01-05 19:39:22.768992
    
    """
    from alembic import op
    import sqlalchemy as sa
    
    # revision identifiers, used by Alembic.
    revision = 'c15fcfd8e40a'
    down_revision = '35ca68348a8d'
    branch_labels = None
    depends_on = None
    
    
    def upgrade():
        # ### commands auto generated by Alembic - please adjust! ###
        op.create_table(
            'users',
            sa.Column('id', sa.String(length=45), nullable=False),
            sa.Column('name', sa.String(length=100), nullable=False),
            sa.Column('password', sa.String(length=150), nullable=False),
            sa.PrimaryKeyConstraint('id')
        )
        # update records
        bind = op.get_bind()
        for user in bind.execute('select id, name from users'):
            if user[1] is None:
                bind.execute("update users set name = %s where id = '%s'" % ("hello_world", user[0]))
        # ### end Alembic commands ###
    
    def downgrade():
        # ### commands auto generated by Alembic - please adjust! ###
        op.drop_table('users')
        # ### end Alembic commands ###
    
  • 相关阅读:
    使用FreeTextBox等控件带来的问题
    DoNet分页控件
    享元模式学习后总结!(见到别人的总结,解我心中迷惑,认可)
    基于工作流平台的ITSM系统
    .net工作流在移动公司的部署
    转载:When Office 2003 couldn’t find file SKU011.CAB Office 2003 reinstallation error
    联通公司代理商佣金管理
    知识管理系统分析之一:网络蜘蛛的分析
    自我介绍像猪一样生活
    知识管理的整体架构
  • 原文地址:https://www.cnblogs.com/guohewei/p/14975029.html
Copyright © 2011-2022 走看看