如何更新 NDB 模型的架构 [英] How to update an NDB Model's Schema

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

问题描述

我已经使用 App Engine 的旧 DB Datastore API 看到了这个问题的解决方案,但在使用较新的 NDB API 时找不到解决方案.

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?

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

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.

对于这样的更新:

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)

非常感谢!

推荐答案

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

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

  • 向我们使用的每个模型添加一个名为 schema_version 的 NDB 整数属性.这将是存储模型时处于活动状态的架构版本.
  • 为每个模型添加一个类属性SCHEMA_VERSION,代表代码的当前模式版本.它默认为 0,每当我们在模型上添加/删除/更改 NDB 属性时,我们都会增加它.
  • 让我们的模型实现一个 updateSchema 方法,该方法可以查看存储的和当前的版本并执行适当的迁移.
  • 在加载实体期间,我们会检查我们加载的实体的架构版本是否已过期.如果是,我们调用 updateSchema 来修复实体,然后再从我们的数据层返回它.然后我们将 schema_version 设置为 SCHEMA_VERSION 的值.
  • 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.

这种方法意味着我们正在按需更新架构.如果我们真的希望现在更新特定类型的所有实体,我们会编写一个 map/reduce 操作来加载和保存该类型的每个实体;架构迁移作为该过程的一部分自动发生,不会导致停机.

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.

现在,除非您正在处理具有模式也可以更改的模型超类的模型,否则这有效.为了解决这个问题,当您想出要存储在 schema_version 中的值时,您需要在类层次结构中向上收集 SCHEMA_VERSION 的不同值.我们通过简单地将它们相加得出实体的官方当前模式版本"来收集它们,但也可以通过其他方式来做到这一点.

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 模型的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆