I tried
query := datastore.NewQuery().KeysOnly()
but it only returns entities from the default namespace.
You need to do it in a for each namespace, see https://godoc.org/cloud.google.com/go/datastore#Query.Namespace.
The problem is I don't have the complete list of namespace.
My company uses a random namespace for integration test, so that multiple test can run concurrently. In tearDown, the script deletes all entities under the namespace. But sometimes tests halted before tearDown, leaving lots of random namespaces in the project.
You might find this helpful: https://cloud.google.com/datastore/docs/concepts/metadataqueries
I think you might be able to query somehow with the __namespace__ field. Though I think the Go sample code there is wrong (filed issue here: https://github.com/GoogleCloudPlatform/golang-samples/issues/204)
can you defer the tearDown? or in the case that your tests are panicing, can you recover and tearDown? There is surely a way to tearDown regardless of whether or not the tests have halted, no?
A keys-only "__namespace__" query should be able to list all the namespaces:
q := datastore.NewQuery("__namespace__").KeysOnly()
keys, err := client.GetAll(ctx, q, nil)
if err != nil {
log.Fatal(err)
}
for _, k := range keys {
fmt.Println(k.Name)
}
@rakyll
Excellent. That works. Thank you!
Closing this thread.
Most helpful comment
A keys-only "__namespace__" query should be able to list all the namespaces: