有没有一种方法可以使用Python3(WITHOUT Dataflow)从数据存储名称空间中删除所有实体? [英] Is there a way to delete all entities from a Datastore namespace Using Python3 (WITHOUT Dataflow)?

查看:61
本文介绍了有没有一种方法可以使用Python3(WITHOUT Dataflow)从数据存储名称空间中删除所有实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在测试期间上载测试数据之前清除Datastore命名空间.将Cloud Datastore API与Python3结合使用.

Need to wipe down a Datastore namespace before test data is uploaded during testing. Using Cloud Datastore API with Python3.

我在Python3中将数据存储区与App Engine一起使用.为了进行测试,我使用Cloud Datastore API编写了一个脚本,用于将几个不同种类的实体上载到数据存储区.由于这是一个小项目,目前只有4种,每种类型只有2-3个实体.

I'm using Datastore with App Engine in Python3. For testing purposes, I have written a script using the Cloud Datastore API to upload several entities of different kinds to datastore. As this is a small project, at the moment there are only 4 kinds and only 2-3 entities per kind.

我想向脚本中添加脚本以擦除数据存储区中包含测试数据的特定命名空间.我希望它在数据上传和测试之前运行,以便每次都可以从干净的状态开始进行测试.我正在使用Cloud Builder将实体上传到数据存储区,并在docker容器中运行测试,然后再部署到App Engine.

I want to add to my pipeline a script to wipe down a particular namespace in Datastore that will contain my test data. I want this to run before the upload of the data and testing so the tests can start from a clean slate every time. I'm using cloud builder to upload the entities to datastore and run my tests in a docker container before deploying to app engine.

目前,我能找到的唯一解决方案是使用Dataflow(完全相信这太过分了),或者使用它的密钥单独删除每个实体.如果可能的话,我宁愿抹掉整个命名空间.

At the moment the only solutions I can find are to use Dataflow (totally overkill for this I believe), or to remove each entity individually using it's key. I'd prefer to just wipe down the entire namespace if possible.

如果有人对此有任何建议或建议,请告诉我!

If anyone has any advice or suggestions on how to do this please let me know!

推荐答案

您可以使用python编写脚本来删除特定名称空间中的所有种类. 假设您事先知道种类的名称.

You can write a script in python to delete all the kinds in a particular namespace. Assuming that you know the name of kinds beforehand.

from google.appengine.ext import ndb
from google.appengine.api import namespace_manager

namespace = "PROVIDE_NAMESPACE_HERE"
kind_list = [kind_1,kind_2,kind_3,kind_4]

namespace_manager.set_namespace(namespace) # will set to the namespace provided

for a_kind in kind_list:
    # will fetch keys of all objects in that kind
    kind_keys = a_kind.gql("").fetch(keys_only = True) 
    # will delete all the keys at once
    ndb.delete_multi(kind_keys) 

从特定名称空间删除所有种类后,您的名称空间将在Cloud Datastore中显示24小时左右,如果在此之后不包含任何种类,它将被自动删除.

After deleting all the kinds from a particular namespace your namespace will be visible for around 24 hours in Cloud Datastore and if after that it doesn't contain any kind it will be automatically deleted.

希望这能回答您的问题!

Hope this answers your question!!

这篇关于有没有一种方法可以使用Python3(WITHOUT Dataflow)从数据存储名称空间中删除所有实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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