Appengine -从标准DB升级到NDB - ReferenceProperties

时间:2022-03-04 04:29:29

I have an AppEngine application that I am considering upgrading to use the NDB database.

我有一个正在考虑升级的AppEngine应用程序来使用NDB数据库。

In my application, I have millions of objects that have old-style db references. I would like to know what the best migration path would be to get these ReferenceProperty values converted to KeyProperty values, or any other solution that would allow me to upgrade to NDB.

在我的应用程序中,我有数百万具有旧式db引用的对象。我想知道最好的迁移路径是什么,以便将这些ReferenceProperty值转换为KeyProperty值,或者其他任何可以让我升级到NDB的解决方案。

(I am hoping for something that doesn't involve massive batch processing of all of the elements in the database and computing the KeyProperty based on the ReferenceProperty -- something elegant would be nice)

(我希望不需要对数据库中的所有元素进行大量的批处理,并基于ReferenceProperty计算KeyProperty,这样比较优雅)

Examples of models that I would like to upgrade from db.Model to ndb.Model are the following:

我想从db升级的模型示例。模型以ndb。模型如下:

class UserModel(db.Model):
    ....

class MailMessageModel(db.Model):
    m_text = db.TextProperty()   
    m_from = db.ReferenceProperty(reference_class = UserModel)
    m_to = db.ReferenceProperty(reference_class = UserModel)

1 个解决方案

#1


12  

Good news, you don't have to make any changes to your persisted data, as ext.db and ndb read and write the exact same data.

好消息是,您不必对持久数据进行任何更改,因为ext.db和ndb读取并编写相同的数据。

Here's the quote from the NDB Cheat Sheet:

以下是来自NDB备忘单的报价:

No Datastore Changes Needed!

In case you wondered, despite the different APIs, NDB and the old ext.db package write exactly the same data to the Datastore. That means you don’t have to do any conversion to your datastore, and you can happily mix and match NDB and ext.db code, as long as the schema you use is equivalent. You can even convert between ext.db and NDB keys using ndb.Key.from_old_key() and key.to_old_key().

如果您想知道,尽管有不同的api,但NDB和旧的ext.db包将完全相同的数据写入数据存储。这意味着您不需要对数据存储进行任何转换,只要您使用的模式是等效的,您就可以轻松地混合和匹配NDB和ext.db代码。您甚至可以使用NDB . key. from_old_key()和key.to_old_key()在ext.db和NDB键之间进行转换。

The cheat sheet is a great guide to convert your model definitions. For example, changing your MailMessageModel should be as easy as:

备忘单是转换模型定义的一个很好的指南。例如,更改MailMessageModel应该很简单:

before:

之前:

class MailMessage(db.Model):
    m_text = db.TextProperty()
    m_from = db.ReferenceProperty(reference_class=UserModel)
    m_to = db.ReferenceProperty(reference_class=UserModel)

after:

后:

class MailMessage(ndb.Model):
    m_text = ndb.TextProperty()
    m_from = ndb.KeyProperty(kind=UserModel)
    m_to = ndb.KeyProperty(kind=UserModel)

I highly recommend using the cheat sheet to assist you with your migration.

我强烈推荐使用备忘单来帮助您迁移。

#1


12  

Good news, you don't have to make any changes to your persisted data, as ext.db and ndb read and write the exact same data.

好消息是,您不必对持久数据进行任何更改,因为ext.db和ndb读取并编写相同的数据。

Here's the quote from the NDB Cheat Sheet:

以下是来自NDB备忘单的报价:

No Datastore Changes Needed!

In case you wondered, despite the different APIs, NDB and the old ext.db package write exactly the same data to the Datastore. That means you don’t have to do any conversion to your datastore, and you can happily mix and match NDB and ext.db code, as long as the schema you use is equivalent. You can even convert between ext.db and NDB keys using ndb.Key.from_old_key() and key.to_old_key().

如果您想知道,尽管有不同的api,但NDB和旧的ext.db包将完全相同的数据写入数据存储。这意味着您不需要对数据存储进行任何转换,只要您使用的模式是等效的,您就可以轻松地混合和匹配NDB和ext.db代码。您甚至可以使用NDB . key. from_old_key()和key.to_old_key()在ext.db和NDB键之间进行转换。

The cheat sheet is a great guide to convert your model definitions. For example, changing your MailMessageModel should be as easy as:

备忘单是转换模型定义的一个很好的指南。例如,更改MailMessageModel应该很简单:

before:

之前:

class MailMessage(db.Model):
    m_text = db.TextProperty()
    m_from = db.ReferenceProperty(reference_class=UserModel)
    m_to = db.ReferenceProperty(reference_class=UserModel)

after:

后:

class MailMessage(ndb.Model):
    m_text = ndb.TextProperty()
    m_from = ndb.KeyProperty(kind=UserModel)
    m_to = ndb.KeyProperty(kind=UserModel)

I highly recommend using the cheat sheet to assist you with your migration.

我强烈推荐使用备忘单来帮助您迁移。