GAE NDB Namespace Removal

In Google App Engine using NDB, how can I completely remove the full namespace?

The following code deletes 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 still exists when called:

 ndb.metadata.get_namespaces() 

and during production, exceptions arise when trying to delete a system view, for example:

 illegal key.path.element.type: __Stat_Ns_Kind__ 

How to completely exclude a namespace?

As @jeremydw pointed out, namespace information is stored as __namespace__ . However, this does not behave as a normal view, and, in particular, deleting objects has no 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) 
+4
source share
1 answer

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 seems like that the list of namespaces is stored in the Data Warehouse itself in a model named Namespace with the form __namespace__ .

Until I did this, perhaps you can find the corresponding object in the data store for the namespace that you want to destroy and then delete. Then, the next time you call ndb.metadata.get_namespaces , the query results should not include an entity for the namespace that you just deleted.

+1
source

Source: https://habr.com/ru/post/1502213/


All Articles