main.py 文件源码

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

项目:appbackendapi 作者: codesdk 项目源码 文件源码
def update_schema_task(cursor=None, num_updated=0, batch_size=100):
    """Task that handles updating the models' schema.

    This is started by
    UpdateSchemaHandler. It scans every entity in the datastore for the
    Picture model and re-saves it so that it has the new schema fields.
    """

    # Force ndb to use v2 of the model by re-loading it.
    reload(models_v2)

    # Get all of the entities for this Model.
    query = models_v2.Picture.query()
    pictures, next_cursor, more = query.fetch_page(
        batch_size, start_cursor=cursor)

    to_put = []
    for picture in pictures:
        # Give the new fields default values.
        # If you added new fields and were okay with the default values, you
        # would not need to do this.
        picture.num_votes = 1
        picture.avg_rating = 5
        to_put.append(picture)

    # Save the updated entities.
    if to_put:
        ndb.put_multi(to_put)
        num_updated += len(to_put)
        logging.info(
            'Put {} entities to Datastore for a total of {}'.format(
                len(to_put), num_updated))

    # If there are more entities, re-queue this task for the next page.
    if more:
        deferred.defer(
            update_schema_task, cursor=next_cursor, num_updated=num_updated)
    else:
        logging.debug(
            'update_schema_task complete with {0} updates!'.format(
                num_updated))
# [END update_schema]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号