Google-cloud-go: datastore: Is there a way to extract entities from all namespace?

Created on 4 Mar 2017  路  5Comments  路  Source: googleapis/google-cloud-go

I tried

query := datastore.NewQuery().KeysOnly()

but it only returns entities from the default namespace.

Most helpful comment

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)
}

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alxmsl picture alxmsl  路  4Comments

imkira picture imkira  路  4Comments

deelienardy picture deelienardy  路  3Comments

purohit picture purohit  路  4Comments

dragan-cikic-shortcut picture dragan-cikic-shortcut  路  3Comments