How to update an NDB Model's Schema [closed]

Deadly 提交于 2019-12-06 02:25:31

So far as I know, NDB doesn't have any built-in schema migration support. We handle schema migrations this way:

  • Add an NDB integer property called schema_version to each Model we use. This will be the version of schema that was active when the model was stored.
  • Add a class attribute SCHEMA_VERSION to each Model, representing the current schema version of the code. It defaults to 0, and we bump this up whenever we add/remove/change NDB properties on the model.
  • Have our Models implement a updateSchema method that can look at the stored and current versions and perform the appropriate migrations.
  • During load of entities, we check to see if the schema version of the entity we loaded is out of date. If it is, we call updateSchema to fix up the entity before we return it from our data layer. We then set schema_version to the value of SCHEMA_VERSION.

This approach means that we are updating the schema on-demand. If we run into a situation where we really want all entities of a particular type to be updated now, we write a map/reduce operation that loads and saves each entity of that type; the schema migration happens automatically as a part of that process, without incurring downtime.

Now, this works unless you are dealing with models with model superclasses whose schemas can also change. To address that, you need to collect the different values of SCHEMA_VERSION up 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 the official "current schema version" of the entity, but other ways of doing this are possible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!