Neat way to remove obsolete properties from entities?

28 views
Skip to first unread message

Greg

unread,
Apr 5, 2011, 12:17:30 AM4/5/11
to Google App Engine
I've stumbled across what looks like a neat way to remove obsolete
properties from entities, without having to switch models to Expando
and back again. I convert the entity to a protocol buffer, which
reveals the obsolete properties. Then I remove them, convert back to
an entity and save it.

So far my testing has shown this doesn't have any ill effects, but I'd
like feedback in case what I'm doing is dangerous. In particular, I
don't understand what would happen to the indexes for the removed
properties - how does the datastore handle these?

Cheers
Greg.

# !!! EXPERIMENTAL CODE !!!
# !!! USE ENTIRELY AT YOUR OWN RISK !!!
# !!! NOT ENDORSED BY GOOGLE (YET) !!!

from google.appengine.ext import db

def remove_stale_properties(e):
current_properties=e.properties().keys()
epb=db.model_to_protobuf(e)
for i in reversed(range(len(epb.property_))):
if epb.property_[i].name() not in current_properties:
logging.info('Deleting property %s:%s'%
(epb.property_[i].name(),epb.property_[i].value()))
del epb.property_[i]
for i in reversed(range(len(epb.raw_property_))):
if epb.raw_property_[i].name() not in current_properties:
logging.info('Deleting raw property %s:%s'%
(epb.raw_property_[i].name(),epb.raw_property_[i].value()))
del epb.raw_property_[i]
return db.model_from_protobuf(epb)

# Example usage

entity_list=Foo.all().fetch(1000)
for entity in entity_list:
entity=remove_stale_properties(entity)
db.put(entity_list)

Robert Kluin

unread,
Apr 5, 2011, 2:27:08 AM4/5/11
to google-a...@googlegroups.com
Hey Greg,
Assuming you are deleting the property definition from the Model,
then I think you can also use:

your_entity._entity.pop('property_to_delete', None)

It is shorter, and probably a little clearer.


Robert

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Erwin Streur

unread,
Apr 6, 2011, 11:45:27 PM4/6/11
to Google App Engine
Robert,

Are you sure that this is going to work? In GAE there is a significant
difference between a property not being set/not existing at all and
being set to null/None.

Erwin
> > For more options, visit this group athttp://groups.google.com/group/google-appengine?hl=en.- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

Robert Kluin

unread,
Apr 7, 2011, 12:05:56 AM4/7/11
to google-a...@googlegroups.com
Hey Erwin,
Try it. Here's some sample code:

class T(db.Model):
sp = db.StringProperty()
ip = db.IntegerProperty()

T(key_name='test', sp='some str', ip=7).put()

# stop. go look in the datastore viewer.
# now, comment out the def of 'sp' and the put(), then rerun with
test_t = T.get_by_key_name('test')
test_t._entity.pop('sp', None)
test_t.put()

Robert

Reply all
Reply to author
Forward
0 new messages