GAE,删除NDB命名空间 [英] GAE, delete NDB namespace

查看:89
本文介绍了GAE,删除NDB命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用NDB的Google App Engine中,如何完全删除整个命名空间?

In Google App Engine, using NDB, how can an entire namespace be entirely removed?

以下代码删除了所有实体:

The following code removes all entities:

def delete(namespace):

    namespace_manager.set_namespace(namespace)

    for kind in ndb.metadata.get_kinds():
        keys = [ k for k in ndb.Query(kind=kind).iter(keys_only=True) ]
        ndb.delete_multi( keys )

但是,在开发服务器上,调用时名称空间仍然存在:

However, on the dev server, the namespace is still there when calling:

ndb.metadata.get_namespaces()

,并且在生产环境中,尝试删除系统种类时会引发异常,例如:

and, in production, exceptions are raised when trying to delete system kind, for example:

illegal key.path.element.type: __Stat_Ns_Kind__

如何彻底清除名称空间?

@jeremydw指出,名称空间信息以__namespace__类型存储.但是,这并不像普通的那样,尤其是删除实体似乎没有任何作用:

How can a namespace be wiped out entirely?

As pointed out by @jeremydw, namespace information is stored in a __namespace__ kind. However, this does not behave as a normal kind and in particular, deleting entities does not seem to have any effect:

id_namepace = 'some_test'

print list( ndb.Query(kind='__namespace__') ) 
# id_namespace is not present
# SomeModel is some existing model
key_entity = ndb.Key('SomeModel', 'some_string_id', namespace=id_namepace)
entity = datastore.CustomerAction(key=key_entity)
entity.put()
print list( ndb.Query(kind='__namespace__') )
# id_namespace is present (expected, namespace was implicitly created by adding one entity in it)
key_entity.delete()
print list( ndb.Query(kind='__namespace__') )
# id_namespace is present (expected, namespace still exists but contains no entities)
key_namespace = ndb.metadata.Namespace.key_for_namespace(id_namepace)
key_namespace.delete()
print list( ndb.Query(kind='__namespace__') )
# id_namespace is still present (not expected, kind='__namespace__' does not behave as a normal kind)

推荐答案

查看SDK中ndb.metadata.get_namespaces的实际实现(

Looking at the actual implementation of ndb.metadata.get_namespaces in the SDK (https://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/ndb/metadata.py#224), it looks like the list of namespaces is stored in Datastore itself, in a model named Namespace with kind __namespace__.

虽然我自己从未尝试过此操作,但是也许您可以在数据存储区中找到要删除的名称空间的对应实体,然后将其删除.然后,下次调用ndb.metadata.get_namespaces时,查询结果不应包含刚删除的名称空间的实体.

While I haven't attempted this ever myself, perhaps you can find the corresponding entity in the Datastore for the namespace that you want to obliterate, and then delete it. Then, next time you call ndb.metadata.get_namespaces, the query results should not include the entity for the namespace that you just deleted.

这篇关于GAE,删除NDB命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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