如何更新NDB模型的模式

如何更新NDB模型的模式

本文介绍了如何更新NDB模型的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用,看到了此问题的解决方案,但在使用新的 NDB API时找不到解决方案。

添加迁移支持的最佳方式是什么,以便我能够从旧版本的模式迁移到新版本。最好是编写一个迁移脚本,以及它如何工作?



类似于像这样迁移架构(请注意示例在 NDB 中):

  class图片(ndb.Model):
作者= ndb.StringProperty()
png_data = ndb.BlobProperty()
name = ndb.StringProperty(default ='')#唯一名称。

到这样的更新:

 class图片(ndb.Model):
author = ndb.StringProperty()
png_data = ndb.BlobProperty()
name = ndb.StringProperty默认='')#唯一的名称。

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

非常感谢!

解决方案

据我所知,NDB没有任何内置的模式迁移支持。我们以这种方式处理模式迁移:


  • 添加一个名为 schema_version 的NDB整数属性我们使用的每个模型。这将是存储模型时激活的模式版本。

  • 为每个模型添加一个类属性 SCHEMA_VERSION ,代表代码的当前模式版本。它默认为0,当我们在模型上添加/删除/更改NDB属性时,我们会碰到这个问题。

  • 让我们的模型实现一个 updateSchema 方法,它可以查看存储的和当前的版本并执行适当的迁移。
  • 在加载实体期间,我们检查是否加载了实体的模式版本的日期。如果是这样,我们在从数据层返回之前调用 updateSchema 来修复实体。然后,我们将 schema_version 设置为 SCHEMA_VERSION 的值。


这种方法意味着我们正在按需更新模式。如果我们遇到了一种情况,那就是我们真的希望现在更新某个特定类型的所有实体,我们编写一个map / reduce操作来加载和保存每种类型的实体。模式迁移作为该进程的一部分自动发生,而不会导致停机。



现在,除非您正在使用架构可能更改的模型超类处理模型,为了解决这个问题,当你想出一个值来存储 schema_version 时,需要在类层次结构中收集 SCHEMA_VERSION C $ C>。我们通过简单地总结它们以获得实体的官方当前模式版本来收集它们,但是也可以采用其他方式来做到这一点。


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

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

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

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

To an updated one like this:

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!

解决方案

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.

这篇关于如何更新NDB模型的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:24