How to update the NDB model schema

I have seen a solution to this question using the "DB Datastore API" App Engine older, but cannot find a solution when using the newer NDB .

What is the best way to add migration support so that I can migrate from the old version of the schema to the new version. Would it be better to write a migration script , and how would that work?

Something like migrating a schema like this (Note that the sample is in the NDB ):

class Picture(ndb.Model):
    author = ndb.StringProperty()
    png_data = ndb.BlobProperty()
    name = ndb.StringProperty(default='')  # Unique name.

      

To the updated one, for example:

class Picture(ndb.Model):
    author = ndb.StringProperty()
    png_data = ndb.BlobProperty()
    name = ndb.StringProperty(default='')  # Unique name.

    num_votes = ndb.IntegerProperty(default=0)
    avg_rating = ndb.FloatProperty(default=0)

      

Many thanks!

+3


source to share


1 answer


As far as I know, NDB does not support native schema migration support. We handle schema migration as follows:

  • Add an NDB integer property called schema_version

    to each used model. This will be the version of the schema that was active when the model was saved.
  • Add a class attribute schema_version

    to each model that represents the current version of the schema code. By default, it is 0, and we increase this every time we add / remove / change NDB properties on the model.
  • Ask our models to implement a method updateSchema

    that can view saved and current versions and perform appropriate migrations.
  • When loading objects, we check if the version of the entity schema we loaded is out of date. If so, we call updateSchema

    to fix the entity before we return it from our data layer. Then we set schema_version

    to value schema_version

    .


This approach means that we update the schema on demand. If we are faced with a situation where we really want all objects of a particular type to be updated now, we write a map / reduce operation that loads and saves every entity of that type; schema migration happens automatically as part of this process with no downtime.

This now works if you are not dealing with models with model superclasses, whose schemas might also change. To solve this problem, you need to collect different values schema_version

across the class hierarchy when you come up with a value to store in schema_version

. We collect them by simply summing them up to come up with an official "version of the current schema" of the object, but other ways of doing this are possible.

+5


source







All Articles