Last active
January 24, 2020 10:04
-
-
Save apstndb/5ffab4b7f90ad7ddb29ed8c529f6052c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"cloud.google.com/go/datastore" | |
"github.com/k0kubun/pp" | |
"log" | |
) | |
var client *datastore.Client | |
type Test struct{ Foo string } | |
func main() { | |
withTx := flag.Bool("with-tx", false, "with transaction?") | |
kind := flag.String("kind", "", "kind") | |
flag.Parse() | |
var err error | |
ctx := context.Background() | |
client, err = datastore.NewClient(ctx, "apstndb-fsds-tokyo") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
exec(ctx, *kind, *withTx) | |
} | |
func exec(ctx context.Context, kind string, withTx bool) { | |
var err error | |
key := datastore.NameKey(kind, "foo", nil) | |
_, err = client.Mutate(ctx, []*datastore.Mutation{datastore.NewInsert(key, &Test{Foo: "bar"})}...) | |
if err != nil { | |
log.Println(err) | |
} | |
var result []Test | |
_, err = client.GetAll(ctx, datastore.NewQuery(kind), &result) | |
if err != nil { | |
log.Println(err) | |
} | |
pp.Println(result) | |
result = []Test{} | |
key2 := datastore.NameKey(kind, "bar", nil) | |
mutations := []*datastore.Mutation{datastore.NewInsert(key, &Test{Foo: "baz"}), datastore.NewInsert(key2, &Test{Foo: "foobar"})} | |
if withTx { | |
_, err = client.RunInTransaction(ctx, func(tx *datastore.Transaction) error { | |
_, err = tx.Mutate(mutations...) | |
return err | |
}) | |
} else { | |
_, err = client.Mutate(ctx, mutations...) | |
} | |
if err != nil { | |
log.Println(err) | |
} | |
_, err = client.GetAll(ctx, datastore.NewQuery(kind), &result) | |
if err != nil { | |
log.Println(err) | |
} | |
pp.Println(result) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gcloud app describe --project=apstndb-fsds-tokyo --format="value(databaseType)" | |
CLOUD_DATASTORE_COMPATIBILITY | |
$ go run ./ --with-tx=false --kind=WithoutTx | |
[]main.Test{ | |
main.Test{ | |
Foo: "bar", | |
}, | |
} | |
2020/01/24 19:03:10 rpc error: code = AlreadyExists desc = entity already exists: app: "b~apstndb-fsds-tokyo" | |
path < | |
Element { | |
type: "WithoutTx" | |
name: "foo" | |
} | |
> | |
[]main.Test{ | |
main.Test{ | |
Foo: "foobar", | |
}, | |
main.Test{ | |
Foo: "bar", | |
}, | |
} | |
$ go run ./ --with-tx=true --kind=WithTx | |
[]main.Test{ | |
main.Test{ | |
Foo: "bar", | |
}, | |
} | |
2020/01/24 19:03:48 rpc error: code = AlreadyExists desc = entity already exists: app: "b~apstndb-fsds-tokyo" | |
path < | |
Element { | |
type: "WithTx" | |
name: "foo" | |
} | |
> | |
[]main.Test{ | |
main.Test{ | |
Foo: "bar", | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment