开发者

How to introduce required property in GAE

开发者 https://www.devze.com 2023-02-11 17:43 出处:网络
I\'ve changed my object to have a new required property in v2. When I attempt to fetch a v1 object from the datastore, I get BadValueError because v1 doesn\'t have the required property. What\'s the b

I've changed my object to have a new required property in v2. When I attempt to fetch a v1 object from the datastore, I get BadValueError because v1 doesn't have the required property. What's the best way to introduce new required开发者_运维知识库 properties on existing data


I would resolve this problem using the mapreduce library.

First, register the mapper in mapreduce.yaml:

mapreduce:
- name: fixing required property
  mapper:
    input_reader: mapreduce.input_readers.DatastoreInputReader
    handler: your handler
    params:
    - name: entity_kind
      default: main.ModelV2

then define a process function to modify the entities:

from mapreduce import operation as op
def process(entity):
 if not entity.newproperty :
    entity.newproperty = None
 yield op.db.Put(entity)

If you are dealing with a relative small number of entities, you could avoid mapreduce modifying directly your entities with something like this:

entities = ModelV2.all()
for entity in entities :
  if not entity.newproperty :
    entity.newproperty = None
    entity.put()


You'll need to add it as an optional property to your model, fetch each existing entity, add the property to it (generating a reasonable value somehow), then put() the entity. Once all of your existing entities have been "upgraded", you can make the property required.

The AppEngine mapreduce API should make this fairly easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消