Created
April 24, 2017 17:24
-
-
Save tarunbhardwaj/f17b2451869a1ee588679c8464b40755 to your computer and use it in GitHub Desktop.
Move Google datastore data to different project or to different account
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
move_google_datastore | |
Move google datastore data to different project / different account. | |
:license: MIT | |
""" | |
from google.cloud import datastore | |
from google.cloud.datastore.entity import Entity | |
# Ref: https://googlecloudplatform.github.io/google-cloud-python/stable/datastore-client.html#google.cloud.datastore.client.Client | |
old_client = datastore.Client( | |
project='project-1', | |
namespace='namespace', | |
credentials=None # Use credentials accordingly | |
) | |
new_client = datastore.Client( | |
project='project-2' | |
namespace=None # None will move to default namespace | |
credentials=None # Use credentials accordingly | |
) | |
def move_kind(kind): | |
for old_entity in old_client.query(kind=kind).fetch(): | |
key = new_client.key(kind, old_entity.key.name) # Create new key | |
print "creating..", key | |
new_entity = Entity(key, exclude_from_indexes=tuple(old_entity.exclude_from_indexes)) # New entity with same indexes | |
for k, v in old_entity.items(): | |
if isinstance(v, datastore.key.Key): | |
if v.id: | |
# TODO: Test autogenerated ids can be moved ? | |
continue | |
else: | |
# continue with custom id(name) type | |
v = new_client.key(v.kind, v.name) | |
elif type(v) is list: | |
# XXX: If value contains list of keys, update all keys to new | |
# Keys under new project. | |
# Update this accordingly. | |
items = [] | |
for ki in v: | |
if ki.id: | |
# TODO: Test autogenerated ids can be moved ? | |
continue | |
else: | |
# continue with custom id(name) type | |
items.append(new_client.key(ki.kind, ki.name)) | |
v = items | |
new_entity[k] = v | |
new_client.put(new_entity) | |
for kind in ['Kind1', 'Kind2', 'Kind3']: | |
move_kind(kind) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this solution. The google-cloud package has now been deprecated, so I created a JavaScript version of this script. You can find that here.