This article, “Updating Your Model's Schema,” is already great and clear, but it does not have a complete code example. I decided to make one and write down some explanations. Just in case I might need it later.
It has one two stages to remove a property from a data model:
Assume a model look like:
Re-define the model to:
Make sure the model inherit from db.Expando and comment out (or just delete the line) the obsolete property.
Here is the example code to delete the attribute, the property:
Note that this snippet is to be used as a webapp.RequestHandler's get method, so it has self.response.
It use entities' keys to walk through every entity, it is efficient and safe. But you may also want to put your application under maintenance, preventing other code to add new entities, even though the values of keys seem to be increased only for new entities, but you really don't need to waste CPU time since new entities has no obsolete property.
Because it have to go through all entities and therefore it takes a lot of time to process. A mechanism to continue the process on the rest of entities is necessary. The code will catch google.appengine.runtime.DeadlineExceededError if it can not finish in one request, it then return a link which allows you to continue if you follow it. If you have lots of entities, you may want to use task instead of manual continuation. You may also want to set up the maximal amount of processing entities like 1000 entities in one request.
Once it has done its job, change the model definition back to db.Model and remove obsolete property line:
That's it.
It has one two stages to remove a property from a data model:
- Inherit from db.Expando if the model does not inherit from that.
- Remove the obsolete property from model definition.
- Delete the attribute, the property, of each entity — del entity.obsolete
- Inherit from db.Model if the model originally inherited from.
How to actually do it:
Assume a model look like:
class MyModel(db.Model):
foo = db.TextProperty()
obsolete = db.TextProperty()
Re-define the model to:
class MyModel(db.Expando):
#class MyModel(db.Model):
foo = db.TextProperty()
# obsolete = db.TextProperty()
Make sure the model inherit from db.Expando and comment out (or just delete the line) the obsolete property.
Here is the example code to delete the attribute, the property:
from google.appengine.runtime import DeadlineExceededError
def del_obsolete(self):
count = 0
last_key = ''
try:
q = MyModel.all()
cont = self.request.get('continue')
if cont:
q.filter('__key__ >=', db.Key(cont))
q.order('__key__')
entities = q.fetch(100)
while entities:
for entity in entities:
last_key = str(entity.key())
try:
del entity.obsolete
except AttributeError:
pass
entity.put()
count += 1
q.filter('__key__ >', entities[-1].key())
entities = q.fetch(100)
except DeadlineExceededError:
self.response.out.write('%d processed, please continue to %s?continue=%s' % (count, self.request.path_url, last_key))
return
self.response.out.write('%d processed, all done.' % count)
Note that this snippet is to be used as a webapp.RequestHandler's get method, so it has self.response.
It use entities' keys to walk through every entity, it is efficient and safe. But you may also want to put your application under maintenance, preventing other code to add new entities, even though the values of keys seem to be increased only for new entities, but you really don't need to waste CPU time since new entities has no obsolete property.
Because it have to go through all entities and therefore it takes a lot of time to process. A mechanism to continue the process on the rest of entities is necessary. The code will catch google.appengine.runtime.DeadlineExceededError if it can not finish in one request, it then return a link which allows you to continue if you follow it. If you have lots of entities, you may want to use task instead of manual continuation. You may also want to set up the maximal amount of processing entities like 1000 entities in one request.
Once it has done its job, change the model definition back to db.Model and remove obsolete property line:
class MyModel(db.Model):
foo = db.TextProperty()
That's it.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.