Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Created April 14, 2011 17:07
Show Gist options
  • Save mccutchen/919951 to your computer and use it in GitHub Desktop.
Save mccutchen/919951 to your computer and use it in GitHub Desktop.
How to migrate App Engine datastore keys from one application to another
import os
from google.appengine.ext import db
def fixkey(key):
"""Takes a db.Key (or its string form) and checks to see if it is tied to
the current application. If not, the key is recreated for the current
application and returned as a string.
"""
app_id = os.environ.get('APPLICATION_ID')
if isinstance(key, basestring):
key = db.Key(key)
if key.app() != app_id:
newkey = db.Key.from_path(*key.to_path())
else:
newkey = key
return str(newkey)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment