Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to introduce required property in GAE

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

like image 937
Brig Avatar asked Feb 16 '11 20:02

Brig


2 Answers

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()
like image 93
systempuntoout Avatar answered Sep 29 '22 03:09

systempuntoout


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.

like image 25
Wooble Avatar answered Sep 29 '22 01:09

Wooble