views:

45

answers:

1

I have an existing schema:

class Example (db.Model) :
 row_num = db.IntegerProperty(required=True)
 updated = db.IntegerProperty()
 ...
 ...

I have now updated this to :

class Example (db.Model) :
 row_num = db.IntegerProperty(required=True)
 updated = db.IntegerProperty(default=0)
 ...
 ...

However, there are over 2 million entities in the Datastore which do not have update = 0 set by default.

What is the easiest way to do this? Can this by done by a single command from the admin terminal?

+2  A: 

You'll need to write a script that iterates through the objects, grabbing them (up to 1000 at a time), updating their property value, and then saving them back.

No, this is not really efficient relative to a standard SQL DB doing the same kind of thing (where you could just issue a single UPDATE), but BigTable (the backing technology behind the GAE Datastore) is not a SQL relational database - it's an entirely different architecture designed to be good at different things and not optimized for updating a single field across millions of rows at a time - hence why GQL syntax has no notion of an UPDATE statement.

Edit:

As David kindly pointed out in comments, Google recently released the Mapper API which can be used to assist with this.

Amber
I wish the appengine guys had created some wrappers for all these common tasks and implemented them in the most optimized way. Would have saved us a all a lot of time.
demos
Wrapping them up might mislead people as to how many resources are going to be used up in what seems like a "simple" call. Basically what it comes down to is that BigTable is a lot different from RDBs.
Amber
The GAE team recently released the [Mapper API](http://googleappengine.blogspot.com/2010/07/introducing-mapper-api.html) which can be used for large-scale schema migrations like this.
David Underhill
The Mapper API is the go-to tool for this. You don't even need to explicitly set the value, since it's a default: Just write a trivial mapper that writes every entity back to the datastore without explicit modifications, and the default value will be set automatically.
Nick Johnson