61e62ef50513_add_transfer_relationships_to_.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:biweeklybudget 作者: jantman 项目源码 文件源码
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        'transactions',
        sa.Column('transfer_id', sa.Integer(), nullable=True)
    )
    op.create_foreign_key(
        op.f('fk_transactions_transfer_id_transactions'),
        'transactions',
        'transactions',
        ['transfer_id'],
        ['id']
    )
    bind = op.get_bind()
    session = Session(bind=bind)
    # begin data manipulation
    last_txn = None
    for txn in session.query(Transaction).filter(
        Transaction.description.like('Budget Transfer - %')
    ).order_by(Transaction.id.asc()).all():
        if last_txn is None:
            last_txn = txn
            continue
        if (
            txn.description == last_txn.description and
            txn.date == last_txn.date and
            txn.notes == last_txn.notes and
            txn.account_id == last_txn.account_id
        ):
            # txn and last_txn are a transfer
            last_txn.transfer_id = txn.id
            txn.transfer_id = last_txn.id
            session.add(txn)
            session.add(last_txn)
            logger.warning(
                'Inferred Transfer relationship between Transactions %d and %d',
                last_txn.id, txn.id
            )
            last_txn = None
            continue
        last_txn = txn
    session.commit()
    # ### end Alembic commands ###
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号